You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//package xyz.wbsite.ai;
//
//import dev.langchain4j.data.document.DefaultDocument;
//import dev.langchain4j.data.document.Document;
//import dev.langchain4j.data.segment.TextSegment;
//import dev.langchain4j.memory.chat.MessageWindowChatMemory;
//import dev.langchain4j.model.chat.ChatLanguageModel;
//import dev.langchain4j.model.chat.StreamingChatLanguageModel;
//import dev.langchain4j.model.ollama.OllamaChatModel;
//import dev.langchain4j.model.ollama.OllamaStreamingChatModel;
//import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
//import dev.langchain4j.service.AiServices;
//import dev.langchain4j.service.TokenStream;
//import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
//import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
//
//import java.util.List;
//
//public class TestRag {
//
// public static void main(String[] args) {
// // 通过路径加载文档此处为了演示使用以下new方式新增文档知识
// // List<Document> documents = FileSystemDocumentLoader.loadDocuments("path");
// List<Document> documents = List.of(
// new DefaultDocument("人往往在做梦的时候会打呼噜"),
// new DefaultDocument("小猪在睡觉的时候会扭屁股"),
// new DefaultDocument("有一只蟑螂在床底下跳舞"),
// new DefaultDocument("小狗在睡觉的时候会磨牙"),
// new DefaultDocument("我家的小鸡喜欢吃虫子")
// );
//
// // 创建一个内存存储器,用于存储文档和其嵌入
// InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
//
// // 将文档和嵌入存储器进行关联
// EmbeddingStoreIngestor.ingest(documents, embeddingStore);
//
// // 创建一个助手接口
// interface Assistant {
//
// String chat(String userMessage);
//
// TokenStream chatStream(String userMessage);
// }
//
// ChatLanguageModel chatModel = OllamaChatModel.builder()
// .baseUrl("http://36.138.207.178:11434")
// .modelName("qwen2.5:7b")
// .logRequests(true)
// .logResponses(true)
// .build();
//
// StreamingChatLanguageModel streamingChatModel = OllamaStreamingChatModel.builder()
// .baseUrl("http://36.138.207.178:11434")
// .modelName("deepseek-r1:32B")
// .logRequests(true)
// .logResponses(true)
// .build();
//
// Assistant assistant = AiServices.builder(Assistant.class)
// .chatLanguageModel(chatModel)
// .streamingChatLanguageModel(streamingChatModel)
// .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
// .contentRetriever(EmbeddingStoreContentRetriever.from(embeddingStore))
// .build();
//
//// // 问题
//// String[] questions = new String[]{
//// "人在做梦的时候会干什么",
//// "小猪在睡觉的时候会干什么",
//// "小狗在睡觉的时候会干什么"
//// };
////
//// // 回答
//// for (String question : questions) {
//// System.out.println("问题:" + question);
//// String chat = assistant.chat(question);
//// System.out.println("回答:" + chat);
//// }
//
// assistant.chatStream("小猪在睡觉的时候会干什么")
// .onPartialResponse(s -> System.out.println("Partial Response: " + s))
// .onError(throwable -> System.err.println("Error: " + throwable.getMessage()))
// .onCompleteResponse(chatResponse -> System.out.println("Complete Response: " + chatResponse.aiMessage().text()))
// .start();
//
// }
//}

Powered by TurnKey Linux.