Spaces:
Running
Running
Update agent.py
Browse files
agent.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
-
from smolagents import CodeAgent, LiteLLMModel, tool, load_tool
|
2 |
-
from langchain_community.tools.tavily_search import TavilySearchResults
|
3 |
-
from langchain_community.document_loaders import WikipediaLoader
|
4 |
-
from youtube_transcript_api import YouTubeTranscriptApi
|
5 |
import asyncio
|
6 |
import os
|
7 |
import yaml
|
@@ -36,33 +36,33 @@ from io import BytesIO
|
|
36 |
# except Exception as e:
|
37 |
# return f"Error performing Google search: {str(e)}"
|
38 |
|
39 |
-
|
40 |
-
def wiki_search(query: str) -> str:
|
41 |
-
"""Search Wikipedia for a query and return maximum 2 results.
|
42 |
-
|
43 |
-
Args:
|
44 |
-
query: The search query."""
|
45 |
-
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
46 |
-
formatted_search_docs = "\n\n---\n\n".join(
|
47 |
-
[
|
48 |
-
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
49 |
-
for doc in search_docs
|
50 |
-
])
|
51 |
-
return {"wiki_results": formatted_search_docs}
|
52 |
|
53 |
-
|
54 |
-
def web_search(query: str) -> str:
|
55 |
-
"""Search Tavily for a query and return maximum 3 results.
|
56 |
-
|
57 |
-
Args:
|
58 |
-
query: The search query."""
|
59 |
-
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
60 |
-
formatted_search_docs = "\n\n---\n\n".join(
|
61 |
-
[
|
62 |
-
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
63 |
-
for doc in search_docs
|
64 |
-
])
|
65 |
-
return {"web_results": formatted_search_docs}
|
66 |
|
67 |
@tool
|
68 |
def ImageAnalysisTool(image_path: str) -> str:
|
@@ -94,7 +94,7 @@ def ImageAnalysisTool(image_path: str) -> str:
|
|
94 |
|
95 |
response = agent.run(
|
96 |
"""
|
97 |
-
Describe
|
98 |
""",
|
99 |
images=image
|
100 |
)
|
@@ -102,17 +102,17 @@ def ImageAnalysisTool(image_path: str) -> str:
|
|
102 |
return f"The image description: '{response}'"
|
103 |
|
104 |
|
105 |
-
|
106 |
-
def youtube_transcript(url: str) -> str:
|
107 |
-
"""
|
108 |
-
Get transcript of YouTube video.
|
109 |
-
Args:
|
110 |
-
url: YouTube video url in ""
|
111 |
-
"""
|
112 |
-
video_id = url.partition("https://www.youtube.com/watch?v=")[2]
|
113 |
-
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
114 |
-
transcript_text = " ".join([item["text"] for item in transcript])
|
115 |
-
return {"youtube_transcript": transcript_text}
|
116 |
|
117 |
#@tool
|
118 |
#class LocalFileAudioTool:
|
@@ -133,7 +133,7 @@ class MagAgent:
|
|
133 |
"""Initialize the MagAgent with search tools."""
|
134 |
print("Initializing MagAgent with search tools...")
|
135 |
model = LiteLLMModel(
|
136 |
-
model_id="gemini/gemini-2.0-flash
|
137 |
api_key= os.environ.get("GEMINI_KEY"),
|
138 |
max_tokens=8192
|
139 |
)
|
|
|
1 |
+
from smolagents import CodeAgent, LiteLLMModel, tool, load_tool, DuckDuckGoSearchTool, WikipediaSearchTool #, HfApiModel, OpenAIServerModel
|
2 |
+
#from langchain_community.tools.tavily_search import TavilySearchResults
|
3 |
+
#from langchain_community.document_loaders import WikipediaLoader
|
4 |
+
#from youtube_transcript_api import YouTubeTranscriptApi
|
5 |
import asyncio
|
6 |
import os
|
7 |
import yaml
|
|
|
36 |
# except Exception as e:
|
37 |
# return f"Error performing Google search: {str(e)}"
|
38 |
|
39 |
+
#@tool
|
40 |
+
#def wiki_search(query: str) -> str:
|
41 |
+
# """Search Wikipedia for a query and return maximum 2 results.
|
42 |
+
#
|
43 |
+
# Args:
|
44 |
+
# query: The search query."""
|
45 |
+
# search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
46 |
+
# formatted_search_docs = "\n\n---\n\n".join(
|
47 |
+
# [
|
48 |
+
# f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
49 |
+
# for doc in search_docs
|
50 |
+
# ])
|
51 |
+
# return {"wiki_results": formatted_search_docs}
|
52 |
|
53 |
+
#@tool
|
54 |
+
#def web_search(query: str) -> str:
|
55 |
+
# """Search Tavily for a query and return maximum 3 results.
|
56 |
+
#
|
57 |
+
# Args:
|
58 |
+
# query: The search query."""
|
59 |
+
# search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
60 |
+
# formatted_search_docs = "\n\n---\n\n".join(
|
61 |
+
# [
|
62 |
+
# f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
63 |
+
# for doc in search_docs
|
64 |
+
# ])
|
65 |
+
# return {"web_results": formatted_search_docs}
|
66 |
|
67 |
@tool
|
68 |
def ImageAnalysisTool(image_path: str) -> str:
|
|
|
94 |
|
95 |
response = agent.run(
|
96 |
"""
|
97 |
+
Describe in details the chess position you see in the image.
|
98 |
""",
|
99 |
images=image
|
100 |
)
|
|
|
102 |
return f"The image description: '{response}'"
|
103 |
|
104 |
|
105 |
+
#@tool
|
106 |
+
#def youtube_transcript(url: str) -> str:
|
107 |
+
# """
|
108 |
+
# Get transcript of YouTube video.
|
109 |
+
# Args:
|
110 |
+
# url: YouTube video url in ""
|
111 |
+
# """
|
112 |
+
# video_id = url.partition("https://www.youtube.com/watch?v=")[2]
|
113 |
+
# transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
114 |
+
# transcript_text = " ".join([item["text"] for item in transcript])
|
115 |
+
# return {"youtube_transcript": transcript_text}
|
116 |
|
117 |
#@tool
|
118 |
#class LocalFileAudioTool:
|
|
|
133 |
"""Initialize the MagAgent with search tools."""
|
134 |
print("Initializing MagAgent with search tools...")
|
135 |
model = LiteLLMModel(
|
136 |
+
model_id="gemini/gemini-2.0-flash",
|
137 |
api_key= os.environ.get("GEMINI_KEY"),
|
138 |
max_tokens=8192
|
139 |
)
|