Initial commit
This commit is contained in:
67
langchain4j-ai-pinecone/pom.xml
Normal file
67
langchain4j-ai-pinecone/pom.xml
Normal file
@@ -0,0 +1,67 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iwe3</groupId>
|
||||
<artifactId>langchain4j-ai-java</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>langchain4j-ai-pinecone</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-pinecone</artifactId>
|
||||
</dependency>
|
||||
<!-- 接入阿里云百炼平台 -->
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-community-dashscope-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<!-- 简单的rag实现 -->
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-easy-rag</artifactId>
|
||||
</dependency>
|
||||
<!-- 解析pdf文档 -->
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-document-parser-apache-pdfbox</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<!--导入低阶依赖-->
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-open-ai-spring-boot-starter</artifactId>
|
||||
<version>1.9.1-beta17</version>
|
||||
</dependency>
|
||||
<!--导入高阶依赖-->
|
||||
<dependency>
|
||||
<groupId>dev.langchain4j</groupId>
|
||||
<artifactId>langchain4j-spring-boot-starter</artifactId>
|
||||
<version>1.9.1-beta17</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.iwe3.langchain4j;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class PineConeApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(PineConeApplication.class,args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.iwe3.langchain4j.config;
|
||||
|
||||
import dev.langchain4j.data.segment.TextSegment;
|
||||
import dev.langchain4j.model.embedding.EmbeddingModel;
|
||||
import dev.langchain4j.store.embedding.EmbeddingStore;
|
||||
import dev.langchain4j.store.embedding.pinecone.PineconeEmbeddingStore;
|
||||
import dev.langchain4j.store.embedding.pinecone.PineconeServerlessIndexConfig;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class EmbeddingStoreConfig {
|
||||
|
||||
@Autowired
|
||||
private EmbeddingModel embeddingModel;
|
||||
|
||||
@Bean
|
||||
public EmbeddingStore<TextSegment> embeddingStore() {
|
||||
// 创建向量存储
|
||||
return PineconeEmbeddingStore.builder()
|
||||
.apiKey(System.getenv("PINECONE_API_KEY"))
|
||||
.index("xiaoai-index") // 如果指定的索引不存在,将创建一个新的索引
|
||||
.nameSpace("xiaoai-namespace") // 如果指定的名称空间不存在,将创建一个新的名称空间
|
||||
.createIndex(PineconeServerlessIndexConfig.builder()
|
||||
.cloud("AWS") // 指定索引部署在 AWS 云服务上。
|
||||
.region("us-east-1") // 指定索引所在的 AWS 区域为 us-east-1。
|
||||
.dimension(embeddingModel.dimension()) // 指定索引的向量维度,该维度与 embeddingModel 生成的向量维度相同。
|
||||
.build())
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.iwe3.langchain4j.config;
|
||||
|
||||
import dev.langchain4j.model.chat.ChatModel;
|
||||
import dev.langchain4j.model.openai.OpenAiChatModel;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class LLMConfig {
|
||||
|
||||
/**
|
||||
* @Description: 普通对话接口 ChatModel
|
||||
*/
|
||||
@Bean(name = "qwen")
|
||||
public ChatModel chatModelQwen()
|
||||
{
|
||||
return OpenAiChatModel.builder()
|
||||
.apiKey(System.getenv("DASH_SCOPE_API_KEY"))
|
||||
.modelName("qwen-plus")
|
||||
.baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
|
||||
.build();
|
||||
}
|
||||
}
|
||||
11
langchain4j-ai-pinecone/src/main/resources/application.yml
Normal file
11
langchain4j-ai-pinecone/src/main/resources/application.yml
Normal file
@@ -0,0 +1,11 @@
|
||||
server:
|
||||
port: 9011
|
||||
spring:
|
||||
application:
|
||||
name: langchain4j-ai-pinecone
|
||||
langchain4j:
|
||||
community:
|
||||
dashscope:
|
||||
embedding-model:
|
||||
model-name: text-embedding-v3
|
||||
api-key: ${DASH_SCOPE_API_KEY}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.iwe3.langchain4j;
|
||||
|
||||
import dev.langchain4j.data.segment.TextSegment;
|
||||
import dev.langchain4j.model.embedding.EmbeddingModel;
|
||||
import dev.langchain4j.store.embedding.EmbeddingMatch;
|
||||
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
|
||||
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
|
||||
import dev.langchain4j.store.embedding.EmbeddingStore;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class PineConeApplicationTest {
|
||||
|
||||
@Autowired
|
||||
private EmbeddingModel embeddingModel;
|
||||
@Autowired
|
||||
private EmbeddingStore embeddingStore;
|
||||
|
||||
|
||||
/**
|
||||
* Pinecone - 相似度匹配
|
||||
*/
|
||||
@Test
|
||||
public void embeddingSearch() {
|
||||
// 提问,并将问题转成向量数据
|
||||
var queryEmbedding = embeddingModel.embed("你最喜欢的运动是什么?").content();
|
||||
|
||||
// 创建搜索请求对象
|
||||
var searchRequest = EmbeddingSearchRequest.builder()
|
||||
.queryEmbedding(queryEmbedding)
|
||||
.maxResults(1) // 匹配最相似的一条记录
|
||||
.minScore(0.8)
|
||||
.build();
|
||||
|
||||
// 根据搜索请求 searchRequest 在向量存储中进行相似度搜索
|
||||
EmbeddingSearchResult<TextSegment> searchResult = embeddingStore.search(searchRequest);
|
||||
|
||||
// searchResult.matches(): 获取搜索结果中的匹配项列表。
|
||||
// .get(0): 从匹配项列表中获取第一个匹配项
|
||||
EmbeddingMatch<TextSegment> embeddingMatch = searchResult.matches().get(0);
|
||||
|
||||
// 获取匹配项的相似度得分
|
||||
System.out.println(embeddingMatch.score()); // 输出:0.8144288515898701
|
||||
|
||||
// 返回文本结果
|
||||
System.out.println(embeddingMatch.embedded().text());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 将文本转换成向量,然后存储到 pinecone 中
|
||||
*
|
||||
* 参考:
|
||||
* https://docs.langchain4j.dev/tutorials/embedding-stores
|
||||
*/
|
||||
@Test
|
||||
public void testPineconeEmbedded() {
|
||||
// 将文本转换成向量
|
||||
var seg1 = TextSegment.from("我喜欢羽毛球");
|
||||
var em1 = embeddingModel.embed(seg1).content();
|
||||
embeddingStore.add(em1, seg1);// 存入向量数据库
|
||||
|
||||
var seg2 = TextSegment.from("今天天气很好");
|
||||
var em2 = embeddingModel.embed(seg2).content();
|
||||
embeddingStore.add(em2, seg2);// 存入向量数据库
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testEmbeddingModel() {
|
||||
var embed = embeddingModel.embed("你好");
|
||||
|
||||
System.out.println("向量维度:" + embed.content().vector().length);
|
||||
System.out.println("向量输出:" + embed.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user