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

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +28 -91
agent.py CHANGED
@@ -1,98 +1,35 @@
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 SDK client β€”β€”β€”
13
- GENAI_CLIENT = genai.Client(api_key=os.environ.get("GEMINI_API_KEY", ""))
14
- GEMINI_MODEL = "gemini-1.5-pro" # or your trial model
15
-
16
- # β€”β€”β€” 2) LLM wrapper with class‐level defaults β€”β€”β€”
17
- class GeminiLLM(LLM):
18
- """
19
- A LangChain-compatible LLM wrapper around Google Gemini.
20
- """
21
- client: genai.Client = GENAI_CLIENT
22
- model: str = GEMINI_MODEL
23
-
24
- @property
25
- def _llm_type(self) -> str:
26
- return "gemini"
27
-
28
- @property
29
- def _identifying_params(self) -> Mapping[str, Any]:
30
- return {"model": self.model}
31
-
32
- def _call(
33
- self,
34
- prompt: str,
35
- stop: Optional[List[str]] = None,
36
- ) -> str:
37
- response = self.client.generate_content(
38
- model=self.model,
39
- contents=[prompt]
40
- )
41
- return response.text
42
 
43
- # β€”β€”β€” 3) Tools β€”β€”β€”
44
 
45
  @tool
46
  def calculator(expr: str) -> str:
47
- """
48
- Safely evaluates a math expression and returns the result.
49
- """
50
  try:
51
- return str(eval(expr, {"__builtins__": {}}))
52
- except Exception as e:
53
- return f"Error: {e}"
54
-
55
- @tool
56
- def wiki_search(query: str) -> str:
57
- """
58
- Loads up to 2 Wikipedia pages for the query and concatenates their text.
59
- """
60
- docs = WikipediaLoader(query=query, load_max_docs=2).load()
61
- return "\n\n".join(d.page_content for d in docs)
62
-
63
- # β€”β€”β€” 4) Agent β€”β€”β€”
64
-
65
- class BasicAgent:
66
- """
67
- Zero-Shot React agent powered by Gemini + two tools.
68
- """
69
- def __init__(self):
70
- # Ensure your key is present
71
- assert os.environ.get("GEMINI_API_KEY"), "❌ GEMINI_API_KEY not set in Secrets"
72
-
73
- # Instantiate our LLM (defaults are on the class)
74
- gemini_llm = GeminiLLM()
75
-
76
- # Build the agent
77
- self.agent = initialize_agent(
78
- tools=[calculator, wiki_search],
79
- llm=gemini_llm,
80
- agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
81
- verbose=True,
82
- max_iterations=5,
83
- early_stopping_method="generate",
84
- )
85
-
86
- def __call__(self, question: str) -> str:
87
- # Build the prompt
88
- prompt = (
89
- "You have access to two tools:\n"
90
- " β€’ calculator(expr)\n"
91
- " β€’ wiki_search(query)\n"
92
- "Think internally; output ONLY the final answer.\n\n"
93
- f"Question: {question}"
94
- )
95
- # Run & extract
96
- raw = self.agent.run(prompt)
97
- lines = [l.strip() for l in raw.splitlines() if l.strip()]
98
- return lines[-1] if lines else raw.strip()
 
1
  # agent.py
 
2
  import os
3
+ from langgraph import Graph, LLM, tool #or other graph library
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # 1) Define tools
6
 
7
  @tool
8
  def calculator(expr: str) -> str:
9
+ """Simple math via Python eval"""
 
 
10
  try:
11
+ return str(eval(expr))
12
+ except Exception:
13
+ return "Error"
14
+ # e.g. search, vector_retrieval, etc.
15
+ # @tool
16
+ # def web_search(query:str) -> str:
17
+ # ...
18
+
19
+ # 2) Build your graph
20
+ def build_graph(provider: str = "huggingface") -> Graph:
21
+ # 2a) Instantiate your LLM endpoint
22
+ api_token = os.environ["HF_API_TOKEN"]
23
+ llm = LLM(provider=provider, token=api_token)
24
+
25
+ # 2b) Attach tools
26
+ llm = llm.with_tools([calculator]) # add more tools here
27
+
28
+ # 2c) Compose your graph
29
+ graph = Graph()
30
+ graph.add_node("ask", llm) # prompt node
31
+ graph.add_node("calc", calculator) # tool node
32
+ graph.add_edge("ask", "calc") # allow ask -> calc
33
+ graph.set.start("ask")
34
+ return graph
35
+