|
import os |
|
from typing import List, Dict, Any, Optional |
|
import base64 |
|
import tempfile |
|
from langchain_core.messages import HumanMessage, SystemMessage |
|
from langchain_openai import ChatOpenAI |
|
from langchain_community.tools import DuckDuckGoSearchResults |
|
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper |
|
from langchain_google_genai import ChatGoogleGenerativeAI |
|
import wikipediaapi |
|
import json |
|
from urllib.parse import urlparse |
|
import pytesseract |
|
from PIL import Image, ImageDraw, ImageFont, ImageEnhance, ImageFilter |
|
import cmath |
|
from langchain_core.tools import tool |
|
from langgraph.graph import START, StateGraph, MessagesState |
|
from langgraph.prebuilt import tools_condition |
|
from langgraph.prebuilt import ToolNode |
|
from langchain_tavily import TavilySearch |
|
|
|
import requests |
|
|
|
system_prompt = """You are a helpful assistant tasked with answering questions using a set of tools. |
|
Now, I will ask you a question. Report your thoughts, and finish your answer with the following template: |
|
FINAL ANSWER: [YOUR FINAL ANSWER]. |
|
YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string. |
|
Your answer should only start with "FINAL ANSWER: ", then follows with the answer. |
|
""" |
|
|
|
|
|
api_key = os.getenv("GEMINI_API_KEY") |
|
|
|
|
|
model = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0, api_key=api_key) |
|
|
|
@tool |
|
def search_wiki(query: str, max_results: int = 2) -> str: |
|
""" |
|
Searches Wikipedia for the given query and returns a maximum of 'max_results' |
|
relevant article summaries, titles, and URLs. |
|
|
|
Args: |
|
query (str): The search query for Wikipedia. |
|
max_results (int): The maximum number of search results to retrieve (default is 3). |
|
|
|
Returns: |
|
str: A JSON string containing a list of dictionaries, where each dictionary |
|
represents a Wikipedia article with its title, summary, and URL. |
|
Returns an empty list if no results are found or an error occurs. |
|
""" |
|
|
|
language_code = 'en' |
|
|
|
headers={'User-Agent': 'LangGraphAgent/1.0 ([email protected])'} |
|
|
|
base_url = 'https://api.wikimedia.org/core/v1/wikipedia/' |
|
endpoint = '/search/page' |
|
url = base_url + language_code + endpoint |
|
parameters = {'q': query, 'limit': max_results} |
|
response = requests.get(url, headers=headers, params=parameters) |
|
response = json.loads(response.text) |
|
return json.dumps(response, indent=2) |
|
|
|
|
|
tavily_search_tool = TavilySearch( |
|
max_results=5, |
|
topic="general", |
|
) |
|
|
|
@tool |
|
def save_and_read_file(content: str, filename: Optional[str] = None) -> str: |
|
""" |
|
Save content to a file and return the path. |
|
Args: |
|
content (str): the content to save to the file |
|
filename (str, optional): the name of the file. If not provided, a random name file will be created. |
|
""" |
|
temp_dir = tempfile.gettempdir() |
|
if filename is None: |
|
temp_file = tempfile.NamedTemporaryFile(delete=False, dir=temp_dir) |
|
filepath = temp_file.name |
|
else: |
|
filepath = os.path.join(temp_dir, filename) |
|
|
|
with open(filepath, "w") as f: |
|
f.write(content) |
|
|
|
return f"File saved to {filepath}. You can read this file to process its contents." |
|
|
|
@tool |
|
def download_file_from_url(url: str, filename: Optional[str] = None) -> str: |
|
""" |
|
Download a file from a URL and save it to a temporary location. |
|
Args: |
|
url (str): the URL of the file to download. |
|
filename (str, optional): the name of the file. If not provided, a random name file will be created. |
|
""" |
|
try: |
|
|
|
if not filename: |
|
path = urlparse(url).path |
|
filename = os.path.basename(path) |
|
if not filename: |
|
filename = f"downloaded_{uuid.uuid4().hex[:8]}" |
|
|
|
|
|
temp_dir = tempfile.gettempdir() |
|
filepath = os.path.join(temp_dir, filename) |
|
|
|
|
|
response = requests.get(url, stream=True) |
|
response.raise_for_status() |
|
|
|
|
|
with open(filepath, "wb") as f: |
|
for chunk in response.iter_content(chunk_size=8192): |
|
f.write(chunk) |
|
|
|
return f"File downloaded to {filepath}. You can read this file to process its contents." |
|
except Exception as e: |
|
return f"Error downloading file: {str(e)}" |
|
|
|
@tool |
|
def sum(a: int, b:int) -> int: |
|
"""Sum up two numbers. |
|
Args: |
|
a: first int |
|
b: second int |
|
""" |
|
return a + b |
|
|
|
@tool |
|
def extract_text_from_image(image_path: str) -> str: |
|
""" |
|
Extract text from an image using OCR library pytesseract (if available). |
|
Args: |
|
image_path (str): the path to the image file. |
|
""" |
|
try: |
|
|
|
image = Image.open(image_path) |
|
|
|
|
|
text = pytesseract.image_to_string(image) |
|
|
|
return f"Extracted text from image:\n\n{text}" |
|
except Exception as e: |
|
return f"Error extracting text from image: {str(e)}" |
|
|
|
|
|
@tool |
|
def analyze_csv_file(file_path: str, query: str) -> str: |
|
""" |
|
Analyze a CSV file using pandas and answer a question about it. |
|
Args: |
|
file_path (str): the path to the CSV file. |
|
query (str): Question about the data |
|
""" |
|
try: |
|
|
|
df = pd.read_csv(file_path) |
|
|
|
|
|
result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n" |
|
result += f"Columns: {', '.join(df.columns)}\n\n" |
|
|
|
|
|
result += "Summary statistics:\n" |
|
result += str(df.describe()) |
|
|
|
return result |
|
|
|
except Exception as e: |
|
return f"Error analyzing CSV file: {str(e)}" |
|
|
|
|
|
@tool |
|
def analyze_excel_file(file_path: str, query: str) -> str: |
|
""" |
|
Analyze an Excel file using pandas and answer a question about it. |
|
Args: |
|
file_path (str): the path to the Excel file. |
|
query (str): Question about the data |
|
""" |
|
try: |
|
|
|
df = pd.read_excel(file_path) |
|
|
|
|
|
result = ( |
|
f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n" |
|
) |
|
result += f"Columns: {', '.join(df.columns)}\n\n" |
|
|
|
|
|
result += "Summary statistics:\n" |
|
result += str(df.describe()) |
|
|
|
return result |
|
|
|
except Exception as e: |
|
return f"Error analyzing Excel file: {str(e)}" |
|
|
|
|
|
@tool |
|
def analyze_image(image_base64: str) -> Dict[str, Any]: |
|
""" |
|
Analyze basic properties of an image (size, mode, color analysis, thumbnail preview). |
|
Args: |
|
image_base64 (str): Base64 encoded image string |
|
Returns: |
|
Dictionary with analysis result |
|
""" |
|
try: |
|
img = decode_image(image_base64) |
|
width, height = img.size |
|
mode = img.mode |
|
|
|
if mode in ("RGB", "RGBA"): |
|
arr = np.array(img) |
|
avg_colors = arr.mean(axis=(0, 1)) |
|
dominant = ["Red", "Green", "Blue"][np.argmax(avg_colors[:3])] |
|
brightness = avg_colors.mean() |
|
color_analysis = { |
|
"average_rgb": avg_colors.tolist(), |
|
"brightness": brightness, |
|
"dominant_color": dominant, |
|
} |
|
else: |
|
color_analysis = {"note": f"No color analysis for mode {mode}"} |
|
|
|
thumbnail = img.copy() |
|
thumbnail.thumbnail((100, 100)) |
|
thumb_path = save_image(thumbnail, "thumbnails") |
|
thumbnail_base64 = encode_image(thumb_path) |
|
|
|
return { |
|
"dimensions": (width, height), |
|
"mode": mode, |
|
"color_analysis": color_analysis, |
|
"thumbnail": thumbnail_base64, |
|
} |
|
except Exception as e: |
|
return {"error": str(e)} |
|
|
|
@tool |
|
def transform_image( |
|
image_base64: str, operation: str, params: Optional[Dict[str, Any]] = None |
|
) -> Dict[str, Any]: |
|
""" |
|
Apply transformations: resize, rotate, crop, flip, brightness, contrast, blur, sharpen, grayscale. |
|
Args: |
|
image_base64 (str): Base64 encoded input image |
|
operation (str): Transformation operation |
|
params (Dict[str, Any], optional): Parameters for the operation |
|
Returns: |
|
Dictionary with transformed image (base64) |
|
""" |
|
try: |
|
img = decode_image(image_base64) |
|
params = params or {} |
|
|
|
if operation == "resize": |
|
img = img.resize( |
|
( |
|
params.get("width", img.width // 2), |
|
params.get("height", img.height // 2), |
|
) |
|
) |
|
elif operation == "rotate": |
|
img = img.rotate(params.get("angle", 90), expand=True) |
|
elif operation == "crop": |
|
img = img.crop( |
|
( |
|
params.get("left", 0), |
|
params.get("top", 0), |
|
params.get("right", img.width), |
|
params.get("bottom", img.height), |
|
) |
|
) |
|
elif operation == "flip": |
|
if params.get("direction", "horizontal") == "horizontal": |
|
img = img.transpose(Image.FLIP_LEFT_RIGHT) |
|
else: |
|
img = img.transpose(Image.FLIP_TOP_BOTTOM) |
|
elif operation == "adjust_brightness": |
|
img = ImageEnhance.Brightness(img).enhance(params.get("factor", 1.5)) |
|
elif operation == "adjust_contrast": |
|
img = ImageEnhance.Contrast(img).enhance(params.get("factor", 1.5)) |
|
elif operation == "blur": |
|
img = img.filter(ImageFilter.GaussianBlur(params.get("radius", 2))) |
|
elif operation == "sharpen": |
|
img = img.filter(ImageFilter.SHARPEN) |
|
elif operation == "grayscale": |
|
img = img.convert("L") |
|
else: |
|
return {"error": f"Unknown operation: {operation}"} |
|
|
|
result_path = save_image(img) |
|
result_base64 = encode_image(result_path) |
|
return {"transformed_image": result_base64} |
|
|
|
except Exception as e: |
|
return {"error": str(e)} |
|
|
|
|
|
tools = [ |
|
tavily_search_tool, |
|
search_wiki, |
|
save_and_read_file, |
|
transform_image, |
|
analyze_image, |
|
analyze_excel_file, |
|
analyze_csv_file, |
|
extract_text_from_image, |
|
download_file_from_url |
|
] |
|
|
|
def build_graph(): |
|
"""Build the graph""" |
|
|
|
llm_with_tools = model.bind_tools(tools) |
|
|
|
|
|
def assistant(state: MessagesState): |
|
"""Assistant node""" |
|
return {"messages": [llm_with_tools.invoke(state["messages"])]} |
|
|
|
builder = StateGraph(MessagesState) |
|
builder.add_node("assistant", assistant) |
|
builder.add_node("tools", ToolNode(tools)) |
|
builder.add_edge(START, "assistant") |
|
builder.add_conditional_edges( |
|
"assistant", |
|
tools_condition, |
|
) |
|
builder.add_edge("tools", "assistant") |
|
|
|
|
|
return builder.compile() |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
question = "When was St. Thomas Aquinas born?" |
|
|
|
graph = build_graph() |
|
|
|
messages = [ |
|
SystemMessage( |
|
content=system_prompt |
|
), |
|
HumanMessage( |
|
content=question |
|
)] |
|
messages = graph.invoke({"messages": messages}) |
|
for m in messages["messages"]: |
|
m.pretty_print() |