This tutorial shows you how to use Mistral AI’s function calling with Linkup to give your AI access to real-time web search results.
2

Install and Setup

Install the required packages:
pip install linkup-sdk mistralai
Set up your clients:
from mistralai import Mistral
from linkup import LinkupClient
import json

# Initialize clients with your API keys
mistral_client = Mistral(api_key="your_mistral_api_key")
linkup_client = LinkupClient(api_key="your_linkup_api_key")
3

Define the Function Schema

Tell Mistral about your search function:
tools = [{
    "type": "function",
    "function": {
        "name": "search_web",
        "description": "Search the web in real time. Use this tool whenever the user needs trusted facts, news, or source-backed information. Returns comprehensive content from the most relevant sources.",
        "parameters": {
            "type": "object",
            "properties": {
                "query": {
                    "type": "string",
                    "description": "The search query"
                }
            },
            "required": ["query"]
        }
    }
}]
4

Handle the Conversation

Put it all together:
# Start a conversation
messages = [
    {"role": "user", "content": "What are the latest AI developments?"}
]

# Get Mistral's response
response = mistral_client.chat.complete(
    model="mistral-large-latest",
    messages=messages,
    tools=tools
)

# Check if Mistral wants to call our function
if response.choices[0].message.tool_calls:
    # Get the function call details
    tool_call = response.choices[0].message.tool_calls[0]
    args = json.loads(tool_call.function.arguments)
    
    # Call Linkup to search the web
    linkup_response = linkup_client.search(query=args["query"], depth="standard")
    search_results = json.dumps([{"content": result.content} for result in linkup_response.results])
    
    # Add the function call and result to messages
    messages.append(response.choices[0].message)
    messages.append({
        "role": "tool",
        "name": "search_web",
        "content": search_results,
        "tool_call_id": tool_call.id
    })
    
    # Get final response with search results
    final_response = mistral_client.chat.complete(
        model="mistral-large-latest",
        messages=messages
    )
    
    print(final_response.choices[0].message.content)
else:
    print(response.choices[0].message.content)
That’s it! Your AI can now search the web in real-time using Linkup whenever it needs current information.