多轮对话
This commit is contained in:
parent
5d06d1c264
commit
9ee7dcde3b
@ -19,4 +19,6 @@ public interface InvokeService {
|
|||||||
String getMsgBySpringAi(String question);
|
String getMsgBySpringAi(String question);
|
||||||
|
|
||||||
String getMsgBySpringAiOllam(String question);
|
String getMsgBySpringAiOllam(String question);
|
||||||
|
|
||||||
|
void moreMessages();
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package com.huangge1199.aiagent.Service.impl;
|
package com.huangge1199.aiagent.Service.impl;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import cn.hutool.http.HttpRequest;
|
import cn.hutool.http.HttpRequest;
|
||||||
import cn.hutool.http.HttpResponse;
|
import cn.hutool.http.HttpResponse;
|
||||||
@ -16,6 +18,9 @@ import com.alibaba.dashscope.exception.NoApiKeyException;
|
|||||||
import com.huangge1199.aiagent.Service.InvokeService;
|
import com.huangge1199.aiagent.Service.InvokeService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.ai.chat.messages.AssistantMessage;
|
import org.springframework.ai.chat.messages.AssistantMessage;
|
||||||
|
import org.springframework.ai.chat.messages.SystemMessage;
|
||||||
|
import org.springframework.ai.chat.messages.UserMessage;
|
||||||
|
import org.springframework.ai.chat.model.ChatResponse;
|
||||||
import org.springframework.ai.chat.prompt.Prompt;
|
import org.springframework.ai.chat.prompt.Prompt;
|
||||||
import org.springframework.ai.ollama.OllamaChatModel;
|
import org.springframework.ai.ollama.OllamaChatModel;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
@ -121,4 +126,42 @@ public class InvokeServiceImpl implements InvokeService {
|
|||||||
.getOutput();
|
.getOutput();
|
||||||
return output.getText();
|
return output.getText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void moreMessages() {
|
||||||
|
|
||||||
|
List<org.springframework.ai.chat.messages.Message> messages = new ArrayList<>();
|
||||||
|
|
||||||
|
//第零轮对话
|
||||||
|
messages.add(new SystemMessage("你是一个旅游规划师"));
|
||||||
|
messages.add(new UserMessage("你是谁?"));
|
||||||
|
ChatResponse response = ollamaChatModel.call(new Prompt(messages));
|
||||||
|
String content = response.getResult().getOutput().getText();
|
||||||
|
System.out.printf("第零轮: %s\n", content);
|
||||||
|
|
||||||
|
messages.add(new AssistantMessage(content));
|
||||||
|
|
||||||
|
//第一轮对话
|
||||||
|
messages.add(new UserMessage("我想去新疆"));
|
||||||
|
response = ollamaChatModel.call(new Prompt(messages));
|
||||||
|
content = response.getResult().getOutput().getText();
|
||||||
|
System.out.printf("第一轮: %s\n", content);
|
||||||
|
|
||||||
|
messages.add(new AssistantMessage(content));
|
||||||
|
|
||||||
|
//第二轮对话
|
||||||
|
messages.add(new UserMessage("能帮我推荐一些旅游景点吗?"));
|
||||||
|
response = ollamaChatModel.call(new Prompt(messages));
|
||||||
|
content = response.getResult().getOutput().getText();
|
||||||
|
System.out.printf("第二轮: %s\n", content);
|
||||||
|
|
||||||
|
messages.add(new AssistantMessage(content));
|
||||||
|
|
||||||
|
//第三轮对话
|
||||||
|
messages.add(new UserMessage("那里这两天的天气如何?"));
|
||||||
|
response = ollamaChatModel.call(new Prompt(messages));
|
||||||
|
content = response.getResult().getOutput().getText();
|
||||||
|
|
||||||
|
System.out.printf("第三轮: %s\n", content);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -71,4 +71,10 @@ public class InvokeController {
|
|||||||
return R.fail(e.getMessage());
|
return R.fail(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/moreMessages")
|
||||||
|
@Operation(summary = "多轮对话")
|
||||||
|
public void moreMessages() {
|
||||||
|
invokeService.moreMessages();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,7 @@ spring:
|
|||||||
ollama:
|
ollama:
|
||||||
base-url: http://192.168.188.2:11435
|
base-url: http://192.168.188.2:11435
|
||||||
chat:
|
chat:
|
||||||
model: gemma3:1b
|
model: DeepSeek-R1-Distill-Qwen-1.5B:latest
|
||||||
server:
|
server:
|
||||||
port: 8123
|
port: 8123
|
||||||
servlet:
|
servlet:
|
||||||
@ -32,4 +32,13 @@ springdoc:
|
|||||||
knife4j:
|
knife4j:
|
||||||
enable: true
|
enable: true
|
||||||
setting:
|
setting:
|
||||||
language: zh_cn
|
language: zh_cn
|
||||||
|
|
||||||
|
logging:
|
||||||
|
level:
|
||||||
|
org:
|
||||||
|
springframework:
|
||||||
|
ai:
|
||||||
|
chat:
|
||||||
|
client:
|
||||||
|
advisor: DEBUG
|
||||||
|
Loading…
Reference in New Issue
Block a user