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 应用开发初学者,如有建议,可以留言,共同探讨学习。(:

### Java DeepSeek-R1 Library Information and Usage DeepSeek-R1 appears not directly related to standard Java libraries or commonly known frameworks based on available references. However, assuming DeepSeek-R1 is a specialized tool or library within certain contexts such as network programming or specific application development environments: For integrating or utilizing a hypothetical **Java DeepSeek-R1** library, one would typically follow these general guidelines for incorporating third-party libraries into Java projects. #### Incorporating Third-Party Libraries in Java Projects To include any external library like an assumed **DeepSeek-R1**, developers often add dependencies through build tools such as Maven or Gradle. Using Maven as an example: ```xml <dependency> <groupId>com.deepseek</groupId> <artifactId>deepseek-r1</artifactId> <version>1.0.0</version> </dependency> ``` Or with Gradle: ```groovy implementation 'com.deepseek:deepseek-r1:1.0.0' ``` These configurations assume the existence of a repository hosting the **DeepSeek-R1** artifact. If this library is proprietary or less common, direct downloads might be necessary followed by manual installation into local repositories[^1]. #### Example Code Snippet Demonstrating Potential Use Given the lack of detailed documentation about **DeepSeek-R1**, here’s a speculative approach demonstrating its potential invocation pattern similar to other Java-based APIs. ```java import com.deepseek.r1.DeepSeekR1; public class Main { public static void main(String[] args) { try (var deepSeekInstance = new DeepSeekR1()) { var result = deepSeekInstance.executeSomeFunctionality(); System.out.println("Result from DeepSeek-R1: " + result); } catch (Exception e) { e.printStackTrace(); } } } ``` The provided code assumes `executeSomeFunctionality` represents a method offered by the supposed **DeepSeek-R1** API which users interact with after initializing it properly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值