spring AI快速使用模板

官方文档https://docs.spring.io/spring-ai/reference/api/vectordbs.html
以使用ollama为例

ollama基本命令

ollama 列出所有命令
ollama pull model_name:version 下载模型
ollama list 查看已下载模型列表
ollama show model_name:version 查看模型元数据
ollama re model_name:version 删除模型
ollama ps 查看正在运行的模型
ollama run model_name 启动模型

浏览器查看模型元数据列表

http://localhost:11434/api/tags

模型ymal配置项模板

spring:
  data:
    redis:
      url: redis://localhost:6379
  ai:
    ollama:
      base-url: localhost:11434
      chat:
        options:
          model: moondream:latest
          temperature: 0.8
      init:
        pull-model-strategy: never
      embedding:
        enabled: true
        model: mxbai-embed-large
    vectorstore:
      redis:
        index: my-index
        prefix: customer-history
        initialize-schema: true

model注入模板

    @Autowired
    private OllamaChatModel ollamaChatModel;
    @Autowired
    private OllamaEmbeddingModel ollamaEmbeddingModel;
    @Autowired
    private RedisVectorStore vectorStore;

响应式识别图片 使用advisor、vector-store、tools 模板

            //ARG记忆检索向量数据库 顾问
            VectorStoreChatMemoryAdvisor vectorMemoryAdvisor =
                    VectorStoreChatMemoryAdvisor.builder(vectorStore).build();
            //执行日志 顾问
            SimpleLoggerAdvisor loggerAdvisor = new SimpleLoggerAdvisor();
            //解析图片输入
            UserMessage userMessage = new UserMessage(message,
                    new Media(MimeTypeUtils.IMAGE_JPEG, imageFile.getResource()));
            ChatClient chatClient = ChatClient.builder(this.ollamaChatModel).build();
            return chatClient.prompt(new Prompt(userMessage))
                    .advisors(List.of(vectorMemoryAdvisor, loggerAdvisor))
                    .system("你是一个图片识别专家,可以识别任何图片并描述内容")
                    .user(message)
                    .tools(new MyTool())
                    .stream()
                    .content();

vector-store 自定义配置模板

@Bean
public JedisPooled jedisPooled() {
    return new JedisPooled("localhost", 6379);
}
@Bean
public VectorStore vectorStore(JedisPooled jedisPooled, EmbeddingModel embeddingModel) {
    return RedisVectorStore.builder(jedisPooled, embeddingModel)
        .indexName("custom-index")                // Optional: defaults to "spring-ai-index"
        .prefix("custom-prefix")                  // Optional: defaults to "embedding:"
        .metadataFields(                         // Optional: define metadata fields for filtering
            MetadataField.tag("country"),
            MetadataField.numeric("year"))
        .initializeSchema(true)                   // Optional: defaults to false
        .batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
        .build();
}

vector-store 使用模板

// 存储
Document documentV1 = new Document(
    "AI and Machine Learning Best Practices",
    Map.of(
        "docId", "AIML-001",
        "version", "1.0",
        "lastUpdated", "2024-01-01"
    )
);
Document documentV2 = new Document(
    "AI and Machine Learning Best Practices - Updated",
    Map.of(
        "docId", "AIML-001",
        "version", "2.0",
        "lastUpdated", "2024-02-01"
    )
);
vectorStore.add(List.of(documentV1,documentV2));

// 查询
SearchRequest request = SearchRequest.builder()
    .query("AI and Machine Learning")
    .filterExpression("docId == 'AIML-001'")
    .build();
List<Document> results = vectorStore.similaritySearch(request);

//删除
Filter.Expression expression1 = new Filter
                .Expression(Filter.ExpressionType.EQ
, new Filter.Key("docId"), new Filter.Value("AIML-001"));

Filter.Expression expression2 = new Filter
                .Expression(Filter.ExpressionType.EQ
, new Filter.Key("version"), new Filter.Value("1.0"));

Filter.Expression deleteOldVersion  = new Filter
                .Expression(Filter.ExpressionType.AND, expression1, expression2);
vectorStore.delete(deleteOldVersion);
vectorStore.delete("docId == 'AIML-001' AND version == '1.0'");
vectorStore.delete("key == 'value'");

RAG(检索增强)

org.springframework.ai.chat.client.advisor.RetrievalAugmentationAdvisor
使用vector-store之前 => 对用户输入优化、分解、翻译、附加上下文
使用vector-store之后 => 对结果排名、压缩、筛选
官方文档https://docs.spring.io/spring-ai/reference/api/retrieval-augmented-generation.html

ETL(提取、转化、加载 )主要定义接口

//提取(pdf、doc、markdown、html、json)
public interface DocumentReader extends Supplier<List<Document>> {

    default List<Document> read() {
        return get();
    }

}
//转化(文本过长分块、提取关键字写进元数据、生成摘要、填充模板)
public interface DocumentTransformer extends Function<List<Document>, List<Document>> {

    default List<Document> transform(List<Document> transform) {
        return apply(transform);
    }

}
//主要是利用vertor-store的实现去存储数据
public interface DocumentWriter extends Consumer<List<Document>> {

    default void write(List<Document> documents) {
        accept(documents);
    }

}

promptTmeplate 模板

PromptTemplate promptTemplate = new PromptTemplate("Tell me a {adjective} joke about {topic}");

Prompt prompt = promptTemplate.create(Map.of("adjective", adjective, "topic", topic));

return chatClient.call(prompt).getResult();

响应评估(evaluation)

@FunctionalInterface
public interface Evaluator {
    EvaluationResponse evaluate(EvaluationRequest evaluationRequest);
}
//内置实现类 RelevancyEvaluator FactCheckingEvaluator
void testEvaluation() {
    String userText = "What is the purpose of Carina?";
    ChatResponse response = ChatClient.builder(chatModel)
            .build().prompt()
            .advisors(new QuestionAnswerAdvisor(vectorStore))
            .user(userText)
            .call()
            .chatResponse();
    String responseContent = response.getResult().getOutput().getContent();

    RelevancyEvaluator relevancyEvaluator = new RelevancyEvaluator(ChatClient.builder(chatModel));

    EvaluationRequest evaluationRequest = new EvaluationRequest(userText,
            (List<Content>) response.getMetadata().get(QuestionAnswerAdvisor.RETRIEVED_DOCUMENTS), responseContent);

    EvaluationResponse evaluationResponse = relevancyEvaluator.evaluate(evaluationRequest);

    assertTrue(evaluationResponse.isPass(), "Response is not relevant to the question");

}

tools模板

public class MyTool {
    @Tool(description = "Get the current date and time in the user's timezone")
    String getCurrentDateTime() {
        return LocalDateTime.now().atZone(LocaleContextHolder.getTimeZone().toZoneId()).toString();
    }
}

类型模板

MessageType 用来表示不同身份(user、system、assistant、tool)
MimeType 用来表示内容类型(img、text、json、octet-stream)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容