I am trying to create a news delivery agent but I am not able to receive response from a custom tool I made. I am able to print the output of the tool, but the agent is not able to get the output. Sharing my code below
import json
from phi.tools import Toolkit
from phi.utils.log import logger
from googleapiclient.discovery import build
from config import google_developer_api_key, custom_search_engine_id
def extract_title_and_link(items):
extracted = [
{
"title": item.get("title"),
"link": item.get("link")
}
for item in items if "title" in item and "link" in item
]
return extracted
class GoogleSearchApi(Toolkit):
"""
To search google using Google Custom Search API
"""
def __init__(self):
super().__init__(name="googlesearch")
self.register(self.google_search)
def google_search(self, query):
"""Use this function to search Google for combined query"""
try:
api_key = google_developer_api_key
cse_id = custom_search_engine_id
print("Entered google search")
service = build("customsearch", "v1", developerKey=api_key)
res = service.cse().list(q=query, cx=cse_id, num =10).execute()
items = res.get('items', [])
formatted_json = json.dumps(items, indent=2, ensure_ascii=False)
logging.info("Results for query: %s\n%s", query, formatted_json)
if not items:
return "No results found."
cleaned_result = extract_title_and_link(items)
logging.info(cleaned_result)
return str(cleaned_result)
except Exception as e:
logger.error(f"Error in Google Search: {e}")
return f"Error in Google Search: {e}"
This is the agent that I made
def create_google_searcher_agent_api():
"""Agent to search google using the custom search API."""
google_searcher_agent_api_instance = Agent(
name = "Google Search API",
enable_thinking=True,
tools=[GoogleSearchApi()],
description="You are a news agent that helps users find the latest news.",
instructions=[
"Given the topics create a search query of 4 words or less",
"Important - You can only use one tool call, so create a single search query by \
combining the topics given by the user \
and respond with URLs of latest news items about that topic."
],
add_datetime_to_instructions=True,
debug_mode=True,
show_tool_calls=True,
tool_call_limit=1
)
return google_searcher_agent_api_instance
Error that I get
ERROR - Failed to get search results
Invalid message type: <class ‘phi.run.response.RunResponse’>