Skip to content

Commit

Permalink
add error handling for vector db
Browse files Browse the repository at this point in the history
  • Loading branch information
Kian1354 committed Aug 17, 2023
1 parent 099c373 commit ddd5cce
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions vocode/streaming/agent/chat_gpt_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,32 @@ async def generate_response(
return
assert self.transcript is not None

chat_parameters = {}
if self.agent_config.vector_db_config:
docs_with_scores = await self.vector_db.similarity_search_with_score(
self.transcript.get_last_user_message()[1]
)
docs_with_scores_str = "\n\n".join(
[
"Document: "
+ doc[0].metadata["source"]
+ f" (Confidence: {doc[1]})\n"
+ doc[0].lc_kwargs["page_content"].replace(r"\n", "\n")
for doc in docs_with_scores
]
)
vector_db_result = f"Found {len(docs_with_scores)} similar documents:\n{docs_with_scores_str}"
messages = format_openai_chat_messages_from_transcript(
self.transcript, self.agent_config.prompt_preamble
)
messages.insert(
-1, vector_db_result_to_openai_chat_message(vector_db_result)
)
chat_parameters = self.get_chat_parameters(messages)
try:
docs_with_scores = await self.vector_db.similarity_search_with_score(
self.transcript.get_last_user_message()[1]
)
docs_with_scores_str = "\n\n".join(
[
"Document: "
+ doc[0].metadata["source"]
+ f" (Confidence: {doc[1]})\n"
+ doc[0].lc_kwargs["page_content"].replace(r"\n", "\n")
for doc in docs_with_scores
]
)
vector_db_result = f"Found {len(docs_with_scores)} similar documents:\n{docs_with_scores_str}"
messages = format_openai_chat_messages_from_transcript(
self.transcript, self.agent_config.prompt_preamble
)
messages.insert(
-1, vector_db_result_to_openai_chat_message(vector_db_result)
)
chat_parameters = self.get_chat_parameters(messages)
except Exception as e:
self.logger.error(f"Error while hitting vector db: {e}", exc_info=True)
chat_parameters = self.get_chat_parameters()
else:
chat_parameters = self.get_chat_parameters()
chat_parameters["stream"] = True
Expand Down

0 comments on commit ddd5cce

Please sign in to comment.