Spaces:
Runtime error
Runtime error
Commit ·
be6f51b
1
Parent(s): c71f12f
Complete Alfred smolagents
Browse files- app.py +51 -0
- retriever.py +43 -0
- tools.py +47 -0
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel
|
| 4 |
+
from langchain_ollama import ChatOllama
|
| 5 |
+
|
| 6 |
+
# Import our custom tools from their modules
|
| 7 |
+
from tools import WeatherInfoTool, HubStatsTool
|
| 8 |
+
from retriever import load_guest_dataset
|
| 9 |
+
|
| 10 |
+
# Initialize the Hugging Face model
|
| 11 |
+
model = LiteLLMModel(
|
| 12 |
+
model_id="ollama_chat/qwen2.5-coder:7b",
|
| 13 |
+
api_base="http://localhost:xxxx",
|
| 14 |
+
temperature=0
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Initialize the web search tool
|
| 18 |
+
search_tool = DuckDuckGoSearchTool()
|
| 19 |
+
|
| 20 |
+
# Initialize the weather tool
|
| 21 |
+
weather_info_tool = WeatherInfoTool()
|
| 22 |
+
|
| 23 |
+
# Initialize the Hub stats tool
|
| 24 |
+
hub_stats_tool = HubStatsTool()
|
| 25 |
+
|
| 26 |
+
# Load the guest dataset and initialize the guest info tool
|
| 27 |
+
guest_info_tool = load_guest_dataset()
|
| 28 |
+
|
| 29 |
+
# Create Alfred with all the tools
|
| 30 |
+
alfred = CodeAgent(tools=[guest_info_tool, search_tool, hub_stats_tool, weather_info_tool], model=model)
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
# query = "Tell me about 'Lady Ada Lovelace'"
|
| 34 |
+
# query = "What's the weather like in Paris tonight? Will it be suitable for our fireworks display?"
|
| 35 |
+
# query = "One of our guests is from Qwen. What can you tell me about their most popular model?"
|
| 36 |
+
query = "I need to speak with Dr. Nikola Tesla about recent advancements in wireless energy. Can you help me prepare for this conversation?"
|
| 37 |
+
|
| 38 |
+
# First interaction
|
| 39 |
+
# response1 = alfred_with_memory.run("Tell me about Lady Ada Lovelace.")
|
| 40 |
+
# print("🎩 Alfred's First Response:")
|
| 41 |
+
# print(response1)
|
| 42 |
+
|
| 43 |
+
# # Second interaction (referencing the first)
|
| 44 |
+
# response2 = alfred_with_memory.run("What projects is she currently working on?", reset=False)
|
| 45 |
+
# print("🎩 Alfred's Second Response:")
|
| 46 |
+
# print(response2)
|
| 47 |
+
|
| 48 |
+
response = alfred.run(query)
|
| 49 |
+
print("🎩 Alfred's Response:")
|
| 50 |
+
print(response)
|
| 51 |
+
|
retriever.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import Tool
|
| 2 |
+
import datasets
|
| 3 |
+
|
| 4 |
+
class GuestInfoRetrieverTool(Tool):
|
| 5 |
+
name = "guest_info_retriever"
|
| 6 |
+
description = "Retrieves detailed information about gala guests based on their name or relation."
|
| 7 |
+
inputs = {
|
| 8 |
+
"query": {
|
| 9 |
+
"type": "string",
|
| 10 |
+
"description": "The name or relation of the guest you want information about."
|
| 11 |
+
}
|
| 12 |
+
}
|
| 13 |
+
output_type = "string"
|
| 14 |
+
|
| 15 |
+
def __init__(self, guest_list, **kwargs):
|
| 16 |
+
super().__init__(**kwargs)
|
| 17 |
+
self.guest_list = guest_list
|
| 18 |
+
|
| 19 |
+
def forward(self, query: str):
|
| 20 |
+
query = query.lower()
|
| 21 |
+
# Busca simples por correspondência de texto em qualquer campo
|
| 22 |
+
results = []
|
| 23 |
+
for guest in self.guest_list:
|
| 24 |
+
# Verifica se a query está no nome ou na descrição
|
| 25 |
+
if query in guest['name'].lower() or query in guest['description'].lower() or query in guest['relation'].lower():
|
| 26 |
+
info = (f"Name: {guest['name']}\n"
|
| 27 |
+
f"Relation: {guest['relation']}\n"
|
| 28 |
+
f"Description: {guest['description']}\n"
|
| 29 |
+
f"Email: {guest['email']}")
|
| 30 |
+
results.append(info)
|
| 31 |
+
|
| 32 |
+
if results:
|
| 33 |
+
# Retorna os 3 primeiros matches
|
| 34 |
+
return "\n\n---\n\n".join(results[:3])
|
| 35 |
+
else:
|
| 36 |
+
return "No matching guest information found."
|
| 37 |
+
|
| 38 |
+
def load_guest_dataset():
|
| 39 |
+
# Carrega o dataset da Hugging Face
|
| 40 |
+
dataset = datasets.load_dataset("agents-course/unit3-invitees", split="train")
|
| 41 |
+
|
| 42 |
+
# Passamos a lista crua de dicionários para a Tool
|
| 43 |
+
return GuestInfoRetrieverTool(guest_list=list(dataset))
|
tools.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
from smolagents import Tool
|
| 3 |
+
from huggingface_hub import list_models
|
| 4 |
+
|
| 5 |
+
class WeatherInfoTool(Tool):
|
| 6 |
+
name = "weather_info"
|
| 7 |
+
description = "Fetches dummy weather information for a given location."
|
| 8 |
+
inputs = {
|
| 9 |
+
"location": {
|
| 10 |
+
"type": "string",
|
| 11 |
+
"description": "The location to get weather information for."
|
| 12 |
+
}
|
| 13 |
+
}
|
| 14 |
+
output_type = "string"
|
| 15 |
+
|
| 16 |
+
def forward(self, location: str):
|
| 17 |
+
weather_conditions = [
|
| 18 |
+
{"condition": "Rainy", "temp_c": 15},
|
| 19 |
+
{"condition": "Clear", "temp_c": 25},
|
| 20 |
+
{"condition": "Windy", "temp_c": 20}
|
| 21 |
+
]
|
| 22 |
+
data = random.choice(weather_conditions)
|
| 23 |
+
return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"
|
| 24 |
+
|
| 25 |
+
class HubStatsTool(Tool):
|
| 26 |
+
name = "hub_stats"
|
| 27 |
+
description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
|
| 28 |
+
inputs = {
|
| 29 |
+
"author": {
|
| 30 |
+
"type": "string",
|
| 31 |
+
"description": "The username of the model author/organization to find models from."
|
| 32 |
+
}
|
| 33 |
+
}
|
| 34 |
+
output_type = "string"
|
| 35 |
+
|
| 36 |
+
def forward(self, author: str):
|
| 37 |
+
try:
|
| 38 |
+
models = list(list_models(author=author, sort="downloads", direction=-1, limit=1))
|
| 39 |
+
if models:
|
| 40 |
+
model = models[0]
|
| 41 |
+
# Usando format para números grandes
|
| 42 |
+
downloads = getattr(model, 'downloads', 0)
|
| 43 |
+
return f"The most downloaded model by {author} is {model.id} with {downloads:,} downloads."
|
| 44 |
+
else:
|
| 45 |
+
return f"No models found for author {author}."
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Error fetching models for {author}: {str(e)}"
|