Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
@@ -8,20 +8,26 @@ 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"
|
17 |
|
18 |
class GeminiLLM(LLM):
|
19 |
"""
|
20 |
-
A LangChain-compatible wrapper around Google Gemini via
|
21 |
"""
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
@property
|
27 |
def _llm_type(self) -> str:
|
@@ -36,7 +42,7 @@ class GeminiLLM(LLM):
|
|
36 |
prompt: str,
|
37 |
stop: Optional[List[str]] = None,
|
38 |
) -> str:
|
39 |
-
#
|
40 |
response = self.client.generate_content(
|
41 |
model=self.model,
|
42 |
contents=[prompt]
|
@@ -51,7 +57,6 @@ def calculator(expr: str) -> str:
|
|
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}"
|
@@ -75,16 +80,16 @@ class BasicAgent:
|
|
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, #
|
87 |
-
max_iterations=5,
|
88 |
early_stopping_method="generate",
|
89 |
)
|
90 |
|
@@ -92,18 +97,15 @@ class BasicAgent:
|
|
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
|
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()
|
|
|
8 |
from langchain.agents import initialize_agent, AgentType
|
9 |
from langchain_community.document_loaders import WikipediaLoader
|
10 |
from langchain.llms.base import LLM
|
11 |
+
from pydantic import Field
|
12 |
|
13 |
# βββ 1) Gemini LLM wrapper βββ
|
14 |
|
15 |
# Initialize your Gemini client once
|
16 |
GENAI_CLIENT = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
17 |
+
GEMINI_MODEL = "gemini-1.5-pro" # or whichever you have access to
|
18 |
|
19 |
class GeminiLLM(LLM):
|
20 |
"""
|
21 |
+
A LangChain-compatible wrapper around Google Gemini via google-genai.
|
22 |
"""
|
23 |
+
# Declare as Pydantic fields so they're accepted
|
24 |
+
client: genai.Client = Field(...)
|
25 |
+
model: str = Field(...)
|
26 |
+
|
27 |
+
def __init__(self, client: genai.Client, model: str, **kwargs):
|
28 |
+
super().__init__(**kwargs)
|
29 |
+
object.__setattr__(self, "client", client)
|
30 |
+
object.__setattr__(self, "model", model)
|
31 |
|
32 |
@property
|
33 |
def _llm_type(self) -> str:
|
|
|
42 |
prompt: str,
|
43 |
stop: Optional[List[str]] = None,
|
44 |
) -> str:
|
45 |
+
# Use the google-genai SDK
|
46 |
response = self.client.generate_content(
|
47 |
model=self.model,
|
48 |
contents=[prompt]
|
|
|
57 |
Safely evaluates a math expression and returns the result.
|
58 |
"""
|
59 |
try:
|
|
|
60 |
return str(eval(expr, {"__builtins__": {}}))
|
61 |
except Exception as e:
|
62 |
return f"Error: {e}"
|
|
|
80 |
# Ensure your secret is set
|
81 |
assert "GEMINI_API_KEY" in os.environ, "β GEMINI_API_KEY not found in Secrets"
|
82 |
|
83 |
+
# Wrap Gemini as an LLM instance
|
84 |
+
gemini_llm = GeminiLLM(client=GENAI_CLIENT, model=GEMINI_MODEL)
|
85 |
|
86 |
+
# Initialize the agent with our two tools
|
87 |
self.agent = initialize_agent(
|
88 |
tools=[calculator, wiki_search],
|
89 |
llm=gemini_llm,
|
90 |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
91 |
+
verbose=True, # see tool calls in logs
|
92 |
+
max_iterations=5,
|
93 |
early_stopping_method="generate",
|
94 |
)
|
95 |
|
|
|
97 |
"""
|
98 |
Runs the agent on the question and returns only the final answer line.
|
99 |
"""
|
|
|
100 |
prompt = (
|
101 |
"You have access to two tools:\n"
|
102 |
" β’ calculator(expr)\n"
|
103 |
" β’ wiki_search(query)\n"
|
104 |
+
"Think internally; output ONLY the final answer.\n\n"
|
105 |
f"Question: {question}"
|
106 |
)
|
107 |
|
|
|
108 |
raw_output = self.agent.run(prompt)
|
109 |
+
# Extract the last non-empty line
|
|
|
110 |
lines = [line.strip() for line in raw_output.splitlines() if line.strip()]
|
111 |
return lines[-1] if lines else raw_output.strip()
|