sivarajng commited on
Commit
fa900ce
·
verified ·
1 Parent(s): 9e6e6d0

Add book_search tool

Browse files
Files changed (1) hide show
  1. app.py +42 -5
app.py CHANGED
@@ -33,6 +33,42 @@ def get_current_time_in_timezone(timezone: str) -> str:
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  final_answer = FinalAnswerTool()
38
 
@@ -42,7 +78,8 @@ final_answer = FinalAnswerTool()
42
  model = HfApiModel(
43
  max_tokens=2096,
44
  temperature=0.5,
45
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
 
46
  custom_role_conversions=None,
47
  )
48
 
@@ -55,14 +92,14 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
62
  planning_interval=None,
63
- name=None,
64
- description=None,
65
- prompt_templates=prompt_templates
66
  )
67
 
68
 
 
33
  except Exception as e:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
+ @tool
37
+ def book_search(title: str, language: str) -> str:
38
+ """A tool that searches for a book based on title or user query and language.
39
+ Args:
40
+ title: A user query or string representing the book title or query.
41
+ language: An optional 2 char string representing the language code. e.g: en, fr, ta
42
+ """
43
+ try:
44
+ # Construct the URL with the given parameters
45
+ base_url = "https://openlibrary.org/search.json"
46
+ params = {
47
+ "title": title,
48
+ "fields": "title,author_name",
49
+ "limit": 10
50
+ }
51
+ if language:
52
+ params["lang"] = language
53
+
54
+ # Make the request
55
+ response = requests.get(base_url, params=params)
56
+ response.raise_for_status()
57
+
58
+ # Parse the response
59
+ data = response.json()
60
+
61
+ # Format results
62
+ results = []
63
+ for book in data.get("docs", []):
64
+ book_title = book.get("title", "Unknown Title")
65
+ authors = ", ".join(book.get("author_name", ["Unknown Author"]))
66
+ results.append(f"Title: *{book_title}*, Author(s): _{authors}_")
67
+
68
+ return "\n".join(results) if results else "No books found."
69
+ except Exception as e:
70
+ return f"Error fetching books: {str(e)}"
71
+
72
 
73
  final_answer = FinalAnswerTool()
74
 
 
78
  model = HfApiModel(
79
  max_tokens=2096,
80
  temperature=0.5,
81
+ # model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
82
+ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded
83
  custom_role_conversions=None,
84
  )
85
 
 
92
 
93
  agent = CodeAgent(
94
  model=model,
95
+ tools=[final_answer, book_search, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
96
  max_steps=6,
97
  verbosity_level=1,
98
  grammar=None,
99
  planning_interval=None,
100
+ name="Book Search Agent",
101
+ description="An agent that searches for books based on user queries and language.",
102
+ prompt_templates=prompt_templates,
103
  )
104
 
105