Implementation

You may use Tools in conjunction with our Web Search API to implement Live Real-Time Web Access to any of our models.

Heres a quick example in Python using Tools and the official shuttleai python lib:

First, let’s start off by making our method which accesses ShuttleAI’s Web Search API.

import httpx
from urllib.parse import quote, unquote

def search_web(query: str, model: str = "search-ddg", limit: int = 5):
    with httpx.get(
        url=f"https://api.shuttleai.app/v1/web-search?q={quote(query)}&limit={limit}&model={model}&key=$SHUTTLEAI_API_KEY"
    ) as response:
        return response.json()

Next, let’s assign a tool which will call our search_web function.

tools = [
    {
        "type": "function",
        "function": {
            "name": "search_web",
            "description": "Searches the web for a given query.",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "The query to search for."
                    },
                    "model": {
                        "type": "string",
                        "enum": ["search-ddg", "search-google"],
                        "description": "Defaults to `search-ddg`. The model to search with."
                    },
                    "limit": {
                        "type": "integer",
                        "description": "Defaults to `5`. The number of results to retrieve for the search."
                    }
                },
                "required": ["query"],
            }
        }
    }
]

Lookin’ good! All that’s left is sending our tools to our chat_completion request, retrieving the arguments, sending those arguments to our function, sending a second request with the results of our function for the model to answer our query.

This should look something like this by the end:

import json, httpx, asyncio
from urllib.parse import quote

from shuttleai import *

def search_web(query: str, model: str = "search-ddg", limit: int = 5):
    with httpx.get(
        url=f"https://api.shuttleai.app/v1/web-search?q={quote(query)}&limit={limit}&model={model}&key=$SHUTTLEAI_API_KEY"
    ) as response:
        return response.json()

tools = [
    {
        "type": "function",
        "function": {
            "name": "search_web",
            "description": "Searches the web for a given query.",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "The query to search for."
                    }
                },
                "required": ["query"],
            }
        }
    }
]

async def main():

    convo = [
        {
            "role": "system",
            "content": "You are an AI with granted access to the internet using your built in `search_web(query: str)` function."
        },
        {
            "role": "user",
            "content": "When was the Five Nights at Freddys movie released? use your tool to search the web for the answer."
        }
    ]

    async with ShuttleAsyncClient("$SHUTTLEAI_API_KEY", 120) as shuttle:
        first_response = await shuttle.chat_completion(
            model="gemini-pro",
            messages=convo,
            tools=tools,
            tool_choice="auto"
        )

        # print(first_response.choices[0].model_dump()) # { 'finish_reason': 'tool_calls', 'message': { 'tool_calls': [ { 'function': { 'arguments': '{"query": "Five Nights at Freddys movie release date"}', 'name': 'search_web' }, 'id': '...', 'type': 'function' } ] } }
        
        if first_response.choices[0].finish_reason == 'tool_calls':
            tool_calls = first_response.choices[0].message.tool_calls
            
            if tool_calls[0].function.name == 'search_web':
                query = json.loads(tool_calls[0].function.arguments)['query']
                search_results = search_web(query)

                # print(search_results) # [ { 'title': '...', 'link': '...', 'snippet': '...' }, { ... } ]
                
                if search_results:
                    convo.append({"role": "system", "content": f"Search results for '{query}': ```{search_results}```"})
                
                second_response = await shuttle.chat_completion(
                    model="gpt-3.5-turbo",
                    messages=convo
                )

                print(second_response.choices[0].model_dump()) # { 'finish_reason': 'stop', 'message': { 'content': 'The Five Nights at Freddy's movie was released on October 27, 2023. It was released for streaming on Peacock and theatrically in the United States.' } }

if __name__ == "__main__":
    asyncio.run(main())