Cohere 통합
Cohere 통합
커뮤니티에서 만든 Cohere 채팅 모델 통합 문서예요. Cohere의 V2 Chat API를 기반으로 구현됐어요. Cohere의 command-r 계열 모델을 LangChain4j에서 바로 쓸 수 있는데, 일반 채팅은 CohereChatModel, 스트리밍은 CohereStreamingChatModel을 주로 쓰게 돼요.
출처: 공식문서
Maven 의존성
1.0.0-alpha1 이후:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-cohere</artifactId>
<version>${latest version here}</version>
</dependency>
BOM을 쓰면 의존성 버전을 일관되게 관리할 수도 있어요:
<dependencyManagement>
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-community-bom</artifactId>
<version>${latest version here}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencyManagement>
채팅 모델 지원
CohereChatModel은 다음 코드로 만들 수 있어요:
ChatModel model = CohereChatModel.builder()
.apiKey(System.getenv("CO_API_KEY"))
.modelName("command-r7b-12-2024")
.logRequests(true)
.logResponses(true)
.build();
스트리밍 응답에는 CohereStreamingChatModel을 써요:
StreamingChatModel streamingModel = CohereStreamingChatModel.builder()
.apiKey(System.getenv("CO_API_KEY"))
.modelName("command-r7b-12-2024")
.logRequests(true)
.logResponses(true)
.build();
설정 가능한 파라미터
CohereChatModel과 CohereStreamingChatModel은 다음 파라미터를 받아요.
| Property | Description | Default Value |
|---|---|---|
baseUrl |
The URL to connect to the Cohere API. | https://api.cohere.com/v2/ |
apiKey |
The API Key. | |
modelName |
The model to use, e.g. command-r7b-12-2024 or command-r-plus. |
|
timeout |
HTTP client timeout for requests. | |
maxRetries |
Maximum number of retries per request. Only available on CohereChatModel. |
3 |
temperature |
Sampling temperature. | |
topP |
Nucleus sampling threshold. | |
topK |
Limits sampling to the topK most likely tokens at each step. |
|
frequencyPenalty |
Penalty for tokens based on how often they have appeared. | |
presencePenalty |
Penalty for tokens that have appeared at least once. | |
maxTokens |
The maximum number of tokens returned by this request. | |
stopSequences |
Sequences that cause the model to stop generating further text. | |
toolSpecifications |
Tool (function) definitions the model can call. | |
toolChoice |
A ToolChoice controlling how the model selects tools. Possible values: AUTO, REQUIRED. |
|
responseFormat |
The response format, e.g. TEXT or JSON. |
|
thinkingType |
A CohereThinkingType enabling or disabling extended thinking for reasoning-capable models. |
|
thinkingTokenBudget |
Maximum tokens the model may spend on internal thinking. | |
safetyMode |
A CohereSafetyMode inserted into the prompt. Possible values: CONTEXTUAL, STRICT, OFF. |
|
priority |
Request priority when the Cohere API is under load. | |
seed |
If set, the model samples tokens deterministically. | |
logprobs |
Whether to include token log probabilities in the response. | |
strictTools |
Whether to enforce strict adherence to tool definitions. | |
defaultRequestParameters |
Default ChatRequestParameters applied to every request. |
|
listeners |
Listeners that listen for request, response and errors. | |
logRequests |
Whether to log request or not. | false |
logResponses |
Whether to log response or not. | false |
표의 값은 영문 그대로 보존했어요. 표를 보면 maxRetries는 CohereChatModel에서만 쓸 수 있고 기본이 3, baseUrl 기본값은 https://api.cohere.com/v2/라는 걸 알 수 있어요.
응답 메타데이터
Cohere 전용 응답 메타데이터에 접근할 수 있어요:
ChatResponse response = model.chat(UserMessage.from("Hello"));
CohereChatResponseMetadata metadata = (CohereChatResponseMetadata) response.metadata();
List<CohereLogprobs> logprobs = metadata.logprobs();
CohereBilledUnits billedUnits = metadata.billedUnits();
Integer cachedTokens = metadata.cachedTokens();
| Property | Description |
|---|---|
logprobs |
Log probabilities for generated tokens. Returned when logprobs is enabled. |
billedUnits |
Billing breakdown for the request (input tokens, output tokens, search units, classifications). |
cachedTokens |
Number of tokens served from Cohere's prompt cache. |
logprobs는 logprobs 옵션을 켰을 때만 돌아오고, billedUnits는 요청의 과금 내역(입력·출력 토큰, 검색 단위, 분류), cachedTokens는 Cohere 프롬프트 캐시에서 제공된 토큰 수예요.