-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py.bak
237 lines (195 loc) · 9.02 KB
/
app.py.bak
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Original Source Code
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
import openai
import os
import json
from dotenv import load_dotenv
import logging
import re
import threading
load_dotenv()
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
app = Flask(__name__)
CORS(app) # Enable CORS for all routes
# Set your OpenAI API key securely
openai.api_key = os.getenv('OPENAI_API_KEY') # Ensure you have set this environment variable
# @app.route('/')
# def index():
# return render_template('index.html')
# How we can handle the in-memory storage for conversations
# Data Struct: {user_id: { message1, message2, ...}}
conversation_history = []
MAX_HISTORY = 3 # Number of user messages to keep
# We are stopping the number of messages for the total tokens sent
MAX_HISTORY = 3
# This handles the "Assist Me" Button
@app.route('/api/assist', methods=['POST'])
def assist():
data = request.json
text = data.get('text', '')
messages = [
{"role": "system", "content": "You are a helpful assistant proficient in correcting Spanish text."},
{"role": "user", "content": f"Correct the following Spanish text and explain any mistakes:\n\n{text}"}
]
response = openai.ChatCompletion.create(
model='gpt-4o-mini',
messages=messages,
max_tokens=900,
temperature=0.7,
)
return jsonify({'result': response.choices[0].message['content'].strip()})
# This handles the conjugate button and table
@app.route('/api/conjugate', methods=['POST'])
def conjugate():
data = request.json
verb = data.get('verb', '')
logger.debug(f"Received request to conjugate verb: {verb}")
messages = [
{"role": "system", "content": "You are a helpful assistant proficient in Spanish verb conjugations."},
{"role": "user", "content": f"Provide the conjugations of the Spanish verb '{verb}' in the following tenses: present, subjunctive, preterite, imperfect, and future. Do not include 'vosotros' forms. Return ONLY the JSON data with keys for each tense ('present', 'subjunctive', etc.), and for each tense, provide a dictionary with keys 'yo', 'tú', 'él/ella/usted', 'nosotros', 'ellos/ellas/ustedes', and their corresponding conjugations."}
]
try:
response = openai.ChatCompletion.create(
model='gpt-4o-mini',
messages=messages,
max_tokens=700,
temperature=0.7,
)
logger.debug(f"Received response from OpenAI: {response}")
content = response.choices[0].message['content'].strip()
logger.debug(f"Extracted content: {content}")
# Extract JSON content
json_match = re.search(r'\{[\s\S]*\}', content)
if json_match:
json_content = json_match.group(0)
logger.debug(f"Extracted JSON content: {json_content}")
conjugations = json.loads(json_content)
logger.debug(f"Parsed conjugations: {conjugations}")
return jsonify({'result': conjugations})
else:
logger.error("No JSON content found in the response")
return jsonify({'error': 'No JSON content found in the response', 'raw_response': content})
except json.JSONDecodeError as e:
logger.error(f"JSON decode error: {str(e)}")
return jsonify({'error': 'Failed to parse conjugations', 'raw_response': content})
except Exception as e:
logger.error(f"Unexpected error: {str(e)}")
return jsonify({'error': 'An unexpected error occurred', 'details': str(e)})
@app.route('/api/define', methods=['POST'])
def define():
data = request.json
word = data.get('word', '')
messages = [
{"role": "system", "content": "You are a helpful assistant proficient in defining Spanish words in English and vice versa."},
{"role": "user", "content": f"Define the Spanish or English word '{word}' in English or Spanish and provide a sentence using the word. If the word has both a definition for its form as a noun or adjective provide both."}
]
response = openai.ChatCompletion.create(
model='gpt-4o-mini',
messages=messages,
max_tokens=200,
temperature=0.7,
)
return jsonify({'result': response.choices[0].message['content'].strip()})
# Handles the Popup box Modal questions
# @app.route('/api/question', methods=['POST'])
# def question():
# data = request.json
# query = data.get('query', '')
# messages = [
# {"role": "system", "content": "You are a helpful assistant for Spanish language learning. Consider cultural and situational nuances while assisting."},
# {"role": "user", "content": query}
# ]
# response = openai.ChatCompletion.create(
# model='gpt-4o-mini',
# messages=messages,
# max_tokens=500,
# temperature=0.7,
# )
# return jsonify({'result': response.choices[0].message['content'].strip()})
# Handles the Chat Window Feature w/ History
SYSTEM_PROMPT = {
"role": "system",
"content": "You are a helpful assistant for Spanish language learning. Consider cultural and situational nuances while assisting."
}
conversation_history.append(SYSTEM_PROMPT.copy())
@app.route('/api/chatbot', methods=['POST'])
def question():
data = request.json
query = data.get('query', '').strip()
if not query:
logger.debug("Received empty query.")
return jsonify({'error': 'Missing or empty query in request'}), 400
# Append the user message
conversation_history.append({"role": "user", "content": query})
logger.debug(f"Appended user message: {query}")
# Prune conversation history to keep only the last MAX_HISTORY user messages and their assistant responses
# Including system prompt
# Find indices of all user messages
user_indices = [i for i, msg in enumerate(conversation_history) if msg['role'] == 'user']
if len(user_indices) > MAX_HISTORY:
# Calculate the index to start keeping messages
keep_from = user_indices[-MAX_HISTORY]
# Retain system prompt and messages from keep_from onwards
pruned_history = [SYSTEM_PROMPT.copy()] + conversation_history[keep_from:]
conversation_history[:] = pruned_history
logger.debug(f"Pruned conversation history to keep last {MAX_HISTORY} user messages.")
# Prepare messages to send to OpenAI
messages_to_send = conversation_history.copy()
logger.debug(f"Messages sent to OpenAI: {json.dumps(messages_to_send, ensure_ascii=False)}")
try:
response = openai.ChatCompletion.create(
model='gpt-4o-mini',
messages=messages_to_send,
max_tokens=500,
temperature=0.7,
)
assistant_message = response.choices[0].message['content'].strip()
logger.debug(f"Assistant response: {assistant_message}")
# Append assistant response to the history
conversation_history.append({"role": "assistant", "content": assistant_message})
logger.debug("Appended assistant message to conversation history.")
return jsonify({'result': assistant_message})
except Exception as e:
logger.error(f"Error communicating with OpenAI: {str(e)}")
return jsonify({'error': 'Failed to get response from assistant', 'details': str(e)}), 500
# Example endpoint to reset conversation (optional)
@app.route('/api/reset_conversation', methods=['POST'])
def reset_conversation():
conversation_history.clear()
conversation_history.append(SYSTEM_PROMPT.copy())
logger.debug("Conversation history has been reset.")
return jsonify({'result': 'Conversation history reset successfully'})
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
# @app.route('/api/translate_to_spanish', methods=['POST'])
# def translate_to_spanish():
# data = request.json
# text = data.get('text', '')
# messages = [
# {"role": "system", "content": "You are a helpful assistant proficient in translating English to Spanish."},
# {"role": "user", "content": f"Translate the following English text to Spanish, considering cultural context:\n\n{text}"}
# ]
# response = openai.ChatCompletion.create(
# model='gpt-4o-mini',
# messages=messages,
# max_tokens=900,
# temperature=0.7,
# )
# return jsonify({'result': response.choices[0].message['content'].strip()})
# @app.route('/api/translate_to_english', methods=['POST'])
# def translate_to_english():
# data = request.json
# text = data.get('text', '')
# messages = [
# {"role": "system", "content": "You are a helpful assistant proficient in translating Spanish to English."},
# {"role": "user", "content": f"Translate the following Spanish text to English, considering cultural context:\n\n{text}"}
# ]
# response = openai.ChatCompletion.create(
# model='gpt-4o-mini',
# messages=messages,
# max_tokens=900,
# temperature=0.7,
# )
# return jsonify({'result': response.choices[0].message['content'].strip()})