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

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +19 -20
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
- # β€”β€”β€” 1) Gemini Client Setup β€”β€”β€”
10
  from google import genai
11
 
12
- # Initialize once at import time
13
  GENAI_CLIENT = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
14
- GEMINI_MODEL = "gemini-1.5-pro" # or "gemini-1.0", "gemini-2.0-flash", etc.
15
 
16
  def gemini_generate(prompt: str) -> str:
17
- """Call Google Gemini via the GenAI SDK."""
18
- response = GENAI_CLIENT.generate_content(
19
  model=GEMINI_MODEL,
20
  contents=[prompt]
21
  )
22
- return response.text
23
 
24
- # β€”β€”β€” 2) Tools β€”β€”β€”
25
 
26
  @tool
27
  def calculator(expr: str) -> str:
28
- """
29
- Safely evaluates a math expression and returns the result.
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 and concatenates their text.
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
- # β€”β€”β€” 3) Agent Definition β€”β€”β€”
45
 
46
  class BasicAgent:
47
  def __init__(self):
48
- # We’re not using Hugging Face anymoreβ€”Gemini handles LLM calls
49
  self.agent = initialize_agent(
50
  [calculator, wiki_search],
51
- # Wrap our gemini_generate as an LLM
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
- result = self.agent.run(prompt)
69
- # Strip off anything but the last line
70
- return result.splitlines()[-1].strip()
 
 
 
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()