Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,19 +57,50 @@ conversation_state = {}
|
|
| 57 |
|
| 58 |
|
| 59 |
# Truncate prompt
|
| 60 |
-
def truncate_prompt(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
model_name = models[model_alias]
|
| 62 |
context_length = context_window.get(model_name, 4096)
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
def chat_with_models(
|
| 69 |
user_input, model_alias, models, conversation_state, timeout=TIMEOUT
|
| 70 |
):
|
| 71 |
model_name = models[model_alias]
|
| 72 |
-
truncated_input = truncate_prompt(user_input, model_alias, models)
|
| 73 |
conversation_state.setdefault(model_name, []).append(
|
| 74 |
{"role": "user", "content": user_input}
|
| 75 |
)
|
|
@@ -81,7 +112,7 @@ def chat_with_models(
|
|
| 81 |
try:
|
| 82 |
response = client.chat.completions.create(
|
| 83 |
model=model_name,
|
| 84 |
-
messages=
|
| 85 |
)
|
| 86 |
model_response["content"] = response.choices[0].message.content
|
| 87 |
except Exception as e:
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
# Truncate prompt
|
| 60 |
+
def truncate_prompt(user_input, model_alias, models, conversation_state):
|
| 61 |
+
"""
|
| 62 |
+
Truncate the conversation history and user input to fit within the model's context window.
|
| 63 |
+
|
| 64 |
+
Args:
|
| 65 |
+
user_input (str): The latest input from the user.
|
| 66 |
+
model_alias (str): Alias for the model being used (e.g., "Model A", "Model B").
|
| 67 |
+
models (dict): Dictionary mapping model aliases to their names.
|
| 68 |
+
conversation_state (dict): State containing the conversation history for all models.
|
| 69 |
+
|
| 70 |
+
Returns:
|
| 71 |
+
str: Truncated conversation history and user input.
|
| 72 |
+
"""
|
| 73 |
model_name = models[model_alias]
|
| 74 |
context_length = context_window.get(model_name, 4096)
|
| 75 |
+
|
| 76 |
+
# Get the full conversation history for the model
|
| 77 |
+
history = conversation_state.get(model_name, [])
|
| 78 |
+
full_conversation = [{"role": msg["role"], "content": msg["content"]} for msg in history]
|
| 79 |
+
full_conversation.append({"role": "user", "content": user_input})
|
| 80 |
+
|
| 81 |
+
# Convert to JSON string for accurate length measurement
|
| 82 |
+
json_conversation = json.dumps(full_conversation)
|
| 83 |
+
|
| 84 |
+
if len(json_conversation) <= context_length:
|
| 85 |
+
# If the full conversation fits, return it as-is
|
| 86 |
+
return full_conversation
|
| 87 |
+
|
| 88 |
+
# Truncate based on the current round
|
| 89 |
+
if not history: # First round, truncate FILO
|
| 90 |
+
while len(json.dumps(full_conversation)) > context_length:
|
| 91 |
+
full_conversation.pop(0) # Remove from the start
|
| 92 |
+
else: # Subsequent rounds, truncate FIFO
|
| 93 |
+
while len(json.dumps(full_conversation)) > context_length:
|
| 94 |
+
full_conversation.pop(-1) # Remove from the end
|
| 95 |
+
|
| 96 |
+
return full_conversation
|
| 97 |
|
| 98 |
|
| 99 |
def chat_with_models(
|
| 100 |
user_input, model_alias, models, conversation_state, timeout=TIMEOUT
|
| 101 |
):
|
| 102 |
model_name = models[model_alias]
|
| 103 |
+
truncated_input = truncate_prompt(user_input, model_alias, models, conversation_state)
|
| 104 |
conversation_state.setdefault(model_name, []).append(
|
| 105 |
{"role": "user", "content": user_input}
|
| 106 |
)
|
|
|
|
| 112 |
try:
|
| 113 |
response = client.chat.completions.create(
|
| 114 |
model=model_name,
|
| 115 |
+
messages=truncated_input,
|
| 116 |
)
|
| 117 |
model_response["content"] = response.choices[0].message.content
|
| 118 |
except Exception as e:
|