Spaces:
Runtime error
Runtime error
abtsousa
commited on
Commit
·
7cdcb1a
1
Parent(s):
8c25962
Implement OracleBot class and update agent configuration
Browse files- Dockerfile +16 -0
- agent/__init__.py +2 -2
- agent/agent.py +45 -27
- 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
|
| 2 |
from .config import create_agent_config
|
| 3 |
from .prompts import (
|
| 4 |
DEFAULT_PROMPT
|
| 5 |
)
|
| 6 |
|
| 7 |
__all__ = [
|
| 8 |
-
"
|
| 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
|
| 7 |
from langchain_core.messages import AIMessage, HumanMessage
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 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 |
}
|