Java: DeepSeek R1 实现工具调用,利用ReAct + Spring AI 的实现思路

1、目前 DeepSeek-R1 不支持原生的工具调用

当前版本的 DeepSeek-R1 模型不支持 tool call 、结构化输出等,官方给出的回应是下一个大版本会支持,态度还是很积极的,但是具体时间未定。https://github.com/deepseek-ai/DeepSeek-R1/issues/9官方回应

2、根据 ReAct 思想,手动实现工具调用

如果有人和我一样,不愿意被动等待官方更新,可以考虑使用 ReAct 的思路,手动实现工具调用。框架使用 Spring-AI、LangChain4J 或者原生 API 调用都可以,主要说一下实现思路。

ReAct 提示词如下:

Respond to the human as helpfully and accurately as possible.

{
   
   {instruction}}

You have access to the following tools:

{
   
   {tools}}

Use a json blob to specify a tool by providing an action key (tool name) and an action_input key (tool input).
Valid "action" values: "Final Answer" or {
   
   {tool_names}}

Provide only ONE action per $JSON_BLOB, as shown:
###
{
"action": $TOOL_NAME,
"action_input": $ACTION_INPUT
}
###
Follow this format:

Question: input question to answer
Thought: consider previous and subsequent steps
Action:
###
$JSON_BLOB
###

Observation: action result
... (repeat Thought/Action/Observation N times)
Thought: I know what to respond
Action:
·``
{
"action": "Final Answer",
"action_input": "Final response to human"
}
·``

Begin! Reminder to ALWAYS respond with a valid json blob of a single action. Use tools if necessary. Respond directly if appropriate. Format is Action:###$JSON_BLOB###then Observation:.

其中:

实现思路

  1. 可以看到看的 ReAct 的 prompt 严格要求了只输出一个 Action,格式也是固定的,两个字段:
    • action: 需要采取的行动,一共两种情况,调用的工具名称/Final Answer。
    • action_input:要么是工具的参数,要么是最终的结果。
  2. 判断 action 的类型,如果是工具名称,则去执行相应的工具,这里也可以借用 Spring AI 的思路 https://docs.spring.io/spring-ai/reference/api/tools.html#_tool_execution
  3. 当工具调用完成之后,需要将调用结果整理为 assistant 消息,加上用户 continue 消息,作为历史 messages 进行二次调用。流程如下:在这里插入图片描述
  4. 循环, 直到 action 是 Final Answer 则直接输出结果。

附中文版 ReAct 提示词

尽可能帮助用户,并提供准确的回答。

{
   
   {instruction}}

你可以使用以下工具:

{
   
   {tools}}

通过使用 JSON 对象来指定工具,提供 action(工具名称)和 action_input(工具输入)键。
有效的 “action” 值有:“Final Answer” 或 {
   
   {tool_names}}。

每个 $JSON_BLOB 只能指定一个动作,格式如下:

###
{
"action": $TOOL_NAME,
"action_input": $ACTION_INPUT
}
###

遵循以下格式:

问题(Question):要回答的输入问题
思考(Thought):考虑之前和接下来的步骤
行动(Action):
###
$JSON_BLOB
###
观察(Observation):行动结果
… (重复“思考/行动/观察”多次,直至完成任务)
思考(Thought):我知道如何回答了
行动(Action):
###
{
"action": "Final Answer",
"action_input": "Final response to human"
}
###

开始!请务必始终以有效的 JSON 对象形式提供单个Action。 如果需要工具就使用工具。 如果适合,请直接回应用户。 格式为:###$JSON_BLOB###然后 Observation:.

本人也是 AI 应用开发初学者,如有建议,可以留言,共同探讨学习。(:

猜你喜欢

转载自blog.csdn.net/qq_33489819/article/details/145636728
今日推荐