Skip to content

Commit

Permalink
Gracefully close thread when there's an exception in the anthropic ll…
Browse files Browse the repository at this point in the history
…m thread. Include full stack traces.
  • Loading branch information
sabaimran committed Sep 3, 2024
1 parent 1790140 commit 895f1c8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
49 changes: 26 additions & 23 deletions src/khoj/processor/conversation/anthropic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,29 @@ def anthropic_chat_completion_with_backoff(
def anthropic_llm_thread(
g, messages, system_prompt, model_name, temperature, api_key, max_prompt_size=None, model_kwargs=None
):
if api_key not in anthropic_clients:
client: anthropic.Anthropic = anthropic.Anthropic(api_key=api_key)
anthropic_clients[api_key] = client
else:
client: anthropic.Anthropic = anthropic_clients[api_key]

formatted_messages: List[anthropic.types.MessageParam] = [
anthropic.types.MessageParam(role=message.role, content=message.content) for message in messages
]

with client.messages.stream(
messages=formatted_messages,
model=model_name, # type: ignore
temperature=temperature,
system=system_prompt,
timeout=20,
max_tokens=DEFAULT_MAX_TOKENS_ANTHROPIC,
**(model_kwargs or dict()),
) as stream:
for text in stream.text_stream:
g.send(text)

g.close()
try:
if api_key not in anthropic_clients:
client: anthropic.Anthropic = anthropic.Anthropic(api_key=api_key)
anthropic_clients[api_key] = client
else:
client: anthropic.Anthropic = anthropic_clients[api_key]

formatted_messages: List[anthropic.types.MessageParam] = [
anthropic.types.MessageParam(role=message.role, content=message.content) for message in messages
]

with client.messages.stream(
messages=formatted_messages,
model=model_name, # type: ignore
temperature=temperature,
system=system_prompt,
timeout=20,
max_tokens=DEFAULT_MAX_TOKENS_ANTHROPIC,
**(model_kwargs or dict()),
) as stream:
for text in stream.text_stream:
g.send(text)
except Exception as e:
logger.error(f"Error in anthropic_llm_thread: {e}", exc_info=True)
finally:
g.close()
2 changes: 1 addition & 1 deletion src/khoj/processor/conversation/openai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ def llm_thread(g, messages, model_name, temperature, openai_api_key=None, api_ba
elif delta_chunk.content:
g.send(delta_chunk.content)
except Exception as e:
logger.error(f"Error in llm_thread: {e}")
logger.error(f"Error in llm_thread: {e}", exc_info=True)
finally:
g.close()

0 comments on commit 895f1c8

Please sign in to comment.