Update main.py
Browse files
main.py
CHANGED
|
@@ -108,7 +108,6 @@ def simulate_text_conversation(caller_number: str, initial_message: str, convers
|
|
| 108 |
system_prompt = (
|
| 109 |
f"You are a call assistant. The unknown caller's text message is '{initial_message}'.\n"
|
| 110 |
"Simulate a multi-turn conversation. Do not mention that you are a virtual assistant. Act like a human"
|
| 111 |
-
|
| 112 |
)
|
| 113 |
else:
|
| 114 |
system_prompt = "You are a call assistant. Do not mention that you are a virtual assistant. Act like a human"
|
|
@@ -118,15 +117,13 @@ def simulate_text_conversation(caller_number: str, initial_message: str, convers
|
|
| 118 |
for entry in chat_history:
|
| 119 |
if entry.get("role") in {"system", "user", "assistant"}:
|
| 120 |
messages.append({"role": entry["role"], "content": entry["content"]})
|
| 121 |
-
|
| 122 |
-
|
| 123 |
assistant_response = llm.invoke(messages)
|
| 124 |
if hasattr(assistant_response, "content"):
|
| 125 |
assistant_response = assistant_response.content
|
| 126 |
else:
|
| 127 |
assistant_response = str(assistant_response)
|
| 128 |
|
| 129 |
-
update_chat_history(caller_number, "user", initial_message)
|
| 130 |
update_chat_history(caller_number, "assistant", assistant_response)
|
| 131 |
|
| 132 |
return assistant_response
|
|
@@ -150,15 +147,13 @@ def simulate_call_conversation(caller_number: str, initial_message: str, convers
|
|
| 150 |
for entry in chat_history:
|
| 151 |
if entry.get("role") in {"system", "user", "assistant"}:
|
| 152 |
messages.append({"role": entry["role"], "content": entry["content"]})
|
| 153 |
-
|
| 154 |
-
|
| 155 |
assistant_response = llm.invoke(messages)
|
| 156 |
if hasattr(assistant_response, "content"):
|
| 157 |
assistant_response = assistant_response.content
|
| 158 |
else:
|
| 159 |
assistant_response = str(assistant_response)
|
| 160 |
|
| 161 |
-
update_chat_history(caller_number, "user", initial_message)
|
| 162 |
update_chat_history(caller_number, "assistant", assistant_response)
|
| 163 |
|
| 164 |
return assistant_response
|
|
@@ -192,6 +187,9 @@ def get_contact(phone: str):
|
|
| 192 |
|
| 193 |
@app.post("/incoming-message")
|
| 194 |
def process_incoming_message(incoming: IncomingMessage):
|
|
|
|
|
|
|
|
|
|
| 195 |
if contacts_collection.find_one({"phone": incoming.phone}):
|
| 196 |
return {
|
| 197 |
"status": "primary",
|
|
|
|
| 108 |
system_prompt = (
|
| 109 |
f"You are a call assistant. The unknown caller's text message is '{initial_message}'.\n"
|
| 110 |
"Simulate a multi-turn conversation. Do not mention that you are a virtual assistant. Act like a human"
|
|
|
|
| 111 |
)
|
| 112 |
else:
|
| 113 |
system_prompt = "You are a call assistant. Do not mention that you are a virtual assistant. Act like a human"
|
|
|
|
| 117 |
for entry in chat_history:
|
| 118 |
if entry.get("role") in {"system", "user", "assistant"}:
|
| 119 |
messages.append({"role": entry["role"], "content": entry["content"]})
|
| 120 |
+
# The initial user message is already saved in chat history
|
|
|
|
| 121 |
assistant_response = llm.invoke(messages)
|
| 122 |
if hasattr(assistant_response, "content"):
|
| 123 |
assistant_response = assistant_response.content
|
| 124 |
else:
|
| 125 |
assistant_response = str(assistant_response)
|
| 126 |
|
|
|
|
| 127 |
update_chat_history(caller_number, "assistant", assistant_response)
|
| 128 |
|
| 129 |
return assistant_response
|
|
|
|
| 147 |
for entry in chat_history:
|
| 148 |
if entry.get("role") in {"system", "user", "assistant"}:
|
| 149 |
messages.append({"role": entry["role"], "content": entry["content"]})
|
| 150 |
+
# The transcribed message is already in chat history
|
|
|
|
| 151 |
assistant_response = llm.invoke(messages)
|
| 152 |
if hasattr(assistant_response, "content"):
|
| 153 |
assistant_response = assistant_response.content
|
| 154 |
else:
|
| 155 |
assistant_response = str(assistant_response)
|
| 156 |
|
|
|
|
| 157 |
update_chat_history(caller_number, "assistant", assistant_response)
|
| 158 |
|
| 159 |
return assistant_response
|
|
|
|
| 187 |
|
| 188 |
@app.post("/incoming-message")
|
| 189 |
def process_incoming_message(incoming: IncomingMessage):
|
| 190 |
+
# Save the incoming message in MongoDB chat history
|
| 191 |
+
update_chat_history(incoming.phone, "user", incoming.message)
|
| 192 |
+
|
| 193 |
if contacts_collection.find_one({"phone": incoming.phone}):
|
| 194 |
return {
|
| 195 |
"status": "primary",
|