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 authored and RezaRahemtola committed Feb 16, 2025
1 parent 270dff3 commit 64aa138
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 @@ -160,6 +161,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()

if stream:
return StreamingResponse(
self.__dump_api_generate_streamed_answer(
Expand All @@ -170,7 +174,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 @@ -185,8 +189,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()

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 64aa138

Please sign in to comment.