Hanah

Hanah

wow agent day07 Search Engine Agent

Reference:Dtawhale wow agent day07

Apply for Search Engine API#

We use Bocha API;
Main Features

  • Content compliance and high quality: Data sources and search results comply with domestic safety regulations, data does not go abroad, meeting the application scenarios of B-end enterprise users, providing clean, accurate, and high-quality search results for AI applications.
  • Multi-modal mixed search: Supports keyword + vector mixed search, using bocha-semantic-reranker for secondary sorting, significantly improving the relevance of search results to questions, with search results including web pages, images, videos, Douyin videos, and other multi-modal content.
  • Multi-model AI search: Has integrated top domestic AI large models such as Alibaba Tongyi Qianwen, ByteDance Yunque large model, and Moon's Dark Side Kimi, allowing users to choose different models for search based on specific needs.
  • Intelligent agent search: Introduces AI intelligent agents, providing richer and deeper answers, currently in internal testing phase. Various lifestyle, business, and knowledge intelligent agents such as restaurant search, hotel search, and scenic spot search.

Search Engine Agent#

After customizing llm and loading the API;

from llama_index.core.tools import FunctionTool
import requests
# You need to fill in BOCHA_API_KEY in the .env file first.
BOCHA_API_KEY = os.getenv('BOCHA_API_KEY')

# Define Bocha Web Search tool
def bocha_web_search_tool(query: str, count: int = 8) -> str:
    """
    Use Bocha Web Search API for online search, returning the search results as a string.
    
    Parameters:
    - query: Search keywords
    - count: Number of search results to return

    Returns:
    - String representation of the search results
    """
    url = 'https://api.bochaai.com/v1/web-search'
    headers = {
        'Authorization': f'Bearer {BOCHA_API_KEY}',  # Please replace with your API key
        'Content-Type': 'application/json'
    }
    data = {
        "query": query,
        "freshness": "noLimit", # Time range for the search, e.g., "oneDay", "oneWeek", "oneMonth", "oneYear", "noLimit"
        "summary": True, # Whether to return a long text summary
        "count": count
    }

    response = requests.post(url, headers=headers, json=data)

    if response.status_code == 200:
        # Formatted search result text returned to the large model
        # You can customize the processing of Bocha's search results
        return str(response.json())
    else:
        raise Exception(f"API request failed, status code: {response.status_code}, error message: {response.text}")

search_tool = FunctionTool.from_defaults(fn=bocha_web_search_tool)
from llama_index.core.agent import ReActAgent
agent = ReActAgent.from_tools([search_tool], llm=llm, verbose=True)

This code mainly defines a function named bocha_web_search_tool, which is used to perform online searches through the Bocha Web Search API and integrates it into an agent.

Test#

# Test case
query = "What are the main contents of Alibaba's ESG report for 2024?"
response = agent.chat(f"Please help me search for the following content: {query}")
print(response)
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.