제네릭 모델 API

제네릭 모델 API (Generic Model API)

모든 AI 모델의 기반을 제공하기 위해 Generic Model API가 만들어졌어요. 공통 패턴을 따르면 Spring AI에 새로운 AI 모델 지원을 쉽게 기여할 수 있죠. 이 문서에서는 Model, StreamingModel, ModelRequest, ModelOptions, ModelResponse, ModelResult 인터페이스를 하나씩 살펴볼게요.

출처: 문서

본문

모든 AI 모델의 기반을 제공하기 위해 Generic Model API가 만들어졌어요. 이로써 공통 패턴을 따라 Spring AI에 새로운 AI 모델 지원을 쉽게 기여할 수 있어요. 다음 섹션에서 이 API를 자세히 살펴볼게요.

Class Diagram

Model

Model 인터페이스는 AI 모델을 호출하기 위한 제네릭 API를 제공해요. 요청 전송과 응답 수신 과정을 추상화해서 다양한 유형의 AI 모델과의 상호작용을 처리하도록 설계됐어요. 이 인터페이스는 서로 다른 유형의 요청과 응답을 수용하기 위해 Java 제네릭을 사용해서, 다양한 AI 모델 구현에서 유연성과 적응성을 높여줘요.

인터페이스는 다음과 같이 정의돼요:

public interface Model<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);

}

StreamingModel

StreamingModel 인터페이스는 스트리밍 응답으로 AI 모델을 호출하기 위한 제네릭 API를 제공해요. 요청 전송과 스트리밍 응답 수신 과정을 추상화해요. 이 인터페이스는 서로 다른 유형의 요청과 응답을 수용하기 위해 Java 제네릭을 사용해서, 다양한 AI 모델 구현에서 유연성과 적응성을 높여줘요.

public interface StreamingModel<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

ModelRequest 인터페이스는 AI 모델에 대한 요청을 나타내요. 지시사항이나 입력(제네릭 타입 T)과 추가 모델 옵션을 포함해서 AI 모델과 상호작용하는 데 필요한 정보를 캡슐화해요. 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

ModelOptions 인터페이스는 AI 모델 상호작용을 위한 커스터마이즈 가능한 옵션을 나타내요. 이 마커 인터페이스는 AI 모델의 동작과 출력에 영향을 줄 수 있는 다양한 설정과 매개변수의 지정을 허용해요. 다양한 AI 시나리오에서 유연성과 적응성을 제공해, AI 모델이 특정 요구사항에 맞게 미세 조정될 수 있도록 설계됐어요.

public interface ModelOptions {

}

ModelResponse

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

ModelResult 인터페이스는 AI 모델의 주요 출력과 이 결과와 연관된 메타데이터에 접근하는 메서드를 제공해요. 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();

}

더 알아보기 (Learn more)