Citations enable models to ground their responses and provide references, making them a powerful feature for Retrieval-Augmented Generation (RAG) and agentic applications. This feature allows the model to provide the source of the information extracted from a document or chunk of data from a tool call.
Our models have been deeply trained to ground on documents and provide sources, with a built-in feature to extract references and citations.
Code Example
To provide documents to the model, you can include the sources as a function call response.
Below is an example of references, in this case from Wikipedia, using tool calls.
Initialize Client
1import os
2from mistralai import Mistral, ToolMessage
3import json
4
5api_key = os.environ["MISTRAL_API_KEY"]
6model = "mistral-large-latest"
7
8client = Mistral(api_key=api_key)
Define the Tool
In this case, we will create a get_information
tool that will return the references mentioned previously.
1get_information_tool = {
2 "type": "function",
3 "function": {
4 "name": "get_information",
5 "description": "Get information from external source.",
6 "parameters": {}
7 },
8}
9
10def get_information():
11 return json.dumps(references)
Set Up Chat History
1chat_history = [
2 {
3 "role": "system",
4 "content": "Answer the user by providing references to the source of the information."
5 },
6 {
7 "role": "user",
8 "content": "Who won the Nobel Prize in 2024?"
9 }
10]
Make the Initial Chat Request
1chat_response = client.chat.complete(
2 model=model,
3 messages=chat_history,
4 tools=[get_information_tool],
5)
6
7if hasattr(chat_response.choices[0].message, 'tool_calls'):
8 tool_call = chat_response.choices[0].message.tool_calls[0]
9 chat_history.append(chat_response.choices[0].message)
10 print(tool_call)
11else:
12 print("No tool call found in the response")
Output:
1function=FunctionCall(name='get_information', arguments='{}') id='F4HiRgdZp' type=None index=0
Handle Tool Call and Append Result
1result = get_information()
2
3tool_call_result = ToolMessage(
4 content=result,
5 tool_call_id=tool_call.id,
6 name=tool_call.function.name,
7)
8
9# Append the tool call message to the chat_history
10chat_history.append(tool_call_result)
Make the Final Chat Request
1chat_response = client.chat.complete(
2 model=model,
3 messages=chat_history,
4 tools=[get_information_tool],
5)
6
7print(chat_response.choices[0].message.content)
Output:
1[TextChunk(text='The Nobel Peace Prize for 2024 was awarded to the Japan Confederation of A- and H-Bomb Sufferers Organizations (Nihon Hidankyo) for their activism against nuclear weapons, including efforts by survivors of the atomic bombings of Hiroshima and Nagasaki', type='text'), ReferenceChunk(reference_ids=[0], type='reference'), TextChunk(text='.', type='text')]
Extract and Print References
1from mistralai.models import TextChunk, ReferenceChunk
2
3refs_used = []
4
5# Print the main response and save each reference
6for chunk in chat_response.choices[0].message.content:
7 if isinstance(chunk, TextChunk):
8 print(chunk.text, end="")
9 elif isinstance(chunk, ReferenceChunk):
10 refs_used += chunk.reference_ids
11
12# Print references only
13if refs_used:
14 print("\n\nSources:")
15 for i, ref in enumerate(set(refs_used), 1):
16 reference = json.loads(result)[str(ref)]
17 print(f"\n{i}. {reference['title']}: {reference['url']}")
Output:
1The Nobel Peace Prize for 2024 was awarded to the Japan Confederation of A- and H-Bomb Sufferers Organizations (Nihon Hidankyo) for their activism against nuclear weapons, including efforts by survivors of the atomic bombings of Hiroshima and Nagasaki.
2
3Sources:
4
51. 2024 Nobel Peace Prize: https://en.wikipedia.org/wiki/2024_Nobel_Peace_Prize
Full Cookbook
You can find a comprehensive cookbook exploring Citations and References leveraging RAG with Wikipedia here.
This template will help get started with web search and document grounding with citations.