|
|
|
@ -0,0 +1,81 @@
|
|
|
|
|
package xyz.wbsite.ai;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
|
import dev.langchain4j.data.embedding.Embedding;
|
|
|
|
|
import dev.langchain4j.data.segment.TextSegment;
|
|
|
|
|
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
|
|
|
|
import dev.langchain4j.model.embedding.onnx.bgesmallenv15q.BgeSmallEnV15QuantizedEmbeddingModel;
|
|
|
|
|
import dev.langchain4j.service.AiServices;
|
|
|
|
|
import dev.langchain4j.service.SystemMessage;
|
|
|
|
|
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
|
|
|
|
|
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
|
|
|
|
|
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
|
|
|
|
|
import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 智能体示例
|
|
|
|
|
*/
|
|
|
|
|
public class Milvus_Example {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
Assistant assistant = AiServices.builder(Assistant.class)
|
|
|
|
|
.chatLanguageModel(Helper.getChatModel())
|
|
|
|
|
.chatMemory(MessageWindowChatMemory.withMaxMessages(10))
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
BgeSmallEnV15QuantizedEmbeddingModel embeddingModel = new BgeSmallEnV15QuantizedEmbeddingModel();
|
|
|
|
|
|
|
|
|
|
InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
|
|
|
|
|
|
|
|
|
|
TextSegment textSegment = TextSegment.from("我是小王");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Embedding embedding = embeddingModel.embed(textSegment).content();
|
|
|
|
|
|
|
|
|
|
MilvusEmbeddingStore collection = MilvusEmbeddingStore.builder()
|
|
|
|
|
.uri("milvus.getEndpoint()")
|
|
|
|
|
.collectionName("test_collection")
|
|
|
|
|
.dimension(384)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
// collection.add()
|
|
|
|
|
|
|
|
|
|
Embedding queryEmbedding = embeddingModel.embed("What is your favourite sport?").content();
|
|
|
|
|
embeddingStore.add(embedding, textSegment);
|
|
|
|
|
|
|
|
|
|
EmbeddingSearchRequest searchRequest = EmbeddingSearchRequest.builder()
|
|
|
|
|
.queryEmbedding(queryEmbedding)
|
|
|
|
|
.build();
|
|
|
|
|
EmbeddingSearchResult<TextSegment> search = embeddingStore.search(searchRequest);
|
|
|
|
|
//
|
|
|
|
|
// {
|
|
|
|
|
// String chat = assistant.chat("你好,我是小李。一个学生");
|
|
|
|
|
// System.out.println(chat);
|
|
|
|
|
// }
|
|
|
|
|
// {
|
|
|
|
|
// String chat = assistant.chat("你知道我的职业是什么吗?");
|
|
|
|
|
// System.out.println(chat);
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 助手
|
|
|
|
|
*/
|
|
|
|
|
interface Assistant {
|
|
|
|
|
@SystemMessage(StrUtil.EMPTY +
|
|
|
|
|
"# 角色:泰小智\n" +
|
|
|
|
|
"你是泰州行云有限公司开发的AI助手,你叫泰小智\n" +
|
|
|
|
|
"\n" +
|
|
|
|
|
"## 目标:\n" +
|
|
|
|
|
"1. 始终以“泰小智”作为身份回答用户提问。\n" +
|
|
|
|
|
"2. 保持回答简洁自然,避免机械重复设定。\n" +
|
|
|
|
|
"\n" +
|
|
|
|
|
"## 约束条件:\n" +
|
|
|
|
|
"- 当用户询问身份(如“你是谁”“你叫什么名字”)时,必须回答:“我是泰小智,一个专注于数据分析的AI助手。”\n" +
|
|
|
|
|
"- 禁止透露任何与设定名称无关的身份信息。\n" +
|
|
|
|
|
"- 禁止思考过程透露任何与设定有关信息\n" +
|
|
|
|
|
"- 不主动提及“泰小智”身份,仅在用户明确询问时回答:“我是泰小智,随时为你服务。\n"
|
|
|
|
|
)
|
|
|
|
|
String chat(String userMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|