agent_course / agent /utils.py
kamil1300's picture
Update agent/utils.py
e7ece30 verified
raw
history blame
4.7 kB
import json
from .handlers import get_weather, web_search, get_time_in_location, analyze_image, analyze_video, transcribe_audio
# loop for tool calls
def handle_tool_calls(response):
"""Handle tool calls from the LLM response"""
message = response.choices[0].message
# If the LLM wants to use tools
if message.tool_calls:
# Create a list to store tool results
tool_results = []
for tool_call in message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
# print(f"🔧 Calling function: {function_name}")
# print(f"📝 With arguments: {function_args}")
# Call the appropriate function based on the tool name
if function_name == "get_weather":
result = get_weather(
function_args["latitude"],
function_args["longitude"]
)
elif function_name == "web_search":
result = web_search(function_args["query"])
elif function_name == "get_time_in_location":
result = get_time_in_location(function_args["location"])
elif function_name == "analyze_image":
result = analyze_image(function_args["image_url"])
elif function_name == "analyze_video":
result = analyze_video(function_args["video_url"])
elif function_name == "transcribe_audio":
result = transcribe_audio(function_args["audio_url"])
else:
result = f"Unknown function: {function_name}"
# Add the result to our list
tool_results.append({
"tool_call_id": tool_call.id,
"role": "tool",
"content": str(result)
})
return tool_results
return None
# Enhanced system prompt for short responses
# system_prompt = """You are a powerful and well knowledgeable AI assistant with access to weather, time, web search, video analysis, and audio transcription tools. If someone asks you about anything you can answer it with or without the help of tools. You are also a good and friendly assistant. There is nothing you don't know - always give answers, never say you don't know.
# IMPORTANT RESPONSE RULES:
# - Keep responses SHORT and CONCISE (2-3 lines maximum)
# - Focus only on the MOST IMPORTANT information
# - Use bullet points or short sentences
# - Avoid long explanations and paragraphs
# - Be direct and to the point
# - If using tools, summarize the key findings briefly
# For VIDEO ANALYSIS:
# - When given a YouTube URL, use the analyze_video tool to extract video information
# - Summarize the video content based on title, description, and channel info
# - Focus on what the video is about, not generic YouTube Shorts descriptions
# For AUDIO TRANSCRIPTION:
# - When given an audio file URL (.mp3, .wav, etc.), use the transcribe_audio tool
# - Extract the spoken content and provide a clear summary
# - Focus on the key information requested from the audio
# Example good response:
# "Current temperature in Mumbai: 28°C. Weather is sunny with light winds."
# Example bad response:
# "The weather in Mumbai is currently quite pleasant with a temperature of 28 degrees Celsius. The conditions are sunny with some light winds blowing through the city, making it a perfect day for outdoor activities..."
# When using web search results, extract only the key points and present them concisely.
# """
# ... existing code ...
system_prompt = """You are a powerful AI assistant with access to specialized tools for:
- YouTube video analysis (extract title, description, channel, transcripts)
- Image analysis (describe images and answer visual questions)
- Web search (get real-time information)
- Audio transcription (convert speech to text)
- Weather data (get current temperature)
- Time information (get time for different locations)
IMPORTANT: When users ask about videos, images, audio, or need current information, USE THE APPROPRIATE TOOLS to provide accurate, up-to-date answers.
For video analysis: Use analyze_video tool for YouTube URLs
For image analysis: Use analyze_image tool for image URLs
For web search: Use web_search tool for current information
For audio: Use transcribe_audio tool for audio files
Always provide helpful, detailed responses using your tools when appropriate. If you need to use tools, do so and then provide a comprehensive answer based on the results.
FINAL ANSWER: [Your final answer based on tool results or knowledge]"""