Spaces:
Sleeping
Sleeping
File size: 3,516 Bytes
4f6b4f2 |
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 |
from dotenv import load_dotenv
from smolagents import tool, PythonInterpreterTool, DuckDuckGoSearchTool, WikipediaSearchTool, VisitWebpageTool, GoogleSearchTool, ToolCallingAgent, CodeAgent, LiteLLMModel
from youtube_transcript_api import YouTubeTranscriptApi
import os
from typing import Dict
load_dotenv()
system_prompt = f"""
You are a general AI assistant that will receive a question.
First, provide a step-by-step explanation of your reasoning to answer the question,
providing your answer with the following template: **FINAL ANSWER: [YOUR FINAL ANSWER]**.
[YOUR FINAL ANSWER] should be a number OR as few words as possible OR a comma separated list of numbers and/or strings.
**Rules to follow when generating your answer:**
- If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise.
- If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise.
- If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.
"""
@tool
def add(a: int, b: int) -> int:
#Google docstring style
"""
Add two numbers together
Args:
a: The first number
b: The second number
Returns:
The sum of the two numbers
"""
return a + b
@tool
def subtract(a: int, b: int) -> int:
"""
Subtract two numbers
Args:
a: The first number
b: The second number
Returns:
The difference between the two numbers
"""
return a - b
@tool
def multiply(a: int, b: int) -> int:
"""
Multiply two numbers
Args:
a: The first number
b: The second number
Returns:
The product of the two numbers
"""
return a * b
@tool
def divide(a: int, b: int) -> float:
"""
Divide two numbers
Args:
a: The first number
b: The second number
Returns:
The quotient of the two numbers
"""
return a / b
@tool
def modulo(a: int, b: int) -> int:
"""
Calculate the modulo of two numbers
Args:
a: The first number
b: The second number
Returns:
The remainder of the division of the two numbers
"""
return a % b
@tool
def youtube_transcript(url: str) -> Dict[str, str]:
"""
Get transcript of YouTube video.
Args:
url: YouTube video url in ""
Returns:
Transcript of the YouTube video
"""
video_id = url.partition("https://www.youtube.com/watch?v=")[2]
transcript = YouTubeTranscriptApi.get_transcript(video_id)
transcript_text = " ".join([item["text"] for item in transcript])
return {"youtube_transcript": transcript_text}
class BasicSmolAgent:
def __init__(self):
self.api_key = os.getenv("OPENAI_API_KEY")
self.model = LiteLLMModel(model_id="openai/o4-mini", api_key=self.api_key)
self.agent = CodeAgent(
tools=[
add, subtract, multiply, divide, modulo,
youtube_transcript,
DuckDuckGoSearchTool(),
WikipediaSearchTool(),
VisitWebpageTool(),
PythonInterpreterTool(),
],
model=self.model
)
def __call__(self, question: str) -> str:
print(f"Question: {question}")
return self.agent.run(question) |