abtsousa commited on
Commit
7cdcb1a
·
1 Parent(s): 8c25962

Implement OracleBot class and update agent configuration

Browse files
Files changed (4) hide show
  1. Dockerfile +16 -0
  2. agent/__init__.py +2 -2
  3. agent/agent.py +45 -27
  4. agent/config.py +4 -3
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.13-slim-bookworm AS base
2
+ FROM base AS builder
3
+ COPY --from=ghcr.io/astral-sh/uv:0.7.13 /uv /bin/uv
4
+ ENV UV_COMPILE_BYTECODE=1 UV_LINK_MODE=copy
5
+ WORKDIR /app
6
+ COPY uv.lock pyproject.toml /app/
7
+ RUN --mount=type=cache,target=/root/.cache/uv \
8
+ uv sync --frozen --no-install-project --no-dev
9
+ COPY . /app
10
+ RUN --mount=type=cache,target=/root/.cache/uv \
11
+ uv sync --frozen --no-dev
12
+ FROM base
13
+ COPY --from=builder /app /app
14
+ ENV PATH="/app/.venv/bin:$PATH"
15
+ WORKDIR /app
16
+ CMD ["python", "/app/main.py"]
agent/__init__.py CHANGED
@@ -1,11 +1,11 @@
1
- from .agent import get_agent
2
  from .config import create_agent_config
3
  from .prompts import (
4
  DEFAULT_PROMPT
5
  )
6
 
7
  __all__ = [
8
- "get_agent",
9
  "create_agent_config",
10
  "DEFAULT_PROMPT"
11
  ]
 
1
+ from .agent import OracleBot
2
  from .config import create_agent_config
3
  from .prompts import (
4
  DEFAULT_PROMPT
5
  )
6
 
7
  __all__ = [
8
+ "OracleBot",
9
  "create_agent_config",
10
  "DEFAULT_PROMPT"
11
  ]
agent/agent.py CHANGED
@@ -3,46 +3,64 @@ from typing_extensions import TypedDict
3
  from langgraph.graph import StateGraph, START, END
4
  from langgraph.prebuilt import tools_condition
5
  from agent.nodes import call_model, tool_node
6
- from agent.state import State
7
  from langchain_core.messages import AIMessage, HumanMessage
 
 
8
 
 
 
 
 
 
 
 
9
 
10
- def get_agent():
11
- """
12
- Get our LangGraph agent with the given model and tools.
13
- """
14
-
15
- class GraphConfig(TypedDict):
16
- app_name: str
17
 
18
- graph = StateGraph(State, context_schema=GraphConfig)
 
 
 
19
 
20
- # Add nodes
21
- graph.add_node("agent", call_model)
22
- graph.add_node("tools", tool_node)
23
-
24
- graph.add_edge(START, "agent")
25
- graph.add_conditional_edges("agent", tools_condition)
26
- graph.add_edge("tools", "agent")
27
-
28
- return graph.compile()
 
 
 
 
 
 
 
 
 
 
29
 
30
  # test
31
  if __name__ == "__main__":
32
  import os
33
 
34
  question = "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia."
35
- messages = [HumanMessage(content=question)]
36
 
37
  try:
38
- graph = get_agent()
39
- from agent.config import create_agent_config
40
- config = create_agent_config("OracleBot")
41
- from app import start_phoenix
42
- result = graph.invoke({"messages": messages}, config)
43
- print("\n=== Agent Response ===")
44
- for m in result["messages"]:
45
- m.pretty_print()
46
  except Exception as e:
47
  print(f"Error running agent: {e}")
48
  print("Make sure your API key is correct and the service is accessible.")
 
3
  from langgraph.graph import StateGraph, START, END
4
  from langgraph.prebuilt import tools_condition
5
  from agent.nodes import call_model, tool_node
6
+ from langgraph.graph import MessagesState
7
  from langchain_core.messages import AIMessage, HumanMessage
8
+ from langgraph.checkpoint.memory import InMemorySaver
9
+ from agent.config import create_agent_config
10
 
11
+ class OracleBot:
12
+ def __init__(self):
13
+ print("Initializing OracleBot")
14
+ self.name = "OracleBot"
15
+ self.thread_id = 1 #TODO fix
16
+ self.config = create_agent_config(self.name, self.thread_id)
17
+ self.graph = self._build_agent(self.name)
18
 
19
+ def answer_question(self, question: str):
20
+ """
21
+ Answer a question using the LangGraph agent.
22
+ """
23
+ messages = [HumanMessage(content=question)]
24
+ for mode, chunk in self.graph.stream({"messages": messages}, config=self.config, stream_mode=["messages", "custom"]): # type: ignore
25
+ print(chunk)
26
 
27
+ def _build_agent(self, name: str):
28
+ """
29
+ Get our LangGraph agent with the given model and tools.
30
+ """
31
 
32
+ class GraphConfig(TypedDict):
33
+ name: str;
34
+ thread_id: int;
35
+
36
+ graph = StateGraph(state_schema=MessagesState, context_schema=GraphConfig)
37
+
38
+ # Add nodes
39
+ graph.add_node("agent", call_model)
40
+ graph.add_node("tools", tool_node)
41
+
42
+ # Add edges
43
+ graph.add_edge(START, "agent")
44
+ graph.add_conditional_edges("agent", tools_condition)
45
+ graph.add_edge("tools", "agent")
46
+
47
+ # Add memory
48
+ memory = InMemorySaver()
49
+
50
+ return graph.compile(checkpointer=memory)
51
 
52
  # test
53
  if __name__ == "__main__":
54
  import os
55
 
56
  question = "How many studio albums were published by Mercedes Sosa between 2000 and 2009 (included)? You can use the latest 2022 version of english wikipedia."
 
57
 
58
  try:
59
+ from logger.phoenix import start_phoenix
60
+ start_phoenix()
61
+ bot = OracleBot()
62
+ bot.answer_question(question)
63
+
 
 
 
64
  except Exception as e:
65
  print(f"Error running agent: {e}")
66
  print("Make sure your API key is correct and the service is accessible.")
agent/config.py CHANGED
@@ -1,6 +1,6 @@
1
  from typing import Literal
2
 
3
- def create_agent_config(app_name: str) -> dict:
4
  """
5
  Create configuration for the agent.
6
 
@@ -12,6 +12,7 @@ def create_agent_config(app_name: str) -> dict:
12
  """
13
  return {
14
  "configurable": {
15
- "app_name": app_name
16
- }
 
17
  }
 
1
  from typing import Literal
2
 
3
+ def create_agent_config(app_name: str, thread_id: int) -> dict:
4
  """
5
  Create configuration for the agent.
6
 
 
12
  """
13
  return {
14
  "configurable": {
15
+ "app_name": app_name,
16
+ "thread_id": thread_id,
17
+ },
18
  }