SpringAI Ollama

0

今天发现原来还有SpringAI这个东西,用来和Ollama对接非常方便。

ollama

mkdir -p /data/ollama
conda create --name ollama python=3.11
conda activate ollama
curl -fsSL https://ollama.com/install.sh | sh
ollama pull glm4
ollama pull quentinz/bge-large-zh-v1.5
conda deactivate

添加依赖

        <dependency>
            <groupId>io.springboot.ai</groupId>
            <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
            <version>1.0.3</version>
        </dependency>

测试代码

    @Test
    public void testApi() {
        final OllamaApi api = new OllamaApi("http://192.168.8.50:11434");
        final OllamaChatClient client = new OllamaChatClient(
            api, OllamaOptions.create()
            .withModel("glm4")
        );
        final Scanner scanner = new Scanner(System.in);
        while(true) {
            final String line = scanner.nextLine();
            if("/exit".equals(line)) {
                break;
            }
//          final String response = client.call(line);
//          System.out.println(response);
            final Flux<String> stream = client.stream(line);
            stream.doOnNext(x -> {
                System.out.println(new String(x.getBytes()));
            });
        }
        scanner.close();
    }
    
    @Test
    public void testEmbedding() {
        final OllamaApi api = new OllamaApi("http://192.168.8.50:11434");
        final OllamaEmbeddingClient client = new OllamaEmbeddingClient(api)
            .withDefaultOptions(OllamaOptions.create().withModel("quentinz/bge-large-zh-v1.5"));
        final VectorStore vectorStore = new SimpleVectorStore(client);
        vectorStore.add(List.of(
            new Document("北京是中国的首都"),
            new Document("熊猫喜欢竹笋"),
            new Document("熊猫是肉食动物"),
            new Document("春天的竹笋最甜美"),
            new Document("熊猫喜欢春天")
        ));
        System.out.println("向量构建完成");
        final Scanner scanner = new Scanner(System.in);
        while(true) {
            final String line = scanner.nextLine();
            if("/exit".equals(line)) {
                break;
            }
            final List<Document> list = vectorStore.similaritySearch(SearchRequest.query(line).withTopK(4));
            list.forEach(v -> {
                System.out.println(v.getContent());
            });
        }
        scanner.close();
    }