Spaces:
Runtime error
Runtime error
history = history[-MAX_HISTORY_LENGTH:]
Browse files
app.py
CHANGED
|
@@ -6,6 +6,8 @@ import os
|
|
| 6 |
|
| 7 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 8 |
MAX_WITHOUT_KEY = 30
|
|
|
|
|
|
|
| 9 |
|
| 10 |
PROMPTS = {
|
| 11 |
"具體範例 Concrete examples": "你是一個樂於助人的AI tutor。你會透過提供具體範例以幫助他們學習新的概念。你總是調整你的範例以符合學生的生活及prior knowledge,你會舉出很多跟舉體且生活相關的範例以幫助學生,並透過一問一答的方式,確認學生的理解程度,請跟我解釋 {}?",
|
|
@@ -59,7 +61,17 @@ def openai_stream(history, openai_key, chat_model, use_key):
|
|
| 59 |
):
|
| 60 |
content = chunk["choices"][0].get("delta", {}).get("content")
|
| 61 |
if content:
|
| 62 |
-
history[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
yield history
|
| 64 |
|
| 65 |
|
|
|
|
| 6 |
|
| 7 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 8 |
MAX_WITHOUT_KEY = 30
|
| 9 |
+
MAX_HISTORY_LENGTH = 10
|
| 10 |
+
|
| 11 |
|
| 12 |
PROMPTS = {
|
| 13 |
"具體範例 Concrete examples": "你是一個樂於助人的AI tutor。你會透過提供具體範例以幫助他們學習新的概念。你總是調整你的範例以符合學生的生活及prior knowledge,你會舉出很多跟舉體且生活相關的範例以幫助學生,並透過一問一答的方式,確認學生的理解程度,請跟我解釋 {}?",
|
|
|
|
| 61 |
):
|
| 62 |
content = chunk["choices"][0].get("delta", {}).get("content")
|
| 63 |
if content:
|
| 64 |
+
# Assuming the format of history is [(message1, response1), (message2, response2), ...]
|
| 65 |
+
if history:
|
| 66 |
+
history[-1][1] += content # Update the last response with the new content.
|
| 67 |
+
else:
|
| 68 |
+
# Handle the case where you might need to add a new message-response pair.
|
| 69 |
+
# This is just a generic example. You might need a different approach based on your exact requirements.
|
| 70 |
+
history.append((messages[-1]["content"], content))
|
| 71 |
+
|
| 72 |
+
# Ensure history keeps only the last 10 message-response pairs.
|
| 73 |
+
history = history[-MAX_HISTORY_LENGTH:]
|
| 74 |
+
|
| 75 |
yield history
|
| 76 |
|
| 77 |
|