【Spring AI】11. 通用模型 API

通用模型 API

为了给所有 AI 模型客户端提供基础,创建了通用模型 API。通过遵循通用模式,使得您可以更加容易向 Spring AI 贡献新的 AI 模型。以下部分将介绍这个 API。

类图


在这里插入图片描述

ModelClient


ModelClient 接口提供了一个用于调用 AI 模型的通用 API 。通过抽象发送请求和接收响应的过程,处理与各种类型的 AI 模型的交互。该接口使用 Java 泛型来适应不同类型的请求和响应,增强了在不同 AI 模型实现中的灵活性和适应性。
下面定义了接口:

public interface ModelClient<TReq extends ModelRequest<?>, TRes extends ModelResponse<?>> {

	/**
	 * Executes a method call to the AI model.
	 * @param request the request object to be sent to the AI model
	 * @return the response from the AI model
	 */
	TRes call(TReq request);

}

StreamingModelClient

StreamingModelClient 接口提供了一个用于调用流式响应的 AI 模型的通用 API。它抽象了发送请求和接收流式响应的过程。该接口使用 Java 泛型来适应不同类型的请求和响应,增强了在不同 AI 模型实现之间的灵活性和适应性。

public interface StreamingModelClient<TReq extends ModelRequest<?>, TResChunk extends ModelResponse<?>> {

	/**
	 * Executes a method call to the AI model.
	 * @param request the request object to be sent to the AI model
	 * @return the streaming response from the AI model
	 */
	Flux<TResChunk> stream(TReq request);

}


ModelRequest


表示向 AI 模型发出请求的接口。该接口封装了与 AI 模型交互所需的必要信息,包括指令或输入(通用类型 T)和其他模型选项参数。它提供了一种标准化的方式来向 AI 模型发送请求,确保所有必要的细节都包含在内,并且可以轻松管理。

public interface ModelRequest<T> {

	/**
	 * Retrieves the instructions or input required by the AI model.
	 * @return the instructions or input required by the AI model
	 */
	T getInstructions(); // required input

	/**
	 * Retrieves the customizable options for AI model interactions.
	 * @return the customizable options for AI model interactions
	 */
	ModelOptions getOptions();

}

ModelOptions


表示 AI 模型交互的可定制选项参数的接口。这个标记接口允许指定各种设置和参数,可以影响 AI 模型的行为和输出。它旨在可以灵活的微调,确保 AI 模型可以满足具体要求。

public interface ModelOptions {

}

ModelResponse


表示从 AI 模型接收到的响应的接口。这个接口提供方法来访问 AI 模型生成的主要结果或结果列表,以及响应元数据。它作为一种标准化的方式来封装和管理来自 AI 模型的输出,确保轻松检索和处理生成的信息。

public interface ModelResponse<T extends ModelResult<?>> {

	/**
	 * Retrieves the result of the AI model.
	 * @return the result generated by the AI model
	 */
	T getResult();

	/**
	 * Retrieves the list of generated outputs by the AI model.
	 * @return the list of generated outputs
	 */
	List<T> getResults();

	/**
	 * Retrieves the response metadata associated with the AI model's response.
	 * @return the response metadata
	 */
	ResponseMetadata getMetadata();

}

ModelResult


该接口提供了AI模型的主要输出以及与该结果相关联的元数据的方法。它旨在提供一种标准化和全面的方式来处理和解释人工智能模型产生的输出。

public interface ModelResult<T> {

	/**
	 * Retrieves the output generated by the AI model.
	 * @return the output generated by the AI model
	 */
	T getOutput();

	/**
	 * Retrieves the metadata associated with the result of an AI model.
	 * @return the metadata associated with the result
	 */
	ResultMetadata getMetadata();

}

相关推荐

  1. SpringAI如何集成Ollama开发AI应用

    2024-04-30 15:44:03       37 阅读
  2. AI模型争霸:通用vs垂直,谁主沉浮?

    2024-04-30 15:44:03       36 阅读
  3. AI大战:通用VS垂直模型,谁主未来?

    2024-04-30 15:44:03       31 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-30 15:44:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-30 15:44:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-30 15:44:03       87 阅读
  4. Python语言-面向对象

    2024-04-30 15:44:03       96 阅读

热门阅读

  1. mySql 基础语法

    2024-04-30 15:44:03       21 阅读
  2. go语言获取变量类型的4种方式

    2024-04-30 15:44:03       30 阅读
  3. 【工作实践-11】关于uniapp切换账号登录失败问题

    2024-04-30 15:44:03       31 阅读
  4. milvus indexnode启动源码分析

    2024-04-30 15:44:03       21 阅读
  5. android中给view添加遮罩层

    2024-04-30 15:44:03       33 阅读
  6. leetcode_40.组合总和 II

    2024-04-30 15:44:03       33 阅读
  7. 什么是AI推理

    2024-04-30 15:44:03       28 阅读
  8. conda修改当前环境中的python版本

    2024-04-30 15:44:03       35 阅读
  9. 响应式布局插件

    2024-04-30 15:44:03       34 阅读
  10. 行列式求解

    2024-04-30 15:44:03       32 阅读
  11. YOLOv8+bytetrack实现多目标追踪

    2024-04-30 15:44:03       40 阅读
  12. 生成能够精确匹配原字符串的正则表达式

    2024-04-30 15:44:03       38 阅读
  13. zynq基础知识学习(1)

    2024-04-30 15:44:03       31 阅读