Spaces:
Sleeping
Sleeping
Developed the basis
Browse files(cherry picked from commit 3b22ac66d7d7e18a6b29687a07e06ce49b5e8487)
- src/model.py +21 -0
- src/party_planner/rag_agent.py +72 -0
- src/party_planner/rag_multiAgents.py +74 -0
- src/party_planner/tools/travel_time.py +62 -0
src/model.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import HfApiModel
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def get_model(
|
| 5 |
+
model_id: str,
|
| 6 |
+
provider: str,
|
| 7 |
+
max_tokens: int = 2048,
|
| 8 |
+
**kwargs
|
| 9 |
+
) -> HfApiModel:
|
| 10 |
+
return HfApiModel(
|
| 11 |
+
model_id=model_id,
|
| 12 |
+
provider=provider,
|
| 13 |
+
max_tokens=max_tokens,
|
| 14 |
+
**kwargs
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
if __name__ == "__main__":
|
| 19 |
+
ModelId = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 20 |
+
Provider = "together"
|
| 21 |
+
model = get_model(ModelId, Provider)
|
src/party_planner/rag_agent.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.docstore.document import Document
|
| 2 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 3 |
+
from smolagents import Tool
|
| 4 |
+
from langchain_community.retrievers import BM25Retriever
|
| 5 |
+
from smolagents import CodeAgent, HfApiModel
|
| 6 |
+
|
| 7 |
+
class PartyPlanningRetrieverTool(Tool):
|
| 8 |
+
name = "party_planning_retriever"
|
| 9 |
+
description = "Uses semantic search to retrieve relevant party planning ideas for Alfred’s superhero-themed party at Wayne Manor."
|
| 10 |
+
inputs = {
|
| 11 |
+
"query": {
|
| 12 |
+
"type": "string",
|
| 13 |
+
"description": "The query to perform. This should be a query related to party planning or superhero themes.",
|
| 14 |
+
}
|
| 15 |
+
}
|
| 16 |
+
output_type = "string"
|
| 17 |
+
|
| 18 |
+
def __init__(self, docs, **kwargs):
|
| 19 |
+
super().__init__(**kwargs)
|
| 20 |
+
self.retriever = BM25Retriever.from_documents(
|
| 21 |
+
docs, k=5 # Retrieve the top 5 documents
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
def forward(self, query: str) -> str:
|
| 25 |
+
assert isinstance(query, str), "Your search query must be a string"
|
| 26 |
+
|
| 27 |
+
docs = self.retriever.invoke(
|
| 28 |
+
query,
|
| 29 |
+
)
|
| 30 |
+
return "\nRetrieved ideas:\n" + "".join(
|
| 31 |
+
[
|
| 32 |
+
f"\n\n===== Idea {str(i)} =====\n" + doc.page_content
|
| 33 |
+
for i, doc in enumerate(docs)
|
| 34 |
+
]
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Simulate a knowledge base about party planning
|
| 38 |
+
party_ideas = [
|
| 39 |
+
{"text": "A superhero-themed masquerade ball with luxury decor, including gold accents and velvet curtains.", "source": "Party Ideas 1"},
|
| 40 |
+
{"text": "Hire a professional DJ who can play themed music for superheroes like Batman and Wonder Woman.", "source": "Entertainment Ideas"},
|
| 41 |
+
{"text": "For catering, serve dishes named after superheroes, like 'The Hulk's Green Smoothie' and 'Iron Man's Power Steak.'", "source": "Catering Ideas"},
|
| 42 |
+
{"text": "Decorate with iconic superhero logos and projections of Gotham and other superhero cities around the venue.", "source": "Decoration Ideas"},
|
| 43 |
+
{"text": "Interactive experiences with VR where guests can engage in superhero simulations or compete in themed games.", "source": "Entertainment Ideas"}
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
source_docs = [
|
| 47 |
+
Document(page_content=doc["text"], metadata={"source": doc["source"]})
|
| 48 |
+
for doc in party_ideas
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
# Split the documents into smaller chunks for more efficient search
|
| 52 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 53 |
+
chunk_size=500,
|
| 54 |
+
chunk_overlap=50,
|
| 55 |
+
add_start_index=True,
|
| 56 |
+
strip_whitespace=True,
|
| 57 |
+
separators=["\n\n", "\n", ".", " ", ""],
|
| 58 |
+
)
|
| 59 |
+
docs_processed = text_splitter.split_documents(source_docs)
|
| 60 |
+
|
| 61 |
+
# Create the retriever tool
|
| 62 |
+
party_planning_retriever = PartyPlanningRetrieverTool(docs_processed)
|
| 63 |
+
|
| 64 |
+
# Initialize the agent
|
| 65 |
+
agent = CodeAgent(tools=[party_planning_retriever], model=HfApiModel())
|
| 66 |
+
|
| 67 |
+
# Example usage
|
| 68 |
+
response = agent.run(
|
| 69 |
+
"Find ideas for a luxury superhero-themed party, including entertainment, catering, and decoration options."
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
print(response)
|
src/party_planner/rag_multiAgents.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import GoogleSearchTool, VisitWebpageTool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
from src.agent import create_agent
|
| 5 |
+
from src.model import get_model
|
| 6 |
+
from src.party_planner.tools.travel_time import calculate_cargo_travel_time
|
| 7 |
+
from src.party_planner.utils import check_reasoning_and_plot
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
web_agent = create_agent(
|
| 11 |
+
"code_agent",
|
| 12 |
+
"web_agent",
|
| 13 |
+
model=get_model(
|
| 14 |
+
"Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 15 |
+
provider="together",
|
| 16 |
+
max_tokens=1024
|
| 17 |
+
),
|
| 18 |
+
tools=[
|
| 19 |
+
GoogleSearchTool(provider="serper"),
|
| 20 |
+
VisitWebpageTool(),
|
| 21 |
+
calculate_cargo_travel_time,
|
| 22 |
+
],
|
| 23 |
+
description="Browses the web to find information",
|
| 24 |
+
verbosity=0,
|
| 25 |
+
max_steps=10
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
manager_agent = create_agent(
|
| 30 |
+
"code_agent",
|
| 31 |
+
"manager_agent",
|
| 32 |
+
get_model(
|
| 33 |
+
"deepseek-ai/DeepSeek-R1",
|
| 34 |
+
"together",
|
| 35 |
+
2048
|
| 36 |
+
),
|
| 37 |
+
[calculate_cargo_travel_time],
|
| 38 |
+
15,
|
| 39 |
+
[
|
| 40 |
+
"geopandas",
|
| 41 |
+
"plotly",
|
| 42 |
+
"shapely",
|
| 43 |
+
"json",
|
| 44 |
+
"pandas",
|
| 45 |
+
"numpy",
|
| 46 |
+
],
|
| 47 |
+
interval=5,
|
| 48 |
+
verbosity=2,
|
| 49 |
+
final_answer_checks=[check_reasoning_and_plot],
|
| 50 |
+
managed_agents=[web_agent]
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
if __name__ == '__main__':
|
| 55 |
+
manager_agent.visualize()
|
| 56 |
+
|
| 57 |
+
manager_agent.run("""
|
| 58 |
+
Find all Batman filming locations in the world, calculate the time to transfer via cargo plane to here (we're in Gotham, 40.7128° N, 74.0060° W).
|
| 59 |
+
Also give me some supercar factories with the same cargo plane transfer time. You need at least 6 points in total.
|
| 60 |
+
Represent this as spatial map of the world, with the locations represented as scatter points with a color that depends on the travel time, and save it to saved_map.png!
|
| 61 |
+
|
| 62 |
+
Here's an example of how to plot and return a map:
|
| 63 |
+
import plotly.express as px
|
| 64 |
+
df = px.data.carshare()
|
| 65 |
+
fig = px.scatter_map(df, lat="centroid_lat", lon="centroid_lon", text="name", color="peak_hour", size=100,
|
| 66 |
+
color_continuous_scale=px.colors.sequential.Magma, size_max=15, zoom=1)
|
| 67 |
+
fig.show()
|
| 68 |
+
fig.write_image("saved_image.png")
|
| 69 |
+
final_answer(fig)
|
| 70 |
+
|
| 71 |
+
Never try to process strings using code: when you have a string to read, just print it and you'll see it.
|
| 72 |
+
""")
|
| 73 |
+
|
| 74 |
+
manager_agent.python_executor.state["fig"]
|
src/party_planner/tools/travel_time.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
from typing import Optional, Tuple
|
| 3 |
+
|
| 4 |
+
from smolagents import tool
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
@tool
|
| 8 |
+
def calculate_cargo_travel_time(
|
| 9 |
+
origin_coords: Tuple[float, float],
|
| 10 |
+
destination_coords: Tuple[float, float],
|
| 11 |
+
cruising_speed_kmh: Optional[float] = 750.0, # Average speed for cargo planes
|
| 12 |
+
) -> float:
|
| 13 |
+
"""
|
| 14 |
+
Calculate the travel time for a cargo plane between two points on Earth using great-circle distance.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
origin_coords: Tuple of (latitude, longitude) for the starting point
|
| 18 |
+
destination_coords: Tuple of (latitude, longitude) for the destination
|
| 19 |
+
cruising_speed_kmh: Optional cruising speed in km/h (defaults to 750 km/h for typical cargo planes)
|
| 20 |
+
|
| 21 |
+
Returns:
|
| 22 |
+
float: The estimated travel time in hours
|
| 23 |
+
|
| 24 |
+
Example:
|
| 25 |
+
>>> # Chicago (41.8781° N, 87.6298° W) to Sydney (33.8688° S, 151.2093° E)
|
| 26 |
+
>>> result = calculate_cargo_travel_time((41.8781, -87.6298), (-33.8688, 151.2093))
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
def to_radians(degrees: float) -> float:
|
| 30 |
+
return degrees * (math.pi / 180)
|
| 31 |
+
|
| 32 |
+
# Extract coordinates
|
| 33 |
+
lat1, lon1 = map(to_radians, origin_coords)
|
| 34 |
+
lat2, lon2 = map(to_radians, destination_coords)
|
| 35 |
+
|
| 36 |
+
# Earth's radius in kilometers
|
| 37 |
+
EARTH_RADIUS_KM = 6371.0
|
| 38 |
+
|
| 39 |
+
# Calculate great-circle distance using the haversine formula
|
| 40 |
+
dlon = lon2 - lon1
|
| 41 |
+
dlat = lat2 - lat1
|
| 42 |
+
|
| 43 |
+
a = (
|
| 44 |
+
math.sin(dlat / 2) ** 2
|
| 45 |
+
+ math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
|
| 46 |
+
)
|
| 47 |
+
c = 2 * math.asin(math.sqrt(a))
|
| 48 |
+
distance = EARTH_RADIUS_KM * c
|
| 49 |
+
|
| 50 |
+
# Add 10% to account for non-direct routes and air traffic controls
|
| 51 |
+
actual_distance = distance * 1.1
|
| 52 |
+
|
| 53 |
+
# Calculate flight time
|
| 54 |
+
# Add 1 hour for takeoff and landing procedures
|
| 55 |
+
flight_time = (actual_distance / cruising_speed_kmh) + 1.0
|
| 56 |
+
|
| 57 |
+
# Format the results
|
| 58 |
+
return round(flight_time, 2)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
print(calculate_cargo_travel_time((41.8781, -87.6298), (-33.8688, 151.2093)))
|