Skip to content

Commit

Permalink
add a default session that gets reused for direct calls
Browse files Browse the repository at this point in the history
  • Loading branch information
moshemalawach committed Jan 9, 2025
1 parent eebad33 commit b86fd33
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions libertai_agents/libertai_agents/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def __init__(
self.system_prompt = system_prompt
self.tools = tools
self.llamacpp_params = llamacpp_params
self.call_session = None

if expose_api:
# Define API routes
Expand Down Expand Up @@ -159,6 +160,9 @@ async def __api_generate_answer(
Generate an answer based on an existing conversation.
The response messages can be streamed or sent in a single block.
"""
if self.call_session is None:
self.call_session = ClientSession()

Check failure on line 164 in libertai_agents/libertai_agents/agents.py

View workflow job for this annotation

GitHub Actions / Package: mypy

[mypy] reported by reviewdog 🐶 Incompatible types in assignment (expression has type "ClientSession", variable has type "None") [assignment] Raw Output: /home/runner/work/libertai-agents/libertai-agents/libertai_agents/libertai_agents/agents.py:164:33: error: Incompatible types in assignment (expression has type "ClientSession", variable has type "None") [assignment]

if stream:
return StreamingResponse(
self.__dump_api_generate_streamed_answer(
Expand All @@ -169,7 +173,7 @@ async def __api_generate_answer(

response_messages: list[Message] = []
async for message in self.generate_answer(
messages, only_final_answer=only_final_answer
messages, only_final_answer=only_final_answer, session=self.call_session
):
response_messages.append(message)
return response_messages
Expand All @@ -184,8 +188,11 @@ async def __dump_api_generate_streamed_answer(
:param only_final_answer: Param to pass to generate_answer
:return: Iterable of each messages from generate_answer dumped to JSON
"""
if self.call_session is None:
self.call_session = ClientSession()

Check failure on line 192 in libertai_agents/libertai_agents/agents.py

View workflow job for this annotation

GitHub Actions / Package: mypy

[mypy] reported by reviewdog 🐶 Incompatible types in assignment (expression has type "ClientSession", variable has type "None") [assignment] Raw Output: /home/runner/work/libertai-agents/libertai-agents/libertai_agents/libertai_agents/agents.py:192:33: error: Incompatible types in assignment (expression has type "ClientSession", variable has type "None") [assignment]

async for message in self.generate_answer(
messages, only_final_answer=only_final_answer
messages, only_final_answer=only_final_answer, session=self.call_session
):
yield json.dumps(message.model_dump(), indent=4)

Expand Down

0 comments on commit b86fd33

Please sign in to comment.