ninooo96 commited on
Commit
80ea88d
·
1 Parent(s): 81917a3

first tools implementation with langgraph

Browse files
Files changed (3) hide show
  1. geminiAgent.py +22 -0
  2. requirements.txt +3 -1
  3. tools/WebSearchTool.py +75 -0
geminiAgent.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import TypedDict, Literal, List, Dict, Optional
2
+ import time
3
+ import requests
4
+ from bs4 import BeautifulSoup
5
+ import operator
6
+ from langchain_core.prompts import ChatPromptTemplate
7
+ from langchain_core.runnables import RunnablePassthrough
8
+ from langchain_core.messages import HumanMessage, AIMessage
9
+ from langchain.tools import tool
10
+ from langchain_core.output_parsers import StrOutputParser
11
+ from langchain_core.pydantic_v1 import BaseModel, Field
12
+ from langgraph.graph import StateGraph, START, END
13
+ import pytesseract
14
+ from PIL import Image
15
+
16
+ @tool
17
+ def ocr_tool(image: str) -> str:
18
+ """
19
+ A tool that performs OCR processing on an image.
20
+ """
21
+ image = Image.open(image)
22
+ return pytesseract.image_to_string(image)
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
  gradio
2
- requests
 
 
 
1
  gradio
2
+ requests
3
+ pytesseract
4
+ PIL
tools/WebSearchTool.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from typing import Optional
3
+ import requests
4
+ from bs4 import BeautifulSoup
5
+ from langchain.tools import tool
6
+
7
+ class WebSearchTool:
8
+ def __init__(self):
9
+ self.last_request_time = 0
10
+ self.min_request_interval = 2.0 # Minimum time between requests in seconds
11
+ self.max_retries = 10
12
+
13
+ def search(self, query: str, domain: Optional[str] = None) -> str:
14
+ """Perform web search with rate limiting and retries."""
15
+ for attempt in range(self.max_retries):
16
+ # Implement rate limiting
17
+ current_time = time.time()
18
+ time_since_last = current_time - self.last_request_time
19
+ if time_since_last < self.min_request_interval:
20
+ time.sleep(self.min_request_interval - time_since_last)
21
+
22
+ try:
23
+ # Make the search request
24
+ results = self._do_search(query, domain)
25
+ self.last_request_time = time.time()
26
+ return results
27
+ except Exception as e:
28
+ if "202 Ratelimit" in str(e):
29
+ if attempt < self.max_retries - 1:
30
+ # Exponential backoff
31
+ wait_time = (2 ** attempt) * self.min_request_interval
32
+ time.sleep(wait_time)
33
+ continue
34
+ return f"Search failed after {self.max_retries} attempts: {str(e)}"
35
+
36
+ return "Search failed due to rate limiting"
37
+
38
+ def _do_search(self, query: str, domain: Optional[str] = None) -> str:
39
+ """Perform the actual search request."""
40
+ try:
41
+ # Construct search URL
42
+ base_url = "https://html.duckduckgo.com/html"
43
+ params = {"q": query}
44
+ if domain:
45
+ params["q"] += f" site:{domain}"
46
+
47
+ # Make request with increased timeout
48
+ response = requests.get(base_url, params=params, timeout=10)
49
+ response.raise_for_status()
50
+
51
+ if response.status_code == 202:
52
+ raise Exception("202 Ratelimit")
53
+
54
+ # Extract search results
55
+ results = []
56
+ soup = BeautifulSoup(response.text, 'html.parser')
57
+ for result in soup.find_all('div', {'class': 'result'}):
58
+ title = result.find('a', {'class': 'result__a'})
59
+ snippet = result.find('a', {'class': 'result__snippet'})
60
+ if title and snippet:
61
+ results.append({
62
+ 'title': title.get_text(),
63
+ 'snippet': snippet.get_text(),
64
+ 'url': title.get('href')
65
+ })
66
+
67
+ # Format results
68
+ formatted_results = []
69
+ for r in results[:10]: # Limit to top 5 results
70
+ formatted_results.append(f"[{r['title']}]({r['url']})\n{r['snippet']}\n")
71
+
72
+ return "## Search Results\n\n" + "\n".join(formatted_results)
73
+
74
+ except requests.RequestException as e:
75
+ raise Exception(f"Search request failed: {str(e)}")