AC-Angelo93 commited on
Commit
4f8cd09
Β·
verified Β·
1 Parent(s): f5c4901

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +67 -27
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
- # β€”β€”β€” 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__": {}}))
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 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,
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
- "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()
 
 
 
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()