本文主要研究一下如何使用langchain4j+JlamaEmbeddingModel调用EmbeddingModel

步骤

pom.xml

    <properties>
        <maven.compiler.source>21</maven.compiler.source>
        <maven.compiler.target>21</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <logback.version>1.5.6</logback.version>
        <jlama.version>0.8.3</jlama.version>
    </properties>

     <dependencies>
        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j-jlama</artifactId>
            <version>1.0.0-beta1</version>
            <exclusions>
                <exclusion>
                    <groupId>com.github.tjake</groupId>
                    <artifactId>jlama-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Add native jlama bindings -->
        <dependency>
            <groupId>com.github.tjake</groupId>
            <artifactId>jlama-native</artifactId>
            <classifier>${os.detected.classifier}</classifier>
            <version>${jlama.version}</version>
        </dependency>

        <dependency>
            <groupId>com.github.tjake</groupId>
            <artifactId>jlama-core</artifactId>
            <version>${jlama.version}</version>
        </dependency>

        <dependency>
            <groupId>dev.langchain4j</groupId>
            <artifactId>langchain4j</artifactId>
            <version>1.0.0-beta1</version>
        </dependency>

    </dependencies> 

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.7.1</version>
            </extension>
        </extensions>
    </build>

下载模型文件

这里使用https://hf-mirror.com/intfloat/e5-small-v2
下载到下载到~/.jlama/models/目录下

示例

public class JlamaEmbeddedModelExample {

    public static void main(String[] args) {
        EmbeddingModel embeddingModel = JlamaEmbeddingModel.builder()
                .modelName("intfloat/e5-small-v2")
                .build();

        // For simplicity, this example uses an in-memory store, but you can choose any external compatible store for production environments.
        EmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();

        TextSegment segment1 = TextSegment.from("I like football.");
        Embedding embedding1 = embeddingModel.embed(segment1).content();
        embeddingStore.add(embedding1, segment1);

        TextSegment segment2 = TextSegment.from("The weather is good today.");
        Embedding embedding2 = embeddingModel.embed(segment2).content();
        embeddingStore.add(embedding2, segment2);

        String userQuery = "What is your favourite sport?";
        Embedding queryEmbedding = embeddingModel.embed(userQuery).content();
        int maxResults = 1;
        List<EmbeddingMatch<TextSegment>> relevant = embeddingStore.findRelevant(queryEmbedding, maxResults);
        EmbeddingMatch<TextSegment> embeddingMatch = relevant.get(0);

        System.out.println("Question: " + userQuery); // What is your favourite sport?
        System.out.println("Response: " + embeddingMatch.embedded().text()); // I like football.
    }
}
这里使用JlamaEmbeddingModel加载intfloat/e5-small-v2,之后embed了两个TextSegment,最后通过embeddingStore.findRelevant去匹配与queryEmbedding最相关的TextSegment

小结

langchain4j提供了langchain4j-jlama来集成Jlama,Jlama提供了JlamaEmbeddingModel来加载EmbeddingModel。

doc


codecraft
11.9k 声望2k 粉丝

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下...