File size: 1,639 Bytes
478dec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from services.llms.LLM import model_gemini
from typing import AsyncIterable, Optional
from langchain.callbacks import AsyncIteratorCallbackHandler

class LLMAgent:
    def __init__(self, model = model_gemini):
        self.agent_id:Optional[str] = None
        self.session_id:Optional[str] = None
        self.thread_id:Optional[str] = None
        self.agent_name:Optional[str] = None
        self.model = model
        self.callback = AsyncIteratorCallbackHandler()
        self.callbacks = [self.callback]

    async def generate(self, messages: list) -> AsyncIterable[str]:
        """ Generates a response from messages using the model's astream method. 
        Args:
            model (ChatGoogleGenerativeAI): The model to use for generating responses.
            messages (list): A list of messages to send to the model.
            ```python
            from langchain_core.messages import HumanMessage, SystemMessage

            messages = [
                SystemMessage(
                    content="You are a helpful assistant! Your name is Bob."
                ),
                HumanMessage(
                    content="What is your name?"
                )
            ]```
        Yields:
            str: The content of each token generated by the model.
        Raises:
            Exception: If an error occurs during the generation process.
        """

        try:
            async for token in self.model.astream(input=messages, callbacks=self.callbacks):
                print(f"token: {token}")
                yield token.content
        except Exception as e:
            print(f"Caught exception: {e}")