Cohere 임베딩
Cohere 임베딩 (Cohere Embeddings)
Bedrock Cohere Embedding 모델을 제공해요. 생성형 AI 기능을 비즈니스 성과를 개선하는 핵심 앱과 워크플로에 통합할 수 있죠. 이 글에서는 Cohere 임베딩 모델 활성화, 프로퍼티 설정, 런타임 옵션, 그리고 저수준 API 사용까지 알아볼게요.
출처: 문서
본문
Bedrock Cohere Embedding 모델을 제공해요. 생성형 AI 기능을 비즈니스 성과를 개선하는 핵심 앱과 워크플로에 통합해요.
AWS Bedrock Cohere Model Page와 Amazon Bedrock User Guide에는 AWS 호스팅 모델 사용법에 대한 자세한 정보가 담겨 있어요.
Prerequisites
API 접근 설정은 Amazon Bedrock에 대한 Spring AI 문서를 참고하세요.
Add Repositories and BOM
Spring AI 아티팩트는 Maven Central과 Spring Snapshot 저장소에 게시돼요. 빌드 시스템에 이러한 저장소를 추가하려면 Artifact Repositories 섹션을 참고하세요.
의존성 관리를 돕기 위해 Spring AI는 프로젝트 전체에서 일관된 Spring AI 버전을 사용하도록 보장하는 BOM(bill of materials)을 제공해요. 빌드 시스템에 Spring AI BOM을 추가하려면 Dependency Management 섹션을 참고하세요.
Auto-configuration
| __ | Spring AI 자동 설정과 스타터 모듈 아티팩트 이름에 큰 변경이 있었어요. 자세한 내용은 upgrade notes를 참고해주세요. |
|---|
프로젝트의 Maven pom.xml 파일에 spring-ai-starter-model-bedrock 의존성을 추가하세요:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-bedrock</artifactId>
</dependency>
또는 Gradle build.gradle 빌드 파일에 추가할 수도 있어요:
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-model-bedrock'
}
| __ | 빌드 파일에 Spring AI BOM을 추가하려면 Dependency Management 섹션을 참고해주세요. |
|---|
Enable Cohere Embedding Support
기본적으로 Cohere 임베딩 모델은 비활성화돼 있어요. 활성화하려면 애플리케이션 구성에서 spring.ai.model.embedding 프로퍼티를 bedrock-cohere로 설정하세요:
spring.ai.model.embedding=bedrock-cohere
또는 Spring Expression Language(SpEL)를 사용해 환경 변수를 참조할 수도 있어요:
# In application.yml
spring:
ai:
model:
embedding: ${AI_MODEL_EMBEDDING}
# In your environment or .env file
export AI_MODEL_EMBEDDING=bedrock-cohere
또한 애플리케이션 시작 시 Java 시스템 프로퍼티로 이 프로퍼티를 설정할 수 있어요:
java -Dspring.ai.model.embedding=bedrock-cohere -jar your-application.jar
Embedding Properties
프리픽스 spring.ai.bedrock.aws는 AWS Bedrock에 대한 연결을 구성하는 프로퍼티 프리픽스예요.
| Property | Description | Default |
|---|---|---|
| spring.ai.bedrock.aws.region | 사용할 AWS 리전. | us-east-1 |
| spring.ai.bedrock.aws.access-key | AWS access key. | - |
| spring.ai.bedrock.aws.secret-key | AWS secret key. | - |
| spring.ai.bedrock.aws.profile.name | AWS 프로필 이름. | - |
| spring.ai.bedrock.aws.profile.credentials-path | AWS 자격 증명 파일 경로. | - |
| spring.ai.bedrock.aws.profile.configuration-path | AWS config 파일 경로. | - |
| __ | 임베딩 자동 설정의 활성화/비활성화는 이제 프리픽스 spring.ai.model.embedding이 붙은 최상위 프로퍼티로 설정해요. 활성화하려면 spring.ai.model.embedding=bedrock-cohere (기본값으로 활성화됨). 비활성화하려면 spring.ai.model.embedding=none (또는 bedrock-cohere와 일치하지 않는 어떤 값). 이 변경은 여러 모델의 설정을 허용하기 위한 것이에요. |
|---|
프리픽스 spring.ai.bedrock.cohere.embedding(BedrockCohereEmbeddingProperties에 정의)은 Cohere의 임베딩 모델 구현을 구성하는 프로퍼티 프리픽스예요.
| Property | Description | Default |
|---|---|---|
| spring.ai.model.embedding | Cohere 지원 활성화 또는 비활성화 | bedrock-cohere |
| spring.ai.bedrock.cohere.embedding.enabled (제거됨, 더 이상 유효하지 않음) | Cohere 지원 활성화 또는 비활성화 | false |
| spring.ai.bedrock.cohere.embedding.model | 사용할 모델 ID. 지원 모델은 CohereEmbeddingModel 참고. | cohere.embed-multilingual-v3 |
| spring.ai.bedrock.cohere.embedding.input-type | 각 유형을 구분하기 위해 특수 토큰을 앞에 붙여요. 서로 다른 유형을 섞으면 안 되지만, 검색과 검색 결과에 사용할 때 유형을 섞는 것은 예외예요. 이 경우 말뭉치를 search_document 유형으로, 쿼리는 search_query 유형으로 임베딩하세요. | SEARCH_DOCUMENT |
| spring.ai.bedrock.cohere.embedding.truncate | 최대 토큰 길이보다 긴 입력을 API가 어떻게 처리할지 지정해요. LEFT 또는 RIGHT를 지정하면 모델은 남은 입력이 모델의 최대 입력 토큰 길이와 정확히 같아질 때까지 입력을 버려요. | NONE |
| __ | Amazon Bedrock을 통해 Cohere에 접근할 때 truncate 기능을 사용할 수 없어요. 이는 Amazon Bedrock의 문제입니다. Spring AI 클래스 BedrockCohereEmbeddingModel은 모델이 지원하는 최대 길이인 2048자로 잘라요(truncate). |
|---|
다른 모델 ID는 CohereEmbeddingModel을 참고하세요. 지원 값은 cohere.embed-multilingual-v3와 cohere.embed-english-v3이에요. 모델 ID 값은 AWS Bedrock 문서의 base model IDs에서도 찾을 수 있어요.
| __ | spring.ai.bedrock.cohere.embedding 프리픽스가 붙은 모든 프로퍼티는 런타임에 EmbeddingRequest 호출에 요청별 Runtime Options를 추가해 재정의할 수 있어요. |
|---|
Runtime Options
BedrockCohereEmbeddingOptions.java는 input-type이나 truncate 같은 모델 구성을 제공해요.
시작 시 기본 옵션은 BedrockCohereEmbeddingModel(api, options) 생성자나 spring.ai.bedrock.cohere.embedding.* 프로퍼티로 구성할 수 있어요.
런타임에는 EmbeddingRequest 호출에 새 요청별 옵션을 추가해 기본 옵션을 재정의할 수 있어요. 예를 들어 특정 요청의 기본 input type을 재정의하려면:
EmbeddingResponse embeddingResponse = embeddingModel.call(
new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
BedrockCohereEmbeddingOptions.builder()
.inputType(InputType.SEARCH_DOCUMENT)
.build()));
Sample Controller
새 Spring Boot 프로젝트를 만들고, pom(또는 gradle) 의존성에 spring-ai-starter-model-bedrock를 추가하세요.
src/main/resources 디렉터리 아래에 application.properties 파일을 추가해 Cohere Embedding 모델을 활성화하고 구성해요:
spring.ai.bedrock.aws.region=eu-central-1
spring.ai.bedrock.aws.access-key=${AWS_ACCESS_KEY_ID}
spring.ai.bedrock.aws.secret-key=${AWS_SECRET_ACCESS_KEY}
spring.ai.model.embedding=bedrock-cohere
spring.ai.bedrock.cohere.embedding.input-type=search-document
| __ | regions, access-key, secret-key를 자신의 AWS 자격 증명으로 바꿔주세요. |
|---|
이렇게 하면 클래스에 주입할 수 있는 BedrockCohereEmbeddingModel 구현이 생성돼요. 텍스트 임베딩에 임베딩 모델을 사용하는 간단한 @Controller 클래스 예시예요:
@RestController
public class EmbeddingController {
private final EmbeddingModel embeddingModel;
@Autowired
public EmbeddingController(EmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
}
@GetMapping("/ai/embedding")
public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of(message));
return Map.of("embedding", embeddingResponse);
}
}
Manual Configuration
BedrockCohereEmbeddingModel은 EmbeddingModel을 구현하며, Bedrock Cohere 서비스에 연결하기 위해 Low-level CohereEmbeddingBedrockApi 클라이언트를 사용해요.
프로젝트의 Maven pom.xml 파일에 spring-ai-bedrock 의존성을 추가하세요:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bedrock</artifactId>
</dependency>
또는 Gradle build.gradle 빌드 파일에 추가할 수도 있어요:
dependencies {
implementation 'org.springframework.ai:spring-ai-bedrock'
}
| __ | 빌드 파일에 Spring AI BOM을 추가하려면 Dependency Management 섹션을 참고해주세요. |
|---|
다음으로 BedrockCohereEmbeddingModel을 만들고 텍스트 임베딩에 사용해요:
var cohereEmbeddingApi =new CohereEmbeddingBedrockApi(
CohereEmbeddingModel.COHERE_EMBED_MULTILINGUAL_V1.id(),
EnvironmentVariableCredentialsProvider.create(), Region.US_EAST_1.id(), new JsonMapper());
var embeddingModel = new BedrockCohereEmbeddingModel(this.cohereEmbeddingApi);
EmbeddingResponse embeddingResponse = this.embeddingModel
.embedForResponse(List.of("Hello World", "World is big and salvation is near"));
Low-level CohereEmbeddingBedrockApi Client
CohereEmbeddingBedrockApi는 AWS Bedrock Cohere Command models 위의 가벼운 Java 클라이언트를 제공해요.
다음 클래스 다이어그램은 CohereEmbeddingBedrockApi 인터페이스와 구성 요소를 보여줘요:
CohereEmbeddingBedrockApi는 단일 및 배치 임베딩 계산을 위해 cohere.embed-english-v3와 cohere.embed-multilingual-v3 모델을 지원해요.
프로그래밍 방식으로 API를 사용하는 간단한 스니펫이에요:
CohereEmbeddingBedrockApi api = new CohereEmbeddingBedrockApi(
CohereEmbeddingModel.COHERE_EMBED_MULTILINGUAL_V1.id(),
EnvironmentVariableCredentialsProvider.create(),
Region.US_EAST_1.id(), new JsonMapper());
CohereEmbeddingRequest request = new CohereEmbeddingRequest(
List.of("I like to eat apples", "I like to eat oranges"),
CohereEmbeddingRequest.InputType.search_document,
CohereEmbeddingRequest.Truncate.NONE);
CohereEmbeddingResponse response = this.api.embedding(this.request);