Spaces:
Sleeping
Sleeping
fix: Add final_answer template and specialized agent prompts
Browse files- agents/analysis_agent.py +14 -4
- agents/manager_agent.py +10 -3
- agents/web_agent.py +13 -6
- prompts.yaml +62 -0
- tools/analysis_tools.py +45 -8
agents/analysis_agent.py
CHANGED
@@ -1,5 +1,8 @@
|
|
1 |
"""
|
2 |
Analysis agent for interpreting song lyrics and providing deeper context.
|
|
|
|
|
|
|
3 |
"""
|
4 |
|
5 |
from smolagents import CodeAgent, VisitWebpageTool
|
@@ -30,16 +33,23 @@ def create_analysis_agent(model):
|
|
30 |
max_delay=7.0
|
31 |
)
|
32 |
|
33 |
-
# Create and return the agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
agent = CodeAgent(
|
35 |
model=model,
|
36 |
tools=[throttled_search_tool, VisitWebpageTool(), analyze_lyrics_tool],
|
37 |
name="lyrics_analysis_agent",
|
38 |
-
description=
|
39 |
-
additional_authorized_imports=["numpy", "bs4"],
|
40 |
max_steps=config['max_steps'],
|
41 |
verbosity_level=config['verbosity_level'],
|
42 |
-
prompt_templates=
|
43 |
)
|
44 |
|
45 |
logger.info("Analysis agent created successfully")
|
|
|
1 |
"""
|
2 |
Analysis agent for interpreting song lyrics and providing deeper context.
|
3 |
+
|
4 |
+
This agent is responsible for analyzing song lyrics and formatting the analysis
|
5 |
+
in a user-friendly way, showing lyrics with comments underneath them.
|
6 |
"""
|
7 |
|
8 |
from smolagents import CodeAgent, VisitWebpageTool
|
|
|
33 |
max_delay=7.0
|
34 |
)
|
35 |
|
36 |
+
# Create and return the agent with specialized lyrics analysis instructions
|
37 |
+
# Customize the prompts to use our specialized lyrics_analysis_agent prompt
|
38 |
+
custom_prompt_templates = prompt_templates.copy()
|
39 |
+
|
40 |
+
# Set our special prompt as the system prompt for better instruction
|
41 |
+
if 'lyrics_analysis_agent' in custom_prompt_templates:
|
42 |
+
custom_prompt_templates['system_prompt'] = custom_prompt_templates['lyrics_analysis_agent']
|
43 |
+
|
44 |
agent = CodeAgent(
|
45 |
model=model,
|
46 |
tools=[throttled_search_tool, VisitWebpageTool(), analyze_lyrics_tool],
|
47 |
name="lyrics_analysis_agent",
|
48 |
+
description="Specialized agent for analyzing song lyrics and providing detailed commentary",
|
49 |
+
additional_authorized_imports=["numpy", "bs4", "json", "re"],
|
50 |
max_steps=config['max_steps'],
|
51 |
verbosity_level=config['verbosity_level'],
|
52 |
+
prompt_templates=custom_prompt_templates
|
53 |
)
|
54 |
|
55 |
logger.info("Analysis agent created successfully")
|
agents/manager_agent.py
CHANGED
@@ -29,17 +29,24 @@ def create_manager_agent(model):
|
|
29 |
analysis_agent = create_analysis_agent(model)
|
30 |
|
31 |
# Create and return the manager agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
agent = CodeAgent(
|
33 |
model=model,
|
34 |
tools=[FinalAnswerTool()],
|
35 |
name="manager_agent",
|
36 |
-
description=
|
37 |
managed_agents=[web_agent, analysis_agent],
|
38 |
-
additional_authorized_imports=["json"],
|
39 |
planning_interval=config['planning_interval'],
|
40 |
verbosity_level=config['verbosity_level'],
|
41 |
max_steps=config['max_steps'],
|
42 |
-
prompt_templates=
|
43 |
)
|
44 |
|
45 |
logger.info("Manager agent created successfully")
|
|
|
29 |
analysis_agent = create_analysis_agent(model)
|
30 |
|
31 |
# Create and return the manager agent
|
32 |
+
# Customize the prompts to use our specialized lyrics_manager_agent prompt
|
33 |
+
custom_prompt_templates = prompt_templates.copy()
|
34 |
+
|
35 |
+
# Set our special prompt as the system prompt for better instruction
|
36 |
+
if 'lyrics_manager_agent' in custom_prompt_templates:
|
37 |
+
custom_prompt_templates['system_prompt'] = custom_prompt_templates['lyrics_manager_agent']
|
38 |
+
|
39 |
agent = CodeAgent(
|
40 |
model=model,
|
41 |
tools=[FinalAnswerTool()],
|
42 |
name="manager_agent",
|
43 |
+
description="Specialized agent for coordinating lyrics search and analysis",
|
44 |
managed_agents=[web_agent, analysis_agent],
|
45 |
+
additional_authorized_imports=["json", "re"],
|
46 |
planning_interval=config['planning_interval'],
|
47 |
verbosity_level=config['verbosity_level'],
|
48 |
max_steps=config['max_steps'],
|
49 |
+
prompt_templates=custom_prompt_templates
|
50 |
)
|
51 |
|
52 |
logger.info("Manager agent created successfully")
|
agents/web_agent.py
CHANGED
@@ -25,20 +25,27 @@ def create_web_agent(model):
|
|
25 |
|
26 |
# Create the throttled search tool
|
27 |
throttled_search_tool = ThrottledDuckDuckGoSearchTool(
|
28 |
-
min_delay=
|
29 |
-
max_delay=
|
30 |
)
|
31 |
|
32 |
-
# Create and return the agent
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
agent = CodeAgent(
|
34 |
model=model,
|
35 |
tools=[throttled_search_tool, VisitWebpageTool()],
|
36 |
name="lyrics_search_agent",
|
37 |
-
description=
|
38 |
-
additional_authorized_imports=["numpy", "bs4"],
|
39 |
max_steps=config['max_steps'],
|
40 |
verbosity_level=config['verbosity_level'],
|
41 |
-
prompt_templates=
|
42 |
)
|
43 |
|
44 |
logger.info("Web agent (lyrics search) created successfully")
|
|
|
25 |
|
26 |
# Create the throttled search tool
|
27 |
throttled_search_tool = ThrottledDuckDuckGoSearchTool(
|
28 |
+
min_delay=5.0,
|
29 |
+
max_delay=10.0
|
30 |
)
|
31 |
|
32 |
+
# Create and return the agent with specialized lyrics search instructions
|
33 |
+
# Customize the prompts to use our specialized lyrics_search_agent prompt
|
34 |
+
custom_prompt_templates = prompt_templates.copy()
|
35 |
+
|
36 |
+
# Set our special prompt as the system prompt for better instruction
|
37 |
+
if 'lyrics_search_agent' in custom_prompt_templates:
|
38 |
+
custom_prompt_templates['system_prompt'] = custom_prompt_templates['lyrics_search_agent']
|
39 |
+
|
40 |
agent = CodeAgent(
|
41 |
model=model,
|
42 |
tools=[throttled_search_tool, VisitWebpageTool()],
|
43 |
name="lyrics_search_agent",
|
44 |
+
description="Specialized agent for finding and extracting complete song lyrics",
|
45 |
+
additional_authorized_imports=["numpy", "bs4", "json", "re"],
|
46 |
max_steps=config['max_steps'],
|
47 |
verbosity_level=config['verbosity_level'],
|
48 |
+
prompt_templates=custom_prompt_templates
|
49 |
)
|
50 |
|
51 |
logger.info("Web agent (lyrics search) created successfully")
|
prompts.yaml
CHANGED
@@ -291,6 +291,68 @@
|
|
291 |
After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
|
292 |
|
293 |
Now write your new plan below.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
"managed_agent":
|
295 |
"task": |-
|
296 |
You're a helpful agent named '{{name}}'.
|
|
|
291 |
After writing the final step of the plan, write the '\n<end_plan>' tag and stop there.
|
292 |
|
293 |
Now write your new plan below.
|
294 |
+
|
295 |
+
|
296 |
+
"final_answer":
|
297 |
+
"pre_messages": |-
|
298 |
+
Now provide your final answer to the task.
|
299 |
+
"post_messages": |-
|
300 |
+
This is your final answer to the task.
|
301 |
+
|
302 |
+
"lyrics_manager_agent": |-
|
303 |
+
You are a specialized lyrics manager agent. Your task is to coordinate between a web search agent and a lyrics analysis agent to provide comprehensive analysis of song lyrics.
|
304 |
+
|
305 |
+
When a user asks for analysis of a song, you should:
|
306 |
+
1. Use the web_agent to search for the complete lyrics of the requested song
|
307 |
+
2. Extract the full song information: title, artist, and COMPLETE LYRICS
|
308 |
+
3. Pass this information to the analysis_agent to get detailed analysis
|
309 |
+
4. Make sure that the final response includes the FULL TEXT of the song with analysis
|
310 |
+
5. Format your final answer to include clearly marked sections of the song with corresponding analysis
|
311 |
+
|
312 |
+
CRITICAL: Always ensure that the COMPLETE lyrics of the song are included in the final response, section by section, with analysis comments under each section. Never truncate or omit any part of the song lyrics.
|
313 |
+
|
314 |
+
"lyrics_search_agent": |-
|
315 |
+
You are a specialized lyrics search agent. Your task is to find and extract COMPLETE song lyrics from the web.
|
316 |
+
|
317 |
+
When searching for lyrics:
|
318 |
+
1. Search for the exact song title and artist name
|
319 |
+
2. Visit lyrics websites such as Genius, AZLyrics, LyricFind, etc.
|
320 |
+
3. Extract the COMPLETE lyrics of the requested song
|
321 |
+
4. Make sure to get ALL verses, choruses, bridges and other sections
|
322 |
+
5. Return the complete structured data in proper JSON format
|
323 |
+
|
324 |
+
Your response should ALWAYS be in this exact JSON format:
|
325 |
+
{
|
326 |
+
"title": "the exact song title",
|
327 |
+
"artist": "the exact artist name",
|
328 |
+
"lyrics": "the COMPLETE lyrics text with proper line breaks",
|
329 |
+
"source_url": "the URL where you found the lyrics"
|
330 |
+
}
|
331 |
+
|
332 |
+
CRITICAL: Never truncate or omit any part of the song lyrics. Include FULL lyrics every time.
|
333 |
+
|
334 |
+
"lyrics_analysis_agent": |-
|
335 |
+
You are a specialized lyrics analysis agent. Your task is to analyze song lyrics and provide detailed commentary while preserving the FULL text.
|
336 |
+
|
337 |
+
When analyzing song lyrics:
|
338 |
+
1. Use the analyze_lyrics_tool to generate a structured JSON analysis
|
339 |
+
2. Process the JSON analysis to create a formatted response
|
340 |
+
3. Make sure to include the COMPLETE lyrics in your response, section by section
|
341 |
+
4. Add your analytical commentary after each section
|
342 |
+
5. Format your response in a clear, readable manner with proper markdown
|
343 |
+
|
344 |
+
Your response should follow this structure:
|
345 |
+
1. Title and artist header
|
346 |
+
2. Overall analysis section with themes, mood, and general insights
|
347 |
+
3. Section-by-section breakdown with:
|
348 |
+
- Clear section headers (Verse 1, Chorus, etc.)
|
349 |
+
- Complete lyrics for each section in a code block
|
350 |
+
- Detailed analysis after each section
|
351 |
+
4. Significant lines analysis
|
352 |
+
5. Conclusion
|
353 |
+
|
354 |
+
CRITICAL: Always include the FULL text of ALL song lyrics in your analysis. Never summarize or truncate the lyrics.
|
355 |
+
|
356 |
"managed_agent":
|
357 |
"task": |-
|
358 |
You're a helpful agent named '{{name}}'.
|
tools/analysis_tools.py
CHANGED
@@ -22,13 +22,45 @@ def analyze_lyrics_tool(song_title: str, artist: str, lyrics: str) -> str:
|
|
22 |
A summary of the song's meaning in English.
|
23 |
"""
|
24 |
|
25 |
-
prompt = f"""You are an expert in songs and their meanings.
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
"""
|
33 |
|
34 |
# Determine which model to use based on configuration
|
@@ -41,4 +73,9 @@ def analyze_lyrics_tool(song_title: str, artist: str, lyrics: str) -> str:
|
|
41 |
|
42 |
# Use the function with retry mechanism
|
43 |
logger.info("Analyzing lyrics for song: '{}' by '{}'", song_title, artist)
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
22 |
A summary of the song's meaning in English.
|
23 |
"""
|
24 |
|
25 |
+
prompt = f"""You are an expert in songs and their meanings.
|
26 |
+
|
27 |
+
Analyze the song "{song_title}" by {artist}. Return a structured JSON with the following information:
|
28 |
+
|
29 |
+
1. Overall analysis of the song including themes, mood, meaning, and context.
|
30 |
+
2. Section-by-section analysis of each part of the song (verses, chorus, bridge, etc.).
|
31 |
+
3. Line-specific insights for particularly significant lines.
|
32 |
+
4. A conclusion about what makes this song unique or significant.
|
33 |
+
|
34 |
+
Format your response as a valid JSON object with the following structure:
|
35 |
+
```
|
36 |
+
{{
|
37 |
+
"summary": "Overall analysis of the song themes, meaning and mood",
|
38 |
+
"main_themes": ["theme1", "theme2", ...],
|
39 |
+
"mood": "The overall mood/emotion of the song",
|
40 |
+
"sections_analysis": [
|
41 |
+
{{
|
42 |
+
"section_type": "verse/chorus/bridge/etc.",
|
43 |
+
"section_number": 1,
|
44 |
+
"lines": ["line1", "line2", ...],
|
45 |
+
"analysis": "Analysis of this section"
|
46 |
+
}},
|
47 |
+
...
|
48 |
+
],
|
49 |
+
"significant_lines": [
|
50 |
+
{{
|
51 |
+
"line": "A specific important line",
|
52 |
+
"significance": "Why this line is important"
|
53 |
+
}},
|
54 |
+
...
|
55 |
+
],
|
56 |
+
"conclusion": "What makes this song significant or unique"
|
57 |
+
}}
|
58 |
+
```
|
59 |
+
|
60 |
+
Make sure your response is a properly formatted JSON. Only provide the JSON object, no other text.
|
61 |
+
|
62 |
+
Here are the lyrics to analyze:
|
63 |
+
{lyrics}
|
64 |
"""
|
65 |
|
66 |
# Determine which model to use based on configuration
|
|
|
73 |
|
74 |
# Use the function with retry mechanism
|
75 |
logger.info("Analyzing lyrics for song: '{}' by '{}'", song_title, artist)
|
76 |
+
# Get raw analysis as JSON
|
77 |
+
response = make_api_call_with_retry(model_to_use, prompt)
|
78 |
+
|
79 |
+
# Note: we're returning the raw response which should be a JSON string
|
80 |
+
# The agent will be responsible for parsing and formatting this data
|
81 |
+
return response
|