File size: 2,419 Bytes
33e3bfc ed680f1 fde864c 1d8f1ea ed680f1 33e3bfc ed680f1 33e3bfc ed680f1 33e3bfc ed680f1 fde864c 33e3bfc ed680f1 33e3bfc ed680f1 7ec5a35 33e3bfc 4044d5c ed680f1 |
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 |
from smolagents import LiteLLMModel
from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, tool, PythonInterpreterTool
from youtube_transcript_api import YouTubeTranscriptApi
import os
@tool
def reverse_sentence_tool(reverse_sentence: str) -> str:
"""
Receives a sentence where both the word order and the characters in each word are reversed.
Returns the sentence with words and order corrected.
Args:
reverse_sentence: A sentence with reversed words and reversed word order.
Returns:
A sentence in natural reading order.
"""
inverted_words = reverse_sentence.split(" ")[::-1]
correct_words = [word[::-1] for word in inverted_words]
return " ".join(correct_words)
@tool
def get_youtube_transcript(video_url: str) -> str:
"""
Fetches the transcript from a YouTube video if available.
Args:
video_url: Full URL to the YouTube video.
Returns:
Transcript text.
"""
video_id = video_url.split("v=")[-1]
transcript = YouTubeTranscriptApi.get_transcript(video_id)
full_text = " ".join([entry['text'] for entry in transcript])
return full_text
@tool
def check_answer(answer: str) -> str:
"""
Reviews the answer to check that it meets the requirements specified by the user and modifies it if necessary.
Args:
answer (str): The answer of the Agent.
Returns:
str: The final answer.
"""
if answer and answer[-1] == '.':
answer = answer[:-1]
if "St." in answer:
answer = answer.replace("St.", "Saint")
return answer
class BasicAgent:
def __init__(self):
# Odczytaj klucz OpenAI z ENV
self.api_key = os.getenv("OPENAI_API_KEY")
# Ustaw preferowany model, np. GPT-4o lub inny OpenAI
self.model = LiteLLMModel(model_id="gpt-4o", api_key=self.api_key)
self.agent = CodeAgent(
tools=[
DuckDuckGoSearchTool(),
PythonInterpreterTool(),
VisitWebpageTool(),
reverse_sentence_tool,
get_youtube_transcript,
check_answer,
],
model=self.model
)
print("BasicAgent initialized (OpenAI).")
def __call__(self, question: str) -> str:
print(f"Agent received question: {question[:50]}...")
answer = self.agent.run(question)
return answer |