summaryrefslogtreecommitdiff
path: root/agent.py
blob: 51661fdcd5f5ef53d3839b948d272d4beb7ec841 (plain)
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
43
44
45
from collections.abc import Iterator

from context import ContextBuilder
from identity import AgentIdentity, UserProfile
from llm import LLM

class Agent:
    def __init__(
        self,
        llm: LLM,
        agent_identity: AgentIdentity,
        user_profile: UserProfile
    ) -> None:
        self.llm = llm

        self.context = ContextBuilder(
            agent_identity,
            user_profile,
        )

        self.messages: list[dict[str, str]] = [
            {
                "role": "system",
                "content": self.context.build_system_prompt(),
            }
        ]

    def chat(self, user_input: str) -> Iterator[str]:
        self.messages.append({
            "role": "user",
            "content": user_input,
        })

        answer_parts: list[str] = []

        for chunk in self.llm.chat(self.messages):
            yield chunk
            answer_parts.append(chunk)

        answer = "".join(answer_parts)

        self.messages.append({
            "role": "assistant",
            "content": answer,
        })