Spaces:
Runtime error
Runtime error
working with openai. hitting context win
Browse files
app.py
CHANGED
@@ -127,69 +127,44 @@ assessor = gr.Interface(fn=assess,
|
|
127 |
description="Enter a Japanese sentence and your English translation attempt to receive evaluation feedback."
|
128 |
)
|
129 |
|
|
|
130 |
|
131 |
-
def respond(learner_data, learning_content, teacher_prompt, message, chat_history):
|
132 |
-
|
133 |
-
prompt = (
|
134 |
-
f"Teacher prompt: {teacher_prompt}\n"
|
135 |
-
f"Learner data: {learner_data}\n"
|
136 |
-
f"Learning content: {learning_content}\n"
|
137 |
-
|
138 |
-
f"Student: {message}\n"
|
139 |
-
"Chatbot:"
|
140 |
-
)
|
141 |
-
|
142 |
-
# Call the OpenAI API
|
143 |
-
response = client.chat.completions.create(
|
144 |
-
messages=[
|
145 |
-
{
|
146 |
-
"role": "user",
|
147 |
-
"content": prompt,
|
148 |
-
}
|
149 |
-
],
|
150 |
-
model="gpt-3.5-turbo",
|
151 |
-
temperature=0.7,
|
152 |
-
max_tokens=150,
|
153 |
-
stop=["\n", " Student:", " Chatbot:"], # Stop generating if these patterns are found
|
154 |
-
)
|
155 |
|
|
|
156 |
|
|
|
157 |
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
-
chat_history.append((message, bot_message))
|
162 |
-
debug_print(chat_history)
|
163 |
-
return chat_history
|
164 |
|
165 |
with gr.Blocks() as demo:
|
166 |
with gr.Row():
|
167 |
learner_data = gr.Textbox(label="Learner Data", placeholder="Enter learner data here...", lines=4, value="Honoka is a Japanese EFL student.")
|
168 |
learning_content = gr.Textbox(label="Learning Content", placeholder="Enter learning content here...", lines=4, value=learning_content)
|
169 |
teacher_prompt = gr.Textbox(label="Teacher Prompt", placeholder="Enter chat guidance here...", lines=4,
|
170 |
-
value="You are a professional EFL teacher. Guide the conversation to discuss the
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
with gr.Row():
|
178 |
-
msg = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
179 |
-
submit_button = gr.Button("Send")
|
180 |
-
|
181 |
-
submit_button.click(
|
182 |
-
respond,
|
183 |
-
inputs=[learner_data, learning_content, teacher_prompt, msg, chatbot],
|
184 |
-
outputs=chatbot,
|
185 |
-
)
|
186 |
-
|
187 |
|
188 |
-
|
189 |
-
clear = gr.ClearButton([msg])
|
190 |
-
|
191 |
-
#msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
192 |
|
|
|
193 |
|
194 |
-
if __name__ == "__main__":
|
195 |
-
demo.launch(debug=True)
|
|
|
127 |
description="Enter a Japanese sentence and your English translation attempt to receive evaluation feedback."
|
128 |
)
|
129 |
|
130 |
+
history_openai_format = []
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
+
def predict(message, history):
|
134 |
|
135 |
+
debug_print("### History:", history_openai_format)
|
136 |
|
137 |
+
for human, assistant in history:
|
138 |
+
history_openai_format.append({"role": "user", "content": human })
|
139 |
+
history_openai_format.append({"role": "assistant", "content":assistant})
|
140 |
+
history_openai_format.append({"role": "user", "content": message})
|
141 |
+
|
142 |
+
response = client.chat.completions.create(
|
143 |
+
model='gpt-3.5-turbo',
|
144 |
+
messages=history_openai_format,
|
145 |
+
temperature=1.0, #?
|
146 |
+
#max_tokens=150,
|
147 |
+
stream=True
|
148 |
+
)
|
149 |
+
|
150 |
+
partial_message = ""
|
151 |
+
for chunk in response:
|
152 |
+
if chunk.choices[0].delta.content is not None:
|
153 |
+
partial_message = partial_message + chunk.choices[0].delta.content
|
154 |
+
yield partial_message
|
155 |
|
|
|
|
|
|
|
156 |
|
157 |
with gr.Blocks() as demo:
|
158 |
with gr.Row():
|
159 |
learner_data = gr.Textbox(label="Learner Data", placeholder="Enter learner data here...", lines=4, value="Honoka is a Japanese EFL student.")
|
160 |
learning_content = gr.Textbox(label="Learning Content", placeholder="Enter learning content here...", lines=4, value=learning_content)
|
161 |
teacher_prompt = gr.Textbox(label="Teacher Prompt", placeholder="Enter chat guidance here...", lines=4,
|
162 |
+
value="You are a professional EFL teacher. Guide the conversation to discuss the Learning Content below.")
|
163 |
+
|
164 |
+
# pre prompt the history_openai_format list
|
165 |
+
history_openai_format.append({"role": "system", "content": f"{teacher_prompt.value} Learner Data: {learner_data.value}. Learning Content: {learning_content.value}. "})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
166 |
|
167 |
+
gr.ChatInterface(predict)
|
|
|
|
|
|
|
168 |
|
169 |
+
demo.launch(debug=True)
|
170 |
|
|
|
|