from mistralai import Mistralfrom linkup import LinkupClientimport json# Initialize clients with your API keysmistral_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:
Copy
Ask AI
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:
Copy
Ask AI
# Start a conversationmessages = [ {"role": "user", "content": "What are the latest AI developments?"}]# Get Mistral's responseresponse = mistral_client.chat.complete( model="mistral-large-latest", messages=messages, tools=tools)# Check if Mistral wants to call our functionif 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.