Spaces:
Sleeping
Sleeping
File size: 7,397 Bytes
b762719 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 71b5dbb d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 d3dc6dc 2051cf0 b762719 2051cf0 d3dc6dc 2051cf0 b762719 2051cf0 d3dc6dc 2051cf0 3ef1b50 2051cf0 d3dc6dc 2051cf0 d3dc6dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
import base64
import re
from dotenv import load_dotenv
import requests
# langchain imports
from langchain_core.tools import Tool, tool
from langchain_core.messages import HumanMessage
from langgraph.prebuilt import create_react_agent
#from langgraph.graph import START, StateGraph
#from langgraph.prebuilt import tools_condition
from langchain_community.tools import DuckDuckGoSearchRun
from langchain_community.retrievers import WikipediaRetriever
from langchain_experimental.utilities import PythonREPL
from langchain_google_genai import ChatGoogleGenerativeAI
#from langchain_ollama import ChatOllama
from langfuse.langchain import CallbackHandler
# tool imports
import pandas as pd
import whisper
from youtube_transcript_api import YouTubeTranscriptApi
load_dotenv()
langfuse_handler = CallbackHandler()
# --- LLM ---
#llm = ChatOllama(model="qwen3:8b", temperature=0)
llm = ChatGoogleGenerativeAI(model='gemini-2.5-flash', temperature=0)
# --- System Prompt ---
with open('system_prompt.txt', 'r', encoding='utf-8') as f:
system_prompt = f.read()
# --- Tools ---
# python REPL tool
python_repl = PythonREPL()
execute_python = Tool(
name="execute_python",
description="A Python shell. Use this tool to execute python commands. "
"Input should be valid python code. "
"If you want to see the output of a value, you should print it out with `print(...)`.",
func=python_repl.run,
)
@tool
def get_youtube_transcript(url: str) -> str:
"""
Retrieve the text transcript of a YouTube video
Args:
url (str): link to the YouTube video
Returns:
str: text transcript
"""
def extract_video_id(url: str) -> str:
# extracts video id from youtube url
patterns = [
r"v=([a-zA-Z0-9_-]{11})", # regular link
r"youtu\.be/([a-zA-Z0-9_-]{11})", # shortened link
r"youtube\.com/embed/([a-zA-Z0-9_-]{11})", # embed link
]
for pattern in patterns:
match = re.search(pattern, url)
if match:
return match.group(1)
raise ValueError("Invalid YouTube URL")
try:
video_id = extract_video_id(url)
api = YouTubeTranscriptApi()
transcript = api.fetch(video_id)
txt = '\n'.join([s.text for s in transcript.snippets])
return txt
except Exception as e:
return f"An error occured using get_youtube_transcript tool: {e}"
@tool
def reverse_string(text: str) -> str:
"""
A tool to reverse the order of characters in a text string
Args:
text (str): text string to reverse
Returns:
str: reversed text string
"""
try:
return text[::-1]
except Exception as e:
return f"An error occured using reverse_string tool: {e}"
@tool
def search_web(query: str) -> str:
"""
A tool to perform a search for a query using the web
Args:
query (str): query to search on the web
Returns:
str: web search result
"""
try:
search = DuckDuckGoSearchRun()
return search.invoke(query)
except Exception as e:
return f"An error occured using search_web tool: {e}"
@tool
def search_wikipedia(query: str) -> str:
"""
A tool to perform a search for a query using Wikipedia
Args:
query (str): query to search on Wikipedia
Returns:
str: wikipedia search result
"""
try:
retriever = WikipediaRetriever()
return retriever.invoke(query)
except Exception as e:
return f"An error occured using search_wiki tool: {e}"
@tool
def transcribe_audio(url: str) -> str:
"""
A tool to transcribe an audio file (.mp3) using an automatic speech recognition model
Args:
url (str): link to audio file (.mp3)
Returns:
str: transcript of the audio file
"""
try:
# fetch audio file
response = requests.get(url)
response.raise_for_status()
tmp = 'tmp_audio.mp3'
with open(tmp, "wb") as f:
f.write(response.content)
# transcribe
model = whisper.load_model('tiny')
result = model.transcribe(tmp)
return result['text']
except Exception as e:
return f"An error occured using transcribe_audio tool: {e}"
@tool(response_format='content_and_artifact')
def view_png_file(url: str) -> str:
"""
A tool to view the contents of an image file (.png)
Args:
url (str): link to image file (.png)
Returns:
str: image contents
"""
try:
# fetch the image
response = requests.get(url)
response.raise_for_status()
# convert image bytes to base64
image = base64.b64encode(response.content).decode('utf-8')
# text + image artifact
return (
"Here is the image.",
[{
"type": "image",
"source": {
"type": "url",
"url": image,
}
}]
)
except Exception as e:
return f"An error occured using view_png_file tool: {e}"
@tool
def view_py_file(url: str) -> str:
"""
A tool to view the contents of a python file (.py)
Args:
url (str): link to python file (.py)
Returns:
str: contents of python file
"""
try:
# fetch python file
response = requests.get(url)
response.raise_for_status()
return response.text
except Exception as e:
return f"An error occured using view_py_file tool: {e}"
@tool
def view_xlsx_file(url: str) -> str:
"""
A tool to view the contents of an excel file (.xlsx)
Args:
url (str): link to excel file (.xlsx)
Returns:
str: contents of excel file
"""
try:
# fetch python file
response = requests.get(url)
response.raise_for_status()
tmp = 'tmp.xlsx'
with open(tmp, "wb") as f:
f.write(response.content)
data = pd.read_excel('tmp.xlsx')
return data.to_string()
except Exception as e:
return f"An error occured using view_xlsx_file tool: {e}"
# agent toolkit
tools = [
execute_python,
get_youtube_transcript,
reverse_string,
search_web, search_wikipedia,
transcribe_audio,
view_png_file, view_py_file, view_xlsx_file
]
# --- LangGraph ---
agent = create_react_agent(
model=llm,
tools=tools,
prompt=system_prompt
)
class GAIAAgent:
def __init__(self):
print("GAIAAgent initialized.")
def __call__(self, question: str) -> str:
print(f"Agent received question: {question}")
messages = agent.invoke(
{"messages": [
#SystemMessage(content=system),
HumanMessage(content=question)
]},
config={
"callbacks": [langfuse_handler],
"recursion_limit": 10
}
)
# extract answer
final_message = messages['messages'][-1].content
match = re.search(r"(?<=FINAL ANSWER:\s).*", final_message)
if match:
final_answer = match.group(0)
else:
final_answer = final_message
print(f"Agent returning answer: {final_answer}")
return final_answer
|