更新 SpringAI/0_使用SpringAI接入AI模型.md
This commit is contained in:
parent
981599bd21
commit
8bbefca044
@ -150,7 +150,7 @@ spring:
|
|||||||
private final ChatModel chatModel;
|
private final ChatModel chatModel;
|
||||||
```
|
```
|
||||||
|
|
||||||
2. 非流式问答
|
2. 同步返回答案
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@GetMapping("chat")
|
@GetMapping("chat")
|
||||||
@ -163,11 +163,11 @@ public String chat(@RequestParam String prompt) {// 用户输入的prompt
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
3. 流式问答
|
3. 流式返回答案
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@GetMapping(value = "chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
@GetMapping(value = "chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||||
public Flux<ServerSentEvent<String>> chatStream(@RequestParam String prompt) {
|
public Flux<ServerSentEvent<String>> chat(@RequestParam String prompt) {
|
||||||
return ChatClient.create(chatModel)
|
return ChatClient.create(chatModel)
|
||||||
.prompt()
|
.prompt()
|
||||||
.user(prompt)
|
.user(prompt)
|
||||||
@ -177,3 +177,26 @@ public String chat(@RequestParam String prompt) {// 用户输入的prompt
|
|||||||
.map(chatResponse -> ServerSentEvent.builder(JSONUtil.toJsonStr(chatResponse)).event("message").build());
|
.map(chatResponse -> ServerSentEvent.builder(JSONUtil.toJsonStr(chatResponse)).event("message").build());
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### ChatMermory
|
||||||
|
|
||||||
|
ChatMermory是一个记录与用户对话的组件,在聊天的模型中将用户与大模型API前几轮对话消息,发送给大模型的API是一个很常见的需求。它本身是一个接口,比如**InMemoryChatMemory**就是一个在JVM内存中记录的实现。可以按照自己的需求实现不同形式的存储,比如Redis、或数据库持久化存储。
|
||||||
|
|
||||||
|
### MessageChatMemoryAdvisor
|
||||||
|
|
||||||
|
ChatMermory仅仅是一个存储和获取历史对话消息的接口,而MessageChatMemoryAdvisor则是ChatClient中的一部分,比如这样做:
|
||||||
|
|
||||||
|
```java
|
||||||
|
// 这里用InMemoryChatMemory做示例
|
||||||
|
private static final ChatMemory chatMemory = new InMemoryChatMemory();
|
||||||
|
// 从历史记录里取6条对话消息一起发送至模型的API。
|
||||||
|
// 历史消息也是算在这一次对话Token消耗的,要关注Token膨胀的问题
|
||||||
|
var messageChatMemoryAdvisor = new MessageChatMemoryAdvisor(chatMemory, sessionId, 6);
|
||||||
|
ChatClient.create(chatModel)
|
||||||
|
.prompt()
|
||||||
|
.user(prompt)
|
||||||
|
.advisors(messageChatMemoryAdvisor)
|
||||||
|
.stream()
|
||||||
|
.content()
|
||||||
|
.map(chatResponse -> ServerSentEvent.builder(chatResponse).event("message").build());
|
||||||
|
```
|
Loading…
Reference in New Issue
Block a user