-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
436 lines (376 loc) Β· 17.6 KB
/
agent.py
File metadata and controls
436 lines (376 loc) Β· 17.6 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from crewai import Crew, Task, Agent
from crewai_tools import SerperDevTool
from langchain_ibm import WatsonxLLM
import os
from config import WATSONX_APIKEY, WATSONX_PROJECT_ID, WATSONX_URL, WATSONX_PLATFORM_URL, WATSONX_MODEL_ID, SERPER_API_KEY, OPENWEATHERMAP_API_KEY
import sys
from datetime import datetime
import logging
import requests
from langchain_core.tools import Tool
"""
# Set all required environment variables
os.environ["WATSONX_APIKEY"] = WATSONX_APIKEY
os.environ["WATSONX_PROJECT_ID"] = WATSONX_PROJECT_ID
os.environ["WATSONX_URL"] = WATSONX_URL
os.environ["WATSONX_PLATFORM_URL"] = WATSONX_PLATFORM_URL
os.environ["WATSONX_MODEL_ID"] = WATSONX_MODEL_ID
os.environ["SERPER_API_KEY"] = SERPER_API_KEY
os.environ["OPENWEATHERMAP_API_KEY"] = OPENWEATHERMAP_API_KEY
"""
class OpenWeatherMapTool:
"""Tool to fetch weather data from OpenWeatherMap API."""
def __init__(self):
self.api_key = OPENWEATHERMAP_API_KEY
def fetch_weather(self, city: str) -> str:
"""Fetch weather data for a specified city."""
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={self.api_key}&units=metric"
try:
response = requests.get(url)
data = response.json()
if response.status_code == 200:
weather_description = data['weather'][0]['description']
temperature = data['main']['temp']
return f"The current weather in {city} is {weather_description} with a temperature of {temperature}Β°C."
else:
return f"Error fetching weather data: {data.get('message', 'Unknown error')}"
except Exception as e:
logging.error(f"Error fetching weather: {str(e)}")
return "Unable to fetch weather data at this time."
class MusicAssistant:
def __init__(self):
self.llm = WatsonxLLM(
model_id=WATSONX_MODEL_ID,
url=WATSONX_URL,
params={
"decoding_method": "sample",
"max_new_tokens": 250,
"temperature": 0.5,
"top_k": 50,
"top_p": 0.95
},
project_id=WATSONX_PROJECT_ID,
)
self.search = SerperDevTool()
self.weather_tool = OpenWeatherMapTool()
self.setup_agents()
self.conversation_history = []
self.current_context = None
self.logger = logging.getLogger(__name__)
def setup_agents(self):
# MAYA - Main Conversational Agent with weather tool
self.maya = Agent(
llm=self.llm,
role="Senior Music Industry Advisor",
goal="Help users navigate the music industry and develop their musical skills, ask other agents for help and call tools when needed",
backstory="I'm MAYA, your dedicated music industry advisor with expertise in trends, artist development, and creative direction",
tools=[self.search, Tool(
name="weather_search",
description="Fetch current weather information for a specified city.",
func=self.weather_tool.fetch_weather
)],
allow_delegation=True,
verbose=1,
max_iterations=1,
max_time=200
)
# Trend Analyst with enhanced search capabilities
self.trend_analyst = Agent(
llm=self.llm,
role="Music Trend Analyst",
goal="Provide data-driven insights into music market trends and audience preferences",
backstory="Expert in music analytics and market trends with access to real-time industry data",
tools=[self.search],
verbose=1,
max_iterations=1,
max_time=200
)
# Creative Assistant
self.creative = Agent(
llm=self.llm,
role="Creative Music Assistant",
goal="Generate original musical compositions and lyrical content.",
backstory="Experienced in songwriting and music theory, stays updated with current trends.",
tools=[],
verbose=1,
max_iterations=1,
max_time=200
)
def start_new_chat(self) -> dict:
"""Start a new chat session"""
self.conversation_history = []
self.current_context = None
greeting = self.get_greeting()
return {
"message": greeting,
"timestamp": datetime.now().isoformat()
}
def get_greeting(self) -> str:
"""Get the initial greeting message"""
greeting_task = Task(
description="""
Create a warm, engaging welcome message for a new user.
Include:
1. A friendly introduction as MAYA (Music Assistant for Your Activities)
2. A small explanation of your capabilities
3. An invitation to start the conversation
Keep it short and sweet with artistic flair.
""",
expected_output="A welcoming greeting message",
agent=self.maya
)
try:
return greeting_task.execute()
except Exception as e:
self.logger.error(f"Error generating greeting: {str(e)}")
return """
π Welcome! I'm MAYA, your Music Assistant.
I'm here to help you with anything music-related.
How can I assist you today?
"""
def run_interaction(self, user_input: str) -> dict:
"""Run interaction with delegation to specialized agents"""
try:
if not user_input.strip():
return {
"message": self.show_menu(),
"timestamp": datetime.now().isoformat()
}
# Handle menu options
if user_input.lower() == 'new':
return {
"message": self.start_new_chat(),
"timestamp": datetime.now().isoformat()
}
elif user_input.lower() == 'exit':
return {
"message": "Thank you for using MAYA! Have a musical day! π΅",
"timestamp": datetime.now().isoformat()
}
elif user_input.lower() == 'menu':
return {
"message": self.show_menu(),
"timestamp": datetime.now().isoformat()
}
elif user_input.lower() == 'save':
return {
"message": self.save_last_conversation(),
"timestamp": datetime.now().isoformat()
}
context = self._get_conversation_context()
# First, perform a search to gather relevant information
search_task = Task(
description=f"""
Perform an initial search to gather relevant information about:
Query: {user_input}
Context: {context}
Focus on finding the most recent and relevant information.
Return the search results in a clear, structured format.
""",
expected_output="Initial search results",
agent=self.maya,
max_iterations=1,
max_time=60
)
initial_search_results = search_task.execute()
# Use search results to inform the analysis
analysis_task = Task(
description=f"""
Analyze this query and determine which specialists should handle it:
Query: {user_input}
Context: {context}
Initial Search Results: {initial_search_results}
If the query involves:
- Music industry news, trends, market analysis -> Delegate to Trend Analyst
- Creative aspects (composition, chords, scale modes, lyrics, artistic direction) -> Delegate to Creative Assistant
- If the query requires an internet search, indicate that a search should be performed.
- Multiple aspects -> Coordinate responses from relevant agents
Return the decision as: "TREND", "CREATIVE", "SEARCH", or "BOTH"
""",
expected_output="Analysis decision for delegation",
agent=self.maya,
max_iterations=1,
max_time=60
)
delegation_decision = analysis_task.execute().strip().upper()
responses = []
if delegation_decision in ["TREND", "BOTH"]:
trend_task = Task(
description=f"""
Analyze the following query from a music industry trend perspective:
Query: {user_input}
Context: {context}
Initial Search Results: {initial_search_results}
Provide insights on current trends, market analysis, and industry patterns.
Focus on delivering clear, concise, and accurate information in a single response.
Use the search results to support your analysis.
""",
expected_output="Trend analysis and insights",
agent=self.trend_analyst,
max_iterations=1,
max_time=180
)
responses.append(f"π Industry Analysis:\n{trend_task.execute()}")
if delegation_decision in ["CREATIVE", "BOTH"]:
creative_task = Task(
description=f"""
Address the following query from a creative and artistic perspective:
Query: {user_input}
Context: {context}
Initial Search Results: {initial_search_results}
Provide creative insights, musical suggestions, or artistic direction.
Include detailed chord progressions, scales, and lyrical content if applicable.
IMPORTANT: When generating musical content:
1. For chord progressions, format them clearly with:
- Chord names (e.g., Am, F, C, G)
- Time signature
- Any specific voicings or variations
2. For lyrics, format them with:
- Clear verse/chorus structure
- Line breaks
- Rhyme scheme indicators if applicable
3. Always mark the start of musical content with "π΅ MUSICAL CONTENT START π΅"
4. Always mark the end of musical content with "π΅ MUSICAL CONTENT END π΅"
Focus on delivering complete, well-structured content in a single response.
Use the search results to inform your creative suggestions.
""",
expected_output="Creative insights and suggestions",
agent=self.creative,
max_iterations=1,
max_time=180
)
creative_response = creative_task.execute()
responses.append(f"π΅ Creative Input:\n{creative_response}")
if "π΅ MUSICAL CONTENT START π΅" in creative_response:
self.save_creative_output(creative_response)
if responses:
synthesis_task = Task(
description=f"""
Synthesize these specialist insights into a coherent response:
Initial Search Results: {initial_search_results}
Specialist Responses: {' '.join(responses)}
Create a clear, unified response that incorporates all relevant insights.
Focus on delivering a well-structured, complete response in a single iteration.
Use the search results to support and validate the specialist insights.
""",
expected_output="Synthesized response",
agent=self.maya,
max_iterations=1,
max_time=120
)
final_response = synthesis_task.execute()
else:
final_response = Task(
description=f"""
Provide a direct response to:
Query: {user_input}
Context: {context}
Initial Search Results: {initial_search_results}
Focus on delivering a clear, complete response in a single iteration.
Use the search results to support your response.
""",
expected_output="Direct response",
agent=self.maya,
max_iterations=1,
max_time=60
).execute()
self.conversation_history.append({
'input': user_input,
'output': final_response,
'timestamp': datetime.now().isoformat()
})
return {
"message": final_response,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
self.logger.error(f"Error in interaction: {str(e)}")
return {
"message": f"I apologize, but I encountered an error. Please try again. Error: {str(e)}",
"timestamp": datetime.now().isoformat()
}
def _get_conversation_context(self) -> str:
"""Get context from the last 5 conversations"""
if not self.conversation_history:
return "No previous conversation context."
recent_conversations = self.conversation_history[-1:]
context = "\n".join([
f"User: {conv['input']}\nMAYA: {conv['output']}"
for conv in recent_conversations
])
return context
def get_conversation_history(self):
"""Get the current conversation history"""
return self.conversation_history
def clear_conversation(self):
"""Clear the conversation history"""
self.conversation_history = []
self.current_context = None
def show_menu(self) -> str:
"""Show menu options when user sends empty input"""
menu = """
π Welcome! Here are your options:
1. Start a new chat (type 'new')
2. Show menu options (type 'menu')
3. Save last conversation (type 'save')
4. Exit (type 'exit')
5. Continue current conversation (just type your question)
What would you like to do?
"""
return menu
def save_last_conversation(self) -> str:
"""Save the last conversation to a text file"""
if not self.conversation_history:
return "No conversation to save. Start a new chat first!"
try:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"outputs/conversation_{timestamp}.txt"
# Create outputs directory if it doesn't exist
os.makedirs("outputs", exist_ok=True)
with open(filename, "w", encoding="utf-8") as f:
f.write(f"MAYA AI Conversation - {timestamp}\n")
f.write("=" * 50 + "\n\n")
for conv in self.conversation_history:
f.write(f"User: {conv['input']}\n")
f.write(f"MAYA: {conv['output']}\n")
f.write("-" * 30 + "\n")
return f"Conversation saved successfully to {filename}"
except Exception as e:
self.logger.error(f"Error saving conversation: {str(e)}")
return f"Error saving conversation: {str(e)}"
def save_creative_output(self, creative_output: str):
"""Save creative output to a file"""
try:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"outputs/creative_output_{timestamp}.txt"
# Create outputs directory if it doesn't exist
os.makedirs("outputs", exist_ok=True)
with open(filename, "w", encoding="utf-8") as f:
f.write(f"MAYA AI Creative Output - {timestamp}\n")
f.write("=" * 50 + "\n\n")
f.write(creative_output)
return f"Creative output saved successfully to {filename}"
except Exception as e:
self.logger.error(f"Error saving creative output: {str(e)}")
return f"Error saving creative output: {str(e)}"
def main():
"""Main function to run the music assistant"""
try:
assistant = MusicAssistant()
print("\n" + assistant.get_greeting())
print("\nPress Enter for menu options at any time.")
while True:
user_input = input("\nYour input: ").strip()
if user_input.lower() in ['exit', 'quit', 'bye']:
print("\nThank you for using MAYA! Have a musical day! π΅")
break
response = assistant.run_interaction(user_input)
print(f"\nMAYA: {response['message']}")
# Show menu again if response was menu-related
if "Welcome!" in response['message']:
continue
except Exception as e:
print(f"An error occurred: {str(e)}")
logging.error(f"Application error: {str(e)}")
if __name__ == "__main__":
main()