dawid-lorek commited on
Commit
33e3bfc
·
verified ·
1 Parent(s): 2f667b0

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +31 -6
agent.py CHANGED
@@ -1,15 +1,31 @@
1
- from smolagents import LiteLLMModel, CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, PythonInterpreterTool, tool
 
2
  from youtube_transcript_api import YouTubeTranscriptApi
3
  import os
4
 
5
  @tool
6
  def reverse_sentence_tool(reverse_sentence: str) -> str:
 
 
 
 
 
 
 
 
7
  inverted_words = reverse_sentence.split(" ")[::-1]
8
  correct_words = [word[::-1] for word in inverted_words]
9
  return " ".join(correct_words)
10
 
11
  @tool
12
  def get_youtube_transcript(video_url: str) -> str:
 
 
 
 
 
 
 
13
  video_id = video_url.split("v=")[-1]
14
  transcript = YouTubeTranscriptApi.get_transcript(video_id)
15
  full_text = " ".join([entry['text'] for entry in transcript])
@@ -17,6 +33,13 @@ def get_youtube_transcript(video_url: str) -> str:
17
 
18
  @tool
19
  def check_answer(answer: str) -> str:
 
 
 
 
 
 
 
20
  if answer and answer[-1] == '.':
21
  answer = answer[:-1]
22
  if "St." in answer:
@@ -25,9 +48,11 @@ def check_answer(answer: str) -> str:
25
 
26
  class BasicAgent:
27
  def __init__(self):
28
- api_key = os.environ.get("OPENAI_API_KEY", "")
29
- # Użyj modelu openai, np. gpt-4o lub gpt-3.5-turbo
30
- self.model = LiteLLMModel(model_id="gpt-4o", api_key=api_key)
 
 
31
  self.agent = CodeAgent(
32
  tools=[
33
  DuckDuckGoSearchTool(),
@@ -35,11 +60,11 @@ class BasicAgent:
35
  VisitWebpageTool(),
36
  reverse_sentence_tool,
37
  get_youtube_transcript,
38
- check_answer
39
  ],
40
  model=self.model
41
  )
42
- print("BasicAgent initialized.")
43
 
44
  def __call__(self, question: str) -> str:
45
  print(f"Agent received question: {question[:50]}...")
 
1
+ from smolagents import LiteLLMModel
2
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool, tool, PythonInterpreterTool
3
  from youtube_transcript_api import YouTubeTranscriptApi
4
  import os
5
 
6
  @tool
7
  def reverse_sentence_tool(reverse_sentence: str) -> str:
8
+ """
9
+ Receives a sentence where both the word order and the characters in each word are reversed.
10
+ Returns the sentence with words and order corrected.
11
+ Args:
12
+ reverse_sentence: A sentence with reversed words and reversed word order.
13
+ Returns:
14
+ A sentence in natural reading order.
15
+ """
16
  inverted_words = reverse_sentence.split(" ")[::-1]
17
  correct_words = [word[::-1] for word in inverted_words]
18
  return " ".join(correct_words)
19
 
20
  @tool
21
  def get_youtube_transcript(video_url: str) -> str:
22
+ """
23
+ Fetches the transcript from a YouTube video if available.
24
+ Args:
25
+ video_url: Full URL to the YouTube video.
26
+ Returns:
27
+ Transcript text.
28
+ """
29
  video_id = video_url.split("v=")[-1]
30
  transcript = YouTubeTranscriptApi.get_transcript(video_id)
31
  full_text = " ".join([entry['text'] for entry in transcript])
 
33
 
34
  @tool
35
  def check_answer(answer: str) -> str:
36
+ """
37
+ Reviews the answer to check that it meets the requirements specified by the user and modifies it if necessary.
38
+ Args:
39
+ answer (str): The answer of the Agent.
40
+ Returns:
41
+ str: The final answer.
42
+ """
43
  if answer and answer[-1] == '.':
44
  answer = answer[:-1]
45
  if "St." in answer:
 
48
 
49
  class BasicAgent:
50
  def __init__(self):
51
+ # Odczytaj klucz OpenAI z ENV
52
+ self.api_key = os.getenv("OPENAI_API_KEY")
53
+ # Ustaw preferowany model, np. GPT-4o lub inny OpenAI
54
+ self.model = LiteLLMModel(model_id="gpt-4o", api_key=self.api_key)
55
+
56
  self.agent = CodeAgent(
57
  tools=[
58
  DuckDuckGoSearchTool(),
 
60
  VisitWebpageTool(),
61
  reverse_sentence_tool,
62
  get_youtube_transcript,
63
+ check_answer,
64
  ],
65
  model=self.model
66
  )
67
+ print("BasicAgent initialized (OpenAI).")
68
 
69
  def __call__(self, question: str) -> str:
70
  print(f"Agent received question: {question[:50]}...")