跳到主要内容

引用和参考文献

引用使模型能够使其回复基于事实并提供参考文献,这使其成为检索增强生成 (RAG) 和智能体应用的强大功能。此功能允许模型提供从文档或工具调用中的数据块中提取信息的来源。

我们的模型经过深度训练,能够基于文档进行事实依据,并提供来源,具有提取参考文献和引用的内置功能。

代码示例

要向模型提供文档,您可以将来源作为函数调用响应包含进去。
下面是一个参考文献的示例,在本例中来自维基百科,使用了工具调用。

参考文献示例
{
"0": {
"url": "https://en.wikipedia.org/wiki/2024_Nobel_Peace_Prize",
"title": "2024 Nobel Peace Prize",
"snippets": [
[
"The 2024 Nobel Peace Prize, an international peace prize established according to Alfred Nobel's will, was awarded to Nihon Hidankyo (the Japan Confederation of A- and H-Bomb Sufferers Organizations), for their activism against nuclear weapons, assisted by victim/survivors (known as Hibakusha) of the atomic bombings of Hiroshima and Nagasaki in 1945.",
"They will receive the prize at a ceremony on 10 December 2024 at Oslo, Norway."
]
],
"description": null,
"date": "2024-11-26T17:39:55.057454",
"source": "wikipedia"
},
"1": {
"url": "https://en.wikipedia.org/wiki/Climate_Change",
"title": "Climate Change",
"snippets": [
[
"Present-day climate change includes both global warming—the ongoing increase in global average temperature—and its wider effects on Earth’s climate system. Climate change in a broader sense also includes previous long-term changes to Earth's climate. The current rise in global temperatures is driven by human activities, especially fossil fuel burning since the Industrial Revolution. Fossil fuel use, deforestation, and some agricultural and industrial practices release greenhouse gases. These gases absorb some of the heat that the Earth radiates after it warms from sunlight, warming the lower atmosphere. Carbon dioxide, the primary gas driving global warming, has increased in concentration by about 50% since the pre-industrial era to levels not seen for millions of years."
]
],
"description": null,
"date": "2024-11-26T17:39:55.057454",
"source": "wikipedia"
},
"2": {
"url": "https://en.wikipedia.org/wiki/Artificial_Intelligence",
"title": "Artificial Intelligence",
"snippets": [
[
"Artificial intelligence (AI) refers to the capability of computational systems to perform tasks typically associated with human intelligence, such as learning, reasoning, problem-solving, perception, and decision-making. It is a field of research in computer science that develops and studies methods and software that enable machines to perceive their environment and use learning and intelligence to take actions that maximize their chances of achieving defined goals. Such machines may be called AIs."
]
],
"description": null,
"date": "2024-11-26T17:39:55.057454",
"source": "wikipedia"
}
}

初始化客户端

import os
from mistralai import Mistral, ToolMessage
import json

api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"

client = Mistral(api_key=api_key)

定义工具

在本例中,我们将创建一个 get_information 工具,它将返回前面提到的参考文献。

get_information_tool = {
"type": "function",
"function": {
"name": "get_information",
"description": "Get information from external source.",
"parameters": {}
},
}

def get_information():
return json.dumps(references)

设置聊天历史记录

chat_history = [
{
"role": "system",
"content": "Answer the user by providing references to the source of the information."
},
{
"role": "user",
"content": "Who won the Nobel Prize in 2024?"
}
]

发起首次聊天请求

chat_response = client.chat.complete(
model=model,
messages=chat_history,
tools=[get_information_tool],
)

if hasattr(chat_response.choices[0].message, 'tool_calls'):
tool_call = chat_response.choices[0].message.tool_calls[0]
chat_history.append(chat_response.choices[0].message)
print(tool_call)
else:
print("No tool call found in the response")

输出

function=FunctionCall(name='get_information', arguments='{}') id='F4HiRgdZp' type=None index=0

处理工具调用并添加结果

result = get_information()

tool_call_result = ToolMessage(
content=result,
tool_call_id=tool_call.id,
name=tool_call.function.name,
)

# Append the tool call message to the chat_history
chat_history.append(tool_call_result)

发起最终聊天请求

chat_response = client.chat.complete(
model=model,
messages=chat_history,
tools=[get_information_tool],
)

print(chat_response.choices[0].message.content)

输出

[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')]

提取并打印参考文献

from mistralai.models import TextChunk, ReferenceChunk

refs_used = []

# Print the main response and save each reference
for chunk in chat_response.choices[0].message.content:
if isinstance(chunk, TextChunk):
print(chunk.text, end="")
elif isinstance(chunk, ReferenceChunk):
refs_used += chunk.reference_ids

# Print references only
if refs_used:
print("\n\nSources:")
for i, ref in enumerate(set(refs_used), 1):
reference = json.loads(result)[str(ref)]
print(f"\n{i}. {reference['title']}: {reference['url']}")

输出

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.

Sources:

1. 2024 Nobel Peace Prize: https://en.wikipedia.org/wiki/2024_Nobel_Peace_Prize

完整指南

您可以在此处找到一份详细的指南,探讨了如何结合维基百科利用 RAG 实现引用和参考文献功能。
此模板将帮助您开始使用网络搜索和带引用的文档事实依据功能。