Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,98 +1,35 @@
|
|
1 |
# agent.py
|
2 |
-
|
3 |
import os
|
4 |
-
from
|
5 |
-
|
6 |
-
from google import genai
|
7 |
-
from langchain.tools import tool
|
8 |
-
from langchain.agents import initialize_agent, AgentType
|
9 |
-
from langchain_community.document_loaders import WikipediaLoader
|
10 |
-
from langchain.llms.base import LLM
|
11 |
-
|
12 |
-
# βββ 1) Gemini SDK client βββ
|
13 |
-
GENAI_CLIENT = genai.Client(api_key=os.environ.get("GEMINI_API_KEY", ""))
|
14 |
-
GEMINI_MODEL = "gemini-1.5-pro" # or your trial model
|
15 |
-
|
16 |
-
# βββ 2) LLM wrapper with classβlevel defaults βββ
|
17 |
-
class GeminiLLM(LLM):
|
18 |
-
"""
|
19 |
-
A LangChain-compatible LLM wrapper around Google Gemini.
|
20 |
-
"""
|
21 |
-
client: genai.Client = GENAI_CLIENT
|
22 |
-
model: str = GEMINI_MODEL
|
23 |
-
|
24 |
-
@property
|
25 |
-
def _llm_type(self) -> str:
|
26 |
-
return "gemini"
|
27 |
-
|
28 |
-
@property
|
29 |
-
def _identifying_params(self) -> Mapping[str, Any]:
|
30 |
-
return {"model": self.model}
|
31 |
-
|
32 |
-
def _call(
|
33 |
-
self,
|
34 |
-
prompt: str,
|
35 |
-
stop: Optional[List[str]] = None,
|
36 |
-
) -> str:
|
37 |
-
response = self.client.generate_content(
|
38 |
-
model=self.model,
|
39 |
-
contents=[prompt]
|
40 |
-
)
|
41 |
-
return response.text
|
42 |
|
43 |
-
#
|
44 |
|
45 |
@tool
|
46 |
def calculator(expr: str) -> str:
|
47 |
-
"""
|
48 |
-
Safely evaluates a math expression and returns the result.
|
49 |
-
"""
|
50 |
try:
|
51 |
-
return str(eval(expr
|
52 |
-
except Exception
|
53 |
-
return
|
54 |
-
|
55 |
-
@tool
|
56 |
-
def
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
# Build the agent
|
77 |
-
self.agent = initialize_agent(
|
78 |
-
tools=[calculator, wiki_search],
|
79 |
-
llm=gemini_llm,
|
80 |
-
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
81 |
-
verbose=True,
|
82 |
-
max_iterations=5,
|
83 |
-
early_stopping_method="generate",
|
84 |
-
)
|
85 |
-
|
86 |
-
def __call__(self, question: str) -> str:
|
87 |
-
# Build the prompt
|
88 |
-
prompt = (
|
89 |
-
"You have access to two tools:\n"
|
90 |
-
" β’ calculator(expr)\n"
|
91 |
-
" β’ wiki_search(query)\n"
|
92 |
-
"Think internally; output ONLY the final answer.\n\n"
|
93 |
-
f"Question: {question}"
|
94 |
-
)
|
95 |
-
# Run & extract
|
96 |
-
raw = self.agent.run(prompt)
|
97 |
-
lines = [l.strip() for l in raw.splitlines() if l.strip()]
|
98 |
-
return lines[-1] if lines else raw.strip()
|
|
|
1 |
# agent.py
|
|
|
2 |
import os
|
3 |
+
from langgraph import Graph, LLM, tool #or other graph library
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# 1) Define tools
|
6 |
|
7 |
@tool
|
8 |
def calculator(expr: str) -> str:
|
9 |
+
"""Simple math via Python eval"""
|
|
|
|
|
10 |
try:
|
11 |
+
return str(eval(expr))
|
12 |
+
except Exception:
|
13 |
+
return "Error"
|
14 |
+
# e.g. search, vector_retrieval, etc.
|
15 |
+
# @tool
|
16 |
+
# def web_search(query:str) -> str:
|
17 |
+
# ...
|
18 |
+
|
19 |
+
# 2) Build your graph
|
20 |
+
def build_graph(provider: str = "huggingface") -> Graph:
|
21 |
+
# 2a) Instantiate your LLM endpoint
|
22 |
+
api_token = os.environ["HF_API_TOKEN"]
|
23 |
+
llm = LLM(provider=provider, token=api_token)
|
24 |
+
|
25 |
+
# 2b) Attach tools
|
26 |
+
llm = llm.with_tools([calculator]) # add more tools here
|
27 |
+
|
28 |
+
# 2c) Compose your graph
|
29 |
+
graph = Graph()
|
30 |
+
graph.add_node("ask", llm) # prompt node
|
31 |
+
graph.add_node("calc", calculator) # tool node
|
32 |
+
graph.add_edge("ask", "calc") # allow ask -> calc
|
33 |
+
graph.set.start("ask")
|
34 |
+
return graph
|
35 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|