parent
60c38614e4
commit
115982fe1e
@ -0,0 +1,12 @@
|
||||
package xyz.wbsite.ai;
|
||||
|
||||
/**
|
||||
* 最简单的聊天示例
|
||||
*/
|
||||
public class Base_Chat_Example {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String chat = Helper.getChatModel().chat("你是谁");
|
||||
System.out.println(chat);
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package xyz.wbsite.ai;
|
||||
|
||||
import cn.hutool.core.thread.ThreadUtil;
|
||||
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
||||
import dev.langchain4j.service.AiServices;
|
||||
import dev.langchain4j.service.TokenStream;
|
||||
|
||||
/**
|
||||
* 简单的流式聊天示例
|
||||
*/
|
||||
public class Easy_StreamChat_Example {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
String question = "你是谁?";
|
||||
|
||||
Assistant assistant = AiServices.builder(Assistant.class)
|
||||
.streamingChatLanguageModel(Helper.getStreamChatModel())
|
||||
.chatMemory(MessageWindowChatMemory.withMaxMessages(10))
|
||||
.build();
|
||||
|
||||
assistant.chat(question)
|
||||
.onPartialResponse(System.out::print)
|
||||
.onCompleteResponse(chatResponse -> {
|
||||
System.out.println();
|
||||
System.out.println("onComplete");
|
||||
})
|
||||
.ignoreErrors()
|
||||
.start();
|
||||
|
||||
ThreadUtil.sleep(10 * 1000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 助手
|
||||
*/
|
||||
interface Assistant {
|
||||
|
||||
TokenStream chat(String userMessage);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package xyz.wbsite.ai;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import dev.langchain4j.data.document.Document;
|
||||
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Helper {
|
||||
|
||||
private static OpenAiStreamingChatModel openAiStreamingChatModel = OpenAiStreamingChatModel.builder()
|
||||
.baseUrl("http://36.138.207.178:11434/v1")
|
||||
.apiKey("1")
|
||||
.modelName("deepseek-r1:14B")
|
||||
.logRequests(true)
|
||||
.logResponses(true)
|
||||
.build();
|
||||
|
||||
private static OpenAiChatModel openAiChatModel = OpenAiChatModel.builder()
|
||||
.baseUrl("http://36.138.207.178:11434/v1")
|
||||
.apiKey("1")
|
||||
.modelName("deepseek-r1:14B")
|
||||
.logRequests(true)
|
||||
.logResponses(true)
|
||||
.build();
|
||||
|
||||
private static OpenAiChatModel toolChatModel = OpenAiChatModel.builder()
|
||||
.baseUrl("http://36.138.207.178:11434/v1")
|
||||
.apiKey("1")
|
||||
.modelName("qwen2.5:7b")
|
||||
.build();
|
||||
|
||||
public static OpenAiStreamingChatModel getStreamChatModel() {
|
||||
return openAiStreamingChatModel;
|
||||
}
|
||||
|
||||
public static OpenAiChatModel getChatModel() {
|
||||
return openAiChatModel;
|
||||
}
|
||||
|
||||
public static OpenAiChatModel getToolChatModel() {
|
||||
return toolChatModel;
|
||||
}
|
||||
|
||||
public static Document getDocument() {
|
||||
return Document.from("人往往在做梦的时候会打呼噜");
|
||||
}
|
||||
|
||||
public static Document getDocument(String text) {
|
||||
return Document.from(text);
|
||||
}
|
||||
|
||||
public static List<Document> getDocuments() {
|
||||
return CollUtil.newArrayList(
|
||||
Document.from("人往往在做梦的时候会打呼噜"),
|
||||
Document.from("小猪在睡觉的时候会扭屁股"),
|
||||
Document.from("有一只蟑螂在床底下跳舞"),
|
||||
Document.from("小狗在睡觉的时候会磨牙"),
|
||||
Document.from("我家的小鸡喜欢吃虫子")
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package xyz.wbsite.ai;
|
||||
|
||||
import dev.langchain4j.data.document.splitter.DocumentSplitters;
|
||||
import dev.langchain4j.data.segment.TextSegment;
|
||||
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
||||
import dev.langchain4j.model.embedding.EmbeddingModel;
|
||||
import dev.langchain4j.model.embedding.onnx.bgesmallenv15q.BgeSmallEnV15QuantizedEmbeddingModel;
|
||||
import dev.langchain4j.rag.DefaultRetrievalAugmentor;
|
||||
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
|
||||
import dev.langchain4j.rag.query.transformer.CompressingQueryTransformer;
|
||||
import dev.langchain4j.service.AiServices;
|
||||
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
|
||||
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
|
||||
|
||||
/**
|
||||
* 对上下文对话进行查询压缩
|
||||
*/
|
||||
public class Query_Compression_Example {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
EmbeddingModel embeddingModel = new BgeSmallEnV15QuantizedEmbeddingModel();
|
||||
|
||||
InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
|
||||
|
||||
EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
|
||||
.documentSplitter(DocumentSplitters.recursive(300, 0))
|
||||
.embeddingStore(embeddingStore)
|
||||
.embeddingModel(embeddingModel)
|
||||
.build();
|
||||
|
||||
ingestor.ingest(Helper.getDocument());
|
||||
|
||||
EmbeddingStoreContentRetriever retriever = EmbeddingStoreContentRetriever.builder()
|
||||
.embeddingModel(embeddingModel)
|
||||
.embeddingStore(embeddingStore)
|
||||
.maxResults(2)
|
||||
.minScore(0.5)
|
||||
.build();
|
||||
|
||||
CompressingQueryTransformer queryTransformer = new CompressingQueryTransformer(Helper.getChatModel());
|
||||
// // 其他实现
|
||||
// QueryTransformer queryTransformer = new QueryTransformer() {
|
||||
// @Override
|
||||
// public Collection<Query> transform(Query query) {
|
||||
// return CollUtil.newArrayList(new Query(query.text().toLowerCase()));
|
||||
// }
|
||||
// };
|
||||
|
||||
DefaultRetrievalAugmentor retrievalAugmentor = DefaultRetrievalAugmentor.builder()
|
||||
.queryTransformer(queryTransformer)
|
||||
.contentRetriever(retriever)
|
||||
.build();
|
||||
|
||||
Assistant assistant = AiServices.builder(Assistant.class)
|
||||
.chatLanguageModel(Helper.getChatModel())
|
||||
.retrievalAugmentor(retrievalAugmentor)
|
||||
.chatMemory(MessageWindowChatMemory.withMaxMessages(4))
|
||||
.build();
|
||||
|
||||
String chat = assistant.chat("人在睡觉时会做什么了?");
|
||||
System.out.println(chat);
|
||||
|
||||
String chat1 = assistant.chat("小猪在睡觉的时候会什么了?");
|
||||
System.out.println(chat1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 助手
|
||||
*/
|
||||
interface Assistant {
|
||||
|
||||
String chat(String userMessage);
|
||||
}
|
||||
}
|
@ -1,122 +0,0 @@
|
||||
package xyz.wbsite.ai;
|
||||
|
||||
import dev.langchain4j.data.document.Document;
|
||||
import dev.langchain4j.data.document.DocumentParser;
|
||||
import dev.langchain4j.data.document.DocumentSplitter;
|
||||
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
|
||||
import dev.langchain4j.data.document.parser.TextDocumentParser;
|
||||
import dev.langchain4j.data.document.splitter.DocumentSplitters;
|
||||
import dev.langchain4j.data.embedding.Embedding;
|
||||
import dev.langchain4j.data.message.ChatMessage;
|
||||
import dev.langchain4j.data.segment.TextSegment;
|
||||
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
|
||||
import dev.langchain4j.model.embedding.EmbeddingModel;
|
||||
import dev.langchain4j.model.embedding.onnx.bgesmallenv15q.BgeSmallEnV15QuantizedEmbeddingModel;
|
||||
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
|
||||
import dev.langchain4j.rag.content.Content;
|
||||
import dev.langchain4j.rag.content.retriever.ContentRetriever;
|
||||
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
|
||||
import dev.langchain4j.rag.query.Query;
|
||||
import dev.langchain4j.service.AiServices;
|
||||
import dev.langchain4j.service.TokenStream;
|
||||
import dev.langchain4j.store.embedding.EmbeddingStore;
|
||||
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static dev.langchain4j.data.document.loader.FileSystemDocumentLoader.loadDocument;
|
||||
|
||||
/**
|
||||
* 主函数入口
|
||||
*/
|
||||
public class TestRagNativeChat {
|
||||
|
||||
public static void main(String[] args) {
|
||||
OpenAiChatModel model = OpenAiChatModel.builder()
|
||||
.baseUrl("http://36.138.207.178:11434/v1")
|
||||
.apiKey("1")
|
||||
.modelName("deepseek-r1:14B")
|
||||
.build();
|
||||
|
||||
|
||||
|
||||
// Now, let's load a document that we want to use for RAG.
|
||||
// We will use the terms of use from an imaginary car rental company, "Miles of Smiles".
|
||||
// For this example, we'll import only a single document, but you can load as many as you need.
|
||||
// LangChain4j offers built-in support for loading documents from various sources:
|
||||
// File System, URL, Amazon S3, Azure Blob Storage, GitHub, Tencent COS.
|
||||
// Additionally, LangChain4j supports parsing multiple document types:
|
||||
// text, pdf, doc, xls, ppt.
|
||||
// However, you can also manually import your data from other sources.
|
||||
DocumentParser documentParser = new TextDocumentParser();
|
||||
Document document = loadDocument("D:\\docs\\人才公共服务平台会议纪要20210720.txt", documentParser);
|
||||
|
||||
|
||||
// Now, we need to split this document into smaller segments, also known as "chunks."
|
||||
// This approach allows us to send only relevant segments to the LLM in response to a user query,
|
||||
// rather than the entire document. For instance, if a user asks about cancellation policies,
|
||||
// we will identify and send only those segments related to cancellation.
|
||||
// A good starting point is to use a recursive document splitter that initially attempts
|
||||
// to split by paragraphs. If a paragraph is too large to fit into a single segment,
|
||||
// the splitter will recursively divide it by newlines, then by sentences, and finally by words,
|
||||
// if necessary, to ensure each piece of text fits into a single segment.
|
||||
DocumentSplitter splitter = DocumentSplitters.recursive(300, 0);
|
||||
List<TextSegment> segments = splitter.split(document);
|
||||
|
||||
|
||||
// Now, we need to embed (also known as "vectorize") these segments.
|
||||
// Embedding is needed for performing similarity searches.
|
||||
// For this example, we'll use a local in-process embedding model, but you can choose any supported model.
|
||||
// Langchain4j currently supports more than 10 popular embedding model providers.
|
||||
EmbeddingModel embeddingModel = new BgeSmallEnV15QuantizedEmbeddingModel();
|
||||
List<Embedding> embeddings = embeddingModel.embedAll(segments).content();
|
||||
|
||||
|
||||
// Next, we will store these embeddings in an embedding store (also known as a "vector database").
|
||||
// This store will be used to search for relevant segments during each interaction with the LLM.
|
||||
// For simplicity, this example uses an in-memory embedding store, but you can choose from any supported store.
|
||||
// Langchain4j currently supports more than 15 popular embedding stores.
|
||||
EmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();
|
||||
embeddingStore.addAll(embeddings, segments);
|
||||
|
||||
// We could also use EmbeddingStoreIngestor to hide manual steps above behind a simpler API.
|
||||
// See an example of using EmbeddingStoreIngestor in _01_Advanced_RAG_with_Query_Compression_Example.
|
||||
|
||||
|
||||
// The content retriever is responsible for retrieving relevant content based on a user query.
|
||||
// Currently, it is capable of retrieving text segments, but future enhancements will include support for
|
||||
// additional modalities like images, audio, and more.
|
||||
ContentRetriever contentRetriever = EmbeddingStoreContentRetriever.builder()
|
||||
.embeddingStore(embeddingStore)
|
||||
.embeddingModel(embeddingModel)
|
||||
.maxResults(2) // on each interaction we will retrieve the 2 most relevant segments
|
||||
.minScore(0.1) // we want to retrieve segments at least somewhat similar to user query
|
||||
.build();
|
||||
|
||||
List<Content> retrieve = contentRetriever.retrieve(new Query("参会人员有哪些人?"));
|
||||
System.out.println();
|
||||
|
||||
Assistant assistant = AiServices.builder(Assistant.class)
|
||||
.chatLanguageModel(model)
|
||||
.chatMemory(MessageWindowChatMemory.withMaxMessages(10))
|
||||
.contentRetriever(contentRetriever)
|
||||
.build();
|
||||
|
||||
// String chat = assistant.chat("参会人员有哪些人?");
|
||||
//System.out.println(chat);
|
||||
}
|
||||
|
||||
|
||||
// 创建一个助手接口
|
||||
interface Assistant {
|
||||
|
||||
String chat(String userMessage);
|
||||
|
||||
TokenStream chatStream(List<ChatMessage> messages);
|
||||
|
||||
TokenStream chatStream(ChatMessage message);
|
||||
|
||||
TokenStream chatStream(String message);
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package xyz.wbsite.ai;
|
||||
|
||||
import dev.langchain4j.data.message.ChatMessage;
|
||||
import dev.langchain4j.data.message.UserMessage;
|
||||
import dev.langchain4j.model.chat.request.ChatRequest;
|
||||
import dev.langchain4j.model.chat.response.ChatResponse;
|
||||
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||
|
||||
/**
|
||||
* 主函数入口
|
||||
*/
|
||||
public class TestSimpleChat {
|
||||
|
||||
public static void main(String[] args) {
|
||||
OpenAiChatModel model = OpenAiChatModel.builder()
|
||||
.baseUrl("http://36.138.207.178:11434/v1")
|
||||
.apiKey("1")
|
||||
.modelName("deepseek-r1:14B")
|
||||
.build();
|
||||
|
||||
String generate = model.chat("你好");
|
||||
System.out.println(generate);
|
||||
|
||||
ChatRequest chatRequest = ChatRequest.builder()
|
||||
.messages(new ChatMessage[]{
|
||||
UserMessage.from("你是谁")
|
||||
})
|
||||
.build();
|
||||
|
||||
ChatResponse chatResponse = model.chat(chatRequest);
|
||||
System.out.println(chatResponse.aiMessage().text());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue