Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Karmazin committed Jun 6, 2024
1 parent 04b92d0 commit cf8ab85
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions just_agents/llm_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,26 @@ def instruct(self, prompt: str):
self.memory.add_message(system_instruction, True)
return system_instruction

def query(self, prompt: str = None, stream: bool = False, run_callbacks: bool = True, output: Optional[Path] = None) -> str:
def query(self, prompt: str, stream: bool = False, run_callbacks: bool = True, output: Optional[Path] = None) -> str:
"""
Query large language model
:param prompt:
:param stream:
:param run_callbacks:
:return:
"""
if prompt is not None:
question = Message(role="user", content=prompt)
self.memory.add_message(question)

question = Message(role="user", content=prompt)
self.memory.add_message(question, run_callbacks)
return self._query(stream, run_callbacks, output)


def query_all(self, messages: list, stream: bool = False, run_callbacks: bool = True, output: Optional[Path] = None) -> str:
self.memory.add_messages(messages, run_callbacks)
return self._query(stream, run_callbacks, output)


def _query(self, stream: bool = False, run_callbacks: bool = True, output: Optional[Path] = None) -> str:
options: Dict = self.llm_options
response: ModelResponse = completion(messages=self.memory.messages, stream=stream, **options)
self._process_response(response)
Expand All @@ -71,7 +80,8 @@ def query(self, prompt: str = None, stream: bool = False, run_callbacks: bool =
self._process_response(response)
answer = self.message_from_response(response)
self.memory.add_message(answer, run_callbacks)
result: str = self.memory.last_message.content if self.memory.last_message is not None and self.memory.last_message.content is not None else str(self.memory.last_message)
result: str = self.memory.last_message.content if self.memory.last_message is not None and self.memory.last_message.content is not None else str(
self.memory.last_message)
if output is not None:
output.write_text(result)
return result
Expand Down

0 comments on commit cf8ab85

Please sign in to comment.