Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,32 +1,30 @@
|
|
1 |
# agent.py
|
2 |
|
3 |
import os
|
4 |
-
import requests
|
5 |
from langchain.tools import tool
|
6 |
from langchain.agents import initialize_agent, AgentType
|
7 |
from langchain_community.document_loaders import WikipediaLoader
|
8 |
|
9 |
-
# βββ
|
10 |
from google import genai
|
11 |
|
12 |
-
#
|
13 |
GENAI_CLIENT = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
14 |
-
GEMINI_MODEL = "gemini-1.5-pro"
|
15 |
|
16 |
def gemini_generate(prompt: str) -> str:
|
17 |
-
|
18 |
-
response = GENAI_CLIENT.generate_content(
|
19 |
model=GEMINI_MODEL,
|
20 |
contents=[prompt]
|
21 |
)
|
22 |
-
return
|
23 |
|
24 |
-
# βββ
|
25 |
|
26 |
@tool
|
27 |
def calculator(expr: str) -> str:
|
28 |
-
|
29 |
-
Safely evaluates a math expression
|
30 |
"""
|
31 |
try:
|
32 |
return str(eval(expr, {"__builtins__": {}}))
|
@@ -35,20 +33,20 @@ def calculator(expr: str) -> str:
|
|
35 |
|
36 |
@tool
|
37 |
def wiki_search(query: str) -> str:
|
38 |
-
|
39 |
-
Fetches up to 2 Wikipedia pages for the query
|
40 |
"""
|
41 |
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
42 |
return "\n\n".join(d.page_content for d in docs)
|
43 |
|
44 |
-
# βββ
|
45 |
|
46 |
class BasicAgent:
|
47 |
def __init__(self):
|
48 |
-
#
|
49 |
self.agent = initialize_agent(
|
50 |
[calculator, wiki_search],
|
51 |
-
# Wrap
|
52 |
lambda prompt: gemini_generate(prompt),
|
53 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
54 |
verbose=True,
|
@@ -57,14 +55,15 @@ class BasicAgent:
|
|
57 |
)
|
58 |
|
59 |
def __call__(self, question: str) -> str:
|
60 |
-
# Prepend your toy examples or system prompt if you like
|
61 |
prompt = (
|
62 |
-
"You have two tools:\n"
|
63 |
" β’ calculator(expr)\n"
|
64 |
" β’ wiki_search(query)\n"
|
65 |
"Use them internally, then OUTPUT ONLY the final answer.\n\n"
|
66 |
f"Question: {question}"
|
67 |
)
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
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 |
+
# βββ Import & initialize Gemini client βββ
|
9 |
from google import genai
|
10 |
|
11 |
+
# Make sure GEMINI_API_KEY is set in your Secrets
|
12 |
GENAI_CLIENT = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
13 |
+
GEMINI_MODEL = "gemini-1.5-pro" # <-- Use your trial model here
|
14 |
|
15 |
def gemini_generate(prompt: str) -> str:
|
16 |
+
resp = GENAI_CLIENT.generate_content(
|
|
|
17 |
model=GEMINI_MODEL,
|
18 |
contents=[prompt]
|
19 |
)
|
20 |
+
return resp.text
|
21 |
|
22 |
+
# βββ Tools βββ
|
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__": {}}))
|
|
|
33 |
|
34 |
@tool
|
35 |
def wiki_search(query: str) -> str:
|
36 |
+
"""
|
37 |
+
Fetches up to 2 Wikipedia pages for the query.
|
38 |
"""
|
39 |
docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
40 |
return "\n\n".join(d.page_content for d in docs)
|
41 |
|
42 |
+
# βββ Agent βββ
|
43 |
|
44 |
class BasicAgent:
|
45 |
def __init__(self):
|
46 |
+
# Zero-Shot React agent with our two tools
|
47 |
self.agent = initialize_agent(
|
48 |
[calculator, wiki_search],
|
49 |
+
# Wrap the gemini_generate function as the LLM
|
50 |
lambda prompt: gemini_generate(prompt),
|
51 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
52 |
verbose=True,
|
|
|
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 |
"Use them internally, then OUTPUT ONLY the final answer.\n\n"
|
63 |
f"Question: {question}"
|
64 |
)
|
65 |
+
|
66 |
+
raw = self.agent.run(prompt)
|
67 |
+
# Return only the final line
|
68 |
+
lines = [l for l in raw.splitlines() if l.strip()]
|
69 |
+
return lines[-1].strip()
|