Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,32 +1,57 @@
|
|
1 |
# agent.py
|
2 |
|
3 |
import os
|
|
|
|
|
|
|
4 |
from langchain.tools import tool
|
5 |
from langchain.agents import initialize_agent, AgentType
|
6 |
from langchain_community.document_loaders import WikipediaLoader
|
|
|
7 |
|
8 |
-
# βββ
|
9 |
-
from google import genai
|
10 |
|
11 |
-
#
|
12 |
GENAI_CLIENT = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
13 |
-
GEMINI_MODEL = "gemini-1.5-pro" #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
model
|
18 |
-
contents=[prompt]
|
19 |
-
)
|
20 |
-
return resp.text
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
@tool
|
25 |
def calculator(expr: str) -> str:
|
26 |
"""
|
27 |
-
Safely evaluates a math expression.
|
28 |
"""
|
29 |
try:
|
|
|
30 |
return str(eval(expr, {"__builtins__": {}}))
|
31 |
except Exception as e:
|
32 |
return f"Error: {e}"
|
@@ -34,36 +59,51 @@ def calculator(expr: str) -> str:
|
|
34 |
@tool
|
35 |
def wiki_search(query: str) -> str:
|
36 |
"""
|
37 |
-
Fetches up to 2 Wikipedia pages
|
38 |
"""
|
39 |
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
40 |
-
return "\n\n".join(
|
41 |
|
42 |
-
# βββ Agent βββ
|
43 |
|
44 |
class BasicAgent:
|
|
|
|
|
|
|
|
|
45 |
def __init__(self):
|
46 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
self.agent = initialize_agent(
|
48 |
-
[calculator, wiki_search],
|
49 |
-
|
50 |
-
lambda prompt: gemini_generate(prompt),
|
51 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
52 |
-
verbose=True,
|
53 |
-
max_iterations=5,
|
54 |
-
early_stopping_method="generate"
|
55 |
)
|
56 |
|
57 |
def __call__(self, question: str) -> str:
|
|
|
|
|
|
|
|
|
58 |
prompt = (
|
59 |
"You have access to two tools:\n"
|
60 |
" β’ calculator(expr)\n"
|
61 |
" β’ wiki_search(query)\n"
|
62 |
-
"
|
63 |
f"Question: {question}"
|
64 |
)
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
1 |
# agent.py
|
2 |
|
3 |
import os
|
4 |
+
from typing import Optional, List, Mapping, Any
|
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 LLM wrapper βββ
|
|
|
13 |
|
14 |
+
# Initialize your Gemini client once
|
15 |
GENAI_CLIENT = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
16 |
+
GEMINI_MODEL = "gemini-1.5-pro" # or "gemini-1.0-turbo" per your trial
|
17 |
+
|
18 |
+
class GeminiLLM(LLM):
|
19 |
+
"""
|
20 |
+
A LangChain-compatible wrapper around Google Gemini via the google-genai SDK.
|
21 |
+
"""
|
22 |
+
def __init__(self, client: genai.Client, model: str):
|
23 |
+
self.client = client
|
24 |
+
self.model = model
|
25 |
+
|
26 |
+
@property
|
27 |
+
def _llm_type(self) -> str:
|
28 |
+
return "gemini"
|
29 |
|
30 |
+
@property
|
31 |
+
def _identifying_params(self) -> Mapping[str, Any]:
|
32 |
+
return {"model": self.model}
|
|
|
|
|
|
|
33 |
|
34 |
+
def _call(
|
35 |
+
self,
|
36 |
+
prompt: str,
|
37 |
+
stop: Optional[List[str]] = None,
|
38 |
+
) -> str:
|
39 |
+
# Call the Gemini SDK
|
40 |
+
response = self.client.generate_content(
|
41 |
+
model=self.model,
|
42 |
+
contents=[prompt]
|
43 |
+
)
|
44 |
+
return response.text
|
45 |
+
|
46 |
+
# βββ 2) Tools definitions βββ
|
47 |
|
48 |
@tool
|
49 |
def calculator(expr: str) -> str:
|
50 |
"""
|
51 |
+
Safely evaluates a math expression and returns the result.
|
52 |
"""
|
53 |
try:
|
54 |
+
# simple sandbox
|
55 |
return str(eval(expr, {"__builtins__": {}}))
|
56 |
except Exception as e:
|
57 |
return f"Error: {e}"
|
|
|
59 |
@tool
|
60 |
def wiki_search(query: str) -> str:
|
61 |
"""
|
62 |
+
Fetches up to 2 Wikipedia pages matching the query and concatenates their text.
|
63 |
"""
|
64 |
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
65 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
66 |
|
67 |
+
# βββ 3) Agent construction βββ
|
68 |
|
69 |
class BasicAgent:
|
70 |
+
"""
|
71 |
+
A Zero-Shot React agent powered by Google Gemini,
|
72 |
+
with access to calculator and wiki_search tools.
|
73 |
+
"""
|
74 |
def __init__(self):
|
75 |
+
# Ensure your secret is set
|
76 |
+
assert "GEMINI_API_KEY" in os.environ, "β GEMINI_API_KEY not found in Secrets"
|
77 |
+
|
78 |
+
# Wrap Gemini as an LLM
|
79 |
+
gemini_llm = GeminiLLM(GENAI_CLIENT, GEMINI_MODEL)
|
80 |
+
|
81 |
+
# Initialize the agent with our tools
|
82 |
self.agent = initialize_agent(
|
83 |
+
tools=[calculator, wiki_search],
|
84 |
+
llm=gemini_llm,
|
|
|
85 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
86 |
+
verbose=True, # turn on logs so you can debug
|
87 |
+
max_iterations=5, # allow up to 5 tool calls
|
88 |
+
early_stopping_method="generate",
|
89 |
)
|
90 |
|
91 |
def __call__(self, question: str) -> str:
|
92 |
+
"""
|
93 |
+
Runs the agent on the question and returns only the final answer line.
|
94 |
+
"""
|
95 |
+
# System prompt + question
|
96 |
prompt = (
|
97 |
"You have access to two tools:\n"
|
98 |
" β’ calculator(expr)\n"
|
99 |
" β’ wiki_search(query)\n"
|
100 |
+
"Think internally; output ONLY the final answer (no chain-of-thought).\n\n"
|
101 |
f"Question: {question}"
|
102 |
)
|
103 |
|
104 |
+
# Run the agent
|
105 |
+
raw_output = self.agent.run(prompt)
|
106 |
+
|
107 |
+
# Extract the last non-empty line as the answer
|
108 |
+
lines = [line.strip() for line in raw_output.splitlines() if line.strip()]
|
109 |
+
return lines[-1] if lines else raw_output.strip()
|