*/@Testvoidofficial(){OpenAiChatModel model =OpenAiChatModel.builder().baseUrl("http://langchain4j.dev/demo/openai/v1").apiKey("demo").modelName("gpt-4o-mini").build();String answer = model.chat("你是谁?");System.out.println(answer);}
复制代码
运行后结果
(, 下载次数: 0)
上传
点击文件名下载附件
使用通义千问(qwen-plus)
/**
* 接入通义千问(qwen-plus)
*/@TestvoidgetApiKey(){//获取环境变量配置的API-KEYString key =System.getenv("API-KEY");OpenAiChatModel model =OpenAiChatModel.builder().baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")//基地址.apiKey(key)//密钥.modelName("qwen-plus")//使用模型.build();//构建//调用chat方法进行交互String mes = model.chat("你是谁?");System.out.println(mes);}
OpenAiChatModel model =OpenAiChatModel.builder().baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")//基地址.apiKey(key)//密钥.modelName("qwen-plus")//使用模型.logRequests(true)//请求.logResponses(true)//响应.build();//构建
//构建向量数据库操作对象@BeanpublicEmbeddingStorestore(){//加载文档中的内容进入内存List<Document> text =ClassPathDocumentLoader.loadDocuments("text");//构建向量数据库操作对象InMemoryEmbeddingStore store =newInMemoryEmbeddingStore();//构建一个EmbeddingStoreIngestor对象 完成数据切割、向量化、存储EmbeddingStoreIngestor build =EmbeddingStoreIngestor.builder().embeddingStore(store).build();
List<Document> text =FileSystemDocumentLoader.loadDocuments("C:\\Users\\Xiri\\Desktop\\Learn_LangChain4j\\LangChain-RAG\\src\\main\\resources\\text");
复制代码
2.类路径(classpath)加载文档
指定类路径下的文件路径(相对 resources 目录的路径)
List<Document> text =ClassPathDocumentLoader.loadDocuments("text");
复制代码
3. URL 加载器(网络加载)
//网络链接加载//湖北省2025年普通高校招生录取控制分数线 网址Document text =UrlDocumentLoader.load("https://jyt.hubei.gov.cn/zfxxgk/zc_GK2020/qtzdgkwj_GK2020/202506/t20250626_5706550.shtml",newApacheTikaDocumentParser());
Document text =FileSystemDocumentLoader.loadDocument("C:\\Users\\Xiri\\Desktop\\Learn_LangChain4j\\LangChain-RAG\\src\\main\\resources\\text\\2025hbgk.pdf",newApachePdfBoxDocumentParser());
//构建向量数据库操作对象@BeanpublicEmbeddingStorestore()//名字配置为store,如果配置名为embeddingStore会重名,有一个默认的{//使用解析器Document text =FileSystemDocumentLoader.loadDocument("C:\\Users\\Xiri\\Desktop\\Learn_LangChain4j\\LangChain-RAG\\src\\main\\resources\\text\\2025hbgk.pdf",newApachePdfBoxDocumentParser());//构建向量数据库操作对象InMemoryEmbeddingStore store =newInMemoryEmbeddingStore();//构建一个EmbeddingStoreIngestor对象 完成数据切割、向量化、存储EmbeddingStoreIngestor build =EmbeddingStoreIngestor.builder().embeddingStore(store).build();
@BeanpublicEmbeddingStorestore()//名字配置为store,如果配置名为embeddingStore会重名,有一个默认的{Document text =FileSystemDocumentLoader.loadDocument("C:\\Users\\Xiri\\Desktop\\Learn_LangChain4j\\LangChain-RAG\\src\\main\\resources\\text\\2025hbgk.pdf",newApachePdfBoxDocumentParser());//构建文档分割器对象DocumentSplitter recursive =DocumentSplitters.recursive(500,//每个片段最大能容纳的字符100//两个片段之间重叠字符个数);//构建向量数据库操作对象InMemoryEmbeddingStore store =newInMemoryEmbeddingStore();//构建一个EmbeddingStoreIngestor对象 完成数据切割、向量化、存储EmbeddingStoreIngestor build =EmbeddingStoreIngestor.builder().embeddingStore(store).documentSplitter(recursive)//设置文档分割器.build();
//构建向量数据库操作对象@BeanpublicEmbeddingStorestore()//名字配置为store,如果配置名为embeddingStore会重名,有一个默认的{Document text =FileSystemDocumentLoader.loadDocument("C:\\Users\\Xiri\\Desktop\\Learn_LangChain4j\\LangChain-RAG\\src\\main\\resources\\text\\2025hbgk.pdf",newApachePdfBoxDocumentParser());//构建文档分割器对象DocumentSplitter recursive =DocumentSplitters.recursive(500,//每个片段最大能容纳的字符100//两个片段之间重叠字符个数);//构建向量数据库操作对象InMemoryEmbeddingStore store =newInMemoryEmbeddingStore();//构建一个EmbeddingStoreIngestor对象 完成数据切割、向量化、存储EmbeddingStoreIngestor build =EmbeddingStoreIngestor.builder().embeddingStore(store).documentSplitter(recursive)//设置文档分割器.embeddingModel(embeddingModel)//设置向量模型.build();