Skip to content

Commit

Permalink
delete conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronClaydon committed Jun 5, 2024
1 parent c89df25 commit 09aa6bc
Showing 1 changed file with 57 additions and 37 deletions.
94 changes: 57 additions & 37 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class OnMessageReceived(BaseModel):
Media : str = ""
ChannelMetadata : str = ""

class OnConversationRemoved(BaseModel):
EventType : str = ""
ConversationSid : str = ""


@app.get("/assistant")
async def get():
updateConversationHistory("dlkfngjkdnfg", json.dumps({}))
Expand Down Expand Up @@ -62,6 +67,14 @@ def updateConversationHistory(conversation: AssistantConversation):

collection.update_one(searchQuery, {"$set": conversation.model_dump()}, upsert=True)

def deleteConversationHistory(conversationID : str):
db = client[os.environ['TRAVIGO_MONGODB_DATABASE']]
collection = db['assistant_conversations']

searchQuery = {"ConversationID": conversationID}

collection.delete_one(searchQuery)

@app.post("/assistant/twilio/webhook")
async def get(request: Request):
client = Client(os.environ['TRAVIGO_TWILIO_ACCOUNT_SID'], os.environ['TRAVIGO_TWILIO_AUTH_TOKEN'])
Expand All @@ -71,55 +84,62 @@ async def get(request: Request):
event_type = form_data['EventType']

if event_type == "onMessageAdded":
received_message = OnMessageReceived(**form_data)
print(received_message)
received_message = OnMessageReceived(**form_data)
print(received_message)

logging.info("Message received")

# Load previous history
conversationHistory = getConversationHistory(received_message.ConversationSid)
previousHistoryDict = json.loads(conversationHistory.Messages)
previousHistory = []

logging.info("Message received")
for historyItem in previousHistoryDict:
parts = []
for partDef in historyItem['parts']:
if 'text' in partDef:
part = Part.from_text(partDef['text'])
elif 'function_call' in partDef or 'function_response' in partDef:
part = Part.from_dict(partDef)

# Load previous history
conversationHistory = getConversationHistory(received_message.ConversationSid)
previousHistoryDict = json.loads(conversationHistory.Messages)
previousHistory = []
parts.append(part)

for historyItem in previousHistoryDict:
parts = []
for partDef in historyItem['parts']:
if 'text' in partDef:
part = Part.from_text(partDef['text'])
elif 'function_call' in partDef or 'function_response' in partDef:
part = Part.from_dict(partDef)
content = Content(role=historyItem['role'], parts=parts)
previousHistory.append(content)

parts.append(part)
# Create new chat
assistant = Assistant()
assistant.create_chat(history=previousHistory)

content = Content(role=historyItem['role'], parts=parts)
previousHistory.append(content)
response = assistant.message(received_message.Body)

# Create new chat
assistant = Assistant()
assistant.create_chat(history=previousHistory)
for part in response.candidates[0].content.parts:
send_message = client.conversations \
.v1 \
.services(os.environ['TRAVIGO_TWILIO_SERVICE_SID']) \
.conversations(received_message.ConversationSid) \
.messages \
.create(author='system', body=part.text)

print(send_message)

response = assistant.message(received_message.Body)
# Update database history
history = []

for part in response.candidates[0].content.parts:
send_message = client.conversations \
.v1 \
.services(os.environ['TRAVIGO_TWILIO_SERVICE_SID']) \
.conversations(received_message.ConversationSid) \
.messages \
.create(author='system', body=part.text)

print(send_message)
for historyItem in assistant.chat.history:
history.append(historyItem.to_dict())

# Update database history
history = []
conversationHistory.Messages = json.dumps(history)

for historyItem in assistant.chat.history:
history.append(historyItem.to_dict())
updateConversationHistory(conversationHistory)

conversationHistory.Messages = json.dumps(history)
return HTMLResponse("OK")
if event_type == "onConversationRemoved":
received_message = OnMessageReceived(**form_data)
deleteConversationHistory(received_message.ConversationSid)

updateConversationHistory(conversationHistory)
logging.info("Conversation removed")

return HTMLResponse("OK")
return HTMLResponse("OK")

return HTMLResponse("Unknown mode")

0 comments on commit 09aa6bc

Please sign in to comment.