[3' read]
Mistral AI Search Engine
When using models, it's crucial to acknowledge that their knowledge can be outdated and limited to their training data. For this reason, allowing the model to access the web is an important step in creating reliable, knowledgeable agents that can answer questions about daily and recent events, or provide rare and specialized knowledge as ground truth.
Agents & Conversations with Web Search#
We provide a web search tool out of the box, which can be used via our Agents and Conversation API. This API includes a list of built-in tools you can use, such as websearch and web_search_premium:
websearch: Provides access to a search engine to navigate the web.web_search_premium: Provides access to a search engine as well as reliable news sources, including verified content from news providers.
Setup#
Let's set up our SDK and create an API key here.
!pip install mistralaifrom mistralai.client import Mistral
import getpass
import os
if not os.environ.get("MISTRAL_API_KEY"):
os.environ["MISTRAL_API_KEY"] = getpass.getpass("Mistral API key: ")
client = Mistral(os.environ["MISTRAL_API_KEY"])With our client ready, we can now create an Agent with web search access. When creating an Agent, you can provide a name, model, description, system prompt instructions, and a set of tools to use.
For this demo, we will only provide web_search access.
websearch_agent = client.beta.agents.create(
model="mistral-medium-latest",
description="Agent able to search information over the web, such as news, weather, sport results...",
name="Websearch Agent",
instructions="You have the ability to perform web searches with `web_search` to find up-to-date information.",
tools=[{"type": "web_search_premium"}],
completion_args={
"temperature": 0.3,
"top_p": 0.95,
}
)
websearch_agentAgent created, we can start a conversation at any moment.
agent_response = client.beta.conversations.start(
agent_id=websearch_agent.id,
inputs="What happened during Apple WWDC 2024?"
)
agent_responseNote: It is also possible to use websearch without an Agent by providing a model directly without an agent id:
response = client.beta.conversations.start(
model="mistral-medium-latest",
instructions="You have the ability to perform web searches with `web_search` to find up-to-date information.",
inputs="What happened during Apple WWDC 2024?",
tools=[{"type": "web_search_premium"}],
# store=False
)
responseThe outputs of our Agents & Conversations API includes the models reply and tool calls, let's clean and format using the references.
# @title Format Response
def format_response_with_references(response):
text_parts = []
references = []
for output in response.outputs:
if output.type == "message.output":
if isinstance(output, str):
return output
for content in output.content:
if content.type == "text":
text_parts.append(content.text)
elif content.type == "tool_reference":
ref = {
"title": getattr(content, "title", "No Title"),
"url": getattr(content, "url", "No URL"),
}
# Add the reference to the list if it's not already there
if ref not in references:
references.append(ref)
# Get the index of the reference (now guaranteed to exist)
ref_index = references.index(ref) + 1
text_parts.append(f"[{ref_index}]")
# Combine all text parts
full_text = " ".join(text_parts)
# Build the references list
reference_list = []
for i, ref in enumerate(references, start=1):
reference_list.append(f"[{i}] {ref['title']} - {ref['url']}")
# Append references to the text
formatted_text = full_text + "\n\nReferences:\n" + "\n".join(reference_list)
return formatted_text
# Example usage:
formatted_response = format_response_with_references(agent_response)
print(formatted_response)Websearch Conversations#
One of the advantages of our Agents & Conversations API compred to Chat Completions is the ability to manage multiturn and conversations out of the box in our cloud without the need of handling them locally.
When creating a conversation, by default, they are stored and you can continue the conversation at any moment. To disable this feature use store=False.
agent_new_response = client.beta.conversations.append(
conversation_id=agent_response.conversation_id,
inputs="Translate to French."
)You can easily create a chat interface leveraging all these features like so:
conversation_id = None
while True:
inp = input("User > ")
if inp == "quit":
break
if conversation_id:
agent_response = client.beta.conversations.start(
agent_id=websearch_agent.id,
inputs=inp
)
conversation_id = agent_response.conversation_id
else:
agent_response = client.beta.conversations.append(
conversation_id=agent_response.conversation_id,
inputs=inp
)
print("Agent >", format_response_with_references(agent_response))