olaf-bu commited on
Commit
c0f4bac
·
1 Parent(s): 51da116

fixed tool input

Browse files
Files changed (1) hide show
  1. tools.py +36 -33
tools.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import logging
2
  from typing import List
3
  import os
@@ -5,6 +6,9 @@ import os
5
  from smolagents import Tool, PythonInterpreterTool, VisitWebpageTool, WikipediaSearchTool
6
  from duckduckgo_search import DDGS
7
 
 
 
 
8
  import whisper
9
  from youtube_transcript_api import YouTubeTranscriptApi
10
 
@@ -24,9 +28,8 @@ class ReadFileTool(Tool):
24
 
25
  name = 'read_file'
26
  description = 'Reads a file and returns its content'
27
- inputs = {
28
- 'file_path': {'type': 'string', 'description': 'Path to the file to read'},
29
- }
30
  output_type = 'string'
31
 
32
  def forward(self, file_path: str) -> str:
@@ -52,9 +55,8 @@ class OpenAISpeechToTextTool(Tool):
52
 
53
  name = 'transcribe_audio'
54
  description = 'Transcribes audio to text and returns the text'
55
- inputs = {
56
- 'audio_path': {'type': 'string', 'description': 'Path to the audio file'},
57
- }
58
  output_type = 'string'
59
 
60
  def forward(self, audio_path: str) -> str:
@@ -83,9 +85,9 @@ class YouTubeTranscriptionTool(Tool):
83
 
84
  name = 'youtube_transcription'
85
  description = 'Fetches the transcript of a YouTube video given its URL'
86
- inputs = {
87
- 'video_url': {'type': 'string', 'description': 'YouTube video URL'},
88
- }
89
  output_type = 'string'
90
 
91
  def forward(self, video_url: str) -> str:
@@ -94,6 +96,9 @@ class YouTubeTranscriptionTool(Tool):
94
  return ' '.join([entry['text'] for entry in transcript])
95
 
96
 
 
 
 
97
  class DescribeImageTool(Tool):
98
  """
99
  Tool to analyze and describe any image using GPT-4 Vision API.
@@ -167,22 +172,16 @@ class DescribeImageTool(Tool):
167
  # Make the API call
168
  response = client.chat.completions.create(
169
  model='gpt-4.1',
170
- messages=[
171
- {
172
- 'role': 'user',
173
- 'content': [
174
- {'type': 'text', 'text': prompt},
175
- {
176
- 'type': 'image_url',
177
- 'image_url': {
178
- 'url': f'data:image/jpeg;base64,{base64_image}'
179
- },
180
- },
181
- ],
182
- }
183
- ],
184
  max_tokens=1000,
185
- )
186
 
187
  return response.choices[0].message.content
188
 
@@ -203,21 +202,25 @@ class ReliableDuckDuckGoSearchTool(Tool):
203
  """
204
 
205
  name = 'duckduckgo_search'
206
- description = 'Performs a web search using DuckDuckGo and returns the top result.'
207
 
208
- def __init__(self, num_results=5):
 
 
 
 
 
 
209
  self.num_results = num_results
210
 
211
  def forward(self, query: str) -> str:
212
- results_text = []
213
  with DDGS() as ddgs:
214
  results = ddgs.text(query, max_results=self.num_results)
215
- for r in results:
216
- results_text.append(f"{r['title']}: {r['href']}")
217
-
218
- if not results_text:
219
- return 'No results found.'
220
- return '\n'.join(results_text)
221
 
222
 
223
  def get_tools() -> List[Tool]:
 
1
+ import base64
2
  import logging
3
  from typing import List
4
  import os
 
6
  from smolagents import Tool, PythonInterpreterTool, VisitWebpageTool, WikipediaSearchTool
7
  from duckduckgo_search import DDGS
8
 
9
+ from openai import OpenAI
10
+
11
+
12
  import whisper
13
  from youtube_transcript_api import YouTubeTranscriptApi
14
 
 
28
 
29
  name = 'read_file'
30
  description = 'Reads a file and returns its content'
31
+ inputs = {'file_path': {'type': 'string', 'description': 'Path to the file to read'},
32
+ }
 
33
  output_type = 'string'
34
 
35
  def forward(self, file_path: str) -> str:
 
55
 
56
  name = 'transcribe_audio'
57
  description = 'Transcribes audio to text and returns the text'
58
+ inputs = {'audio_path': {'type': 'string', 'description': 'Path to the audio file'},
59
+ }
 
60
  output_type = 'string'
61
 
62
  def forward(self, audio_path: str) -> str:
 
85
 
86
  name = 'youtube_transcription'
87
  description = 'Fetches the transcript of a YouTube video given its URL'
88
+ inputs = {'video_url': {'type': 'string',
89
+ 'description': 'YouTube video URL'},
90
+ }
91
  output_type = 'string'
92
 
93
  def forward(self, video_url: str) -> str:
 
96
  return ' '.join([entry['text'] for entry in transcript])
97
 
98
 
99
+ client = OpenAI()
100
+
101
+
102
  class DescribeImageTool(Tool):
103
  """
104
  Tool to analyze and describe any image using GPT-4 Vision API.
 
172
  # Make the API call
173
  response = client.chat.completions.create(
174
  model='gpt-4.1',
175
+ messages=[{'role': 'user',
176
+ 'content': [{'type': 'text',
177
+ 'text': prompt},
178
+ {'type': 'image_url',
179
+ 'image_url': {'url': f'data:image/jpeg;base64,{base64_image}'
180
+ },
181
+ }, ],
182
+ }],
 
 
 
 
 
 
183
  max_tokens=1000,
184
+ )
185
 
186
  return response.choices[0].message.content
187
 
 
202
  """
203
 
204
  name = 'duckduckgo_search'
205
+ description = 'Performs a duckduckgo web search based on your query (think a Google search) then returns the top search results.'
206
 
207
+ inputs = {'query': {'type': 'string',
208
+ 'description': 'The search query to perform.'},
209
+ }
210
+ output_type = 'string'
211
+
212
+
213
+ def __init__(self, num_results=3):
214
  self.num_results = num_results
215
 
216
  def forward(self, query: str) -> str:
 
217
  with DDGS() as ddgs:
218
  results = ddgs.text(query, max_results=self.num_results)
219
+ if len(results) == 0:
220
+ raise Exception("No results found! Try a less restrictive/shorter query.")
221
+ postprocessed_results = [f"[{result['title']}]({result['href']})\n{result['body']}"
222
+ for result in results]
223
+ return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
 
224
 
225
 
226
  def get_tools() -> List[Tool]: