Cohere (Java)
Trace Cohere chat, embedding, and reranking operations in Java with TracedCohereClient.
📝
TL;DR
TracedCohereClientwraps the Cohere Java SDK (com.cohere.api)- Three operations:
chat(),embed(), andrerank() - Reranking uses
RERANKERspan kind - the only Java integration with this - Captures tool calls, chat history, preamble, and provider-specific attributes
Prerequisites
Complete the Java SDK setup first.
Installation
<dependency>
<groupId>com.github.future-agi.traceAI</groupId>
<artifactId>traceai-java-cohere</artifactId>
<version>main-SNAPSHOT</version>
</dependency>implementation 'com.github.future-agi.traceAI:traceai-java-cohere:main-SNAPSHOT' You also need the Cohere Java SDK:
<dependency>
<groupId>com.cohere</groupId>
<artifactId>cohere-java</artifactId>
<version>1.5.0</version>
</dependency>implementation 'com.cohere:cohere-java:1.5.0' Wrap the client
import ai.traceai.TraceAI;
import ai.traceai.cohere.TracedCohereClient;
import com.cohere.api.Cohere;
TraceAI.initFromEnvironment();
Cohere client = Cohere.builder()
.token(System.getenv("COHERE_API_KEY"))
.build();
TracedCohereClient traced = new TracedCohereClient(client);
Chat
import com.cohere.api.requests.ChatRequest;
import com.cohere.api.types.NonStreamedChatResponse;
NonStreamedChatResponse response = traced.chat(ChatRequest.builder()
.message("What is the capital of France?")
.model("command-r-plus")
.temperature(0.7)
.build());
System.out.println(response.getText());
Span created: “Cohere Chat” with kind LLM
Embeddings
import com.cohere.api.requests.EmbedRequest;
import com.cohere.api.types.EmbedResponse;
EmbedResponse response = traced.embed(EmbedRequest.builder()
.texts(List.of("Hello world", "Goodbye world"))
.model("embed-english-v3.0")
.inputType(EmbedInputType.SEARCH_DOCUMENT)
.build());
// EmbedResponse is a union type - use the visitor pattern to access results
response.visit(new EmbedResponse.Visitor<Void>() {
@Override
public Void visitEmbeddingsFloats(EmbedFloatsResponse floats) {
System.out.println("Vectors: " + floats.getEmbeddings().size());
return null;
}
@Override
public Void visitEmbeddingsByType(EmbedByTypeResponse byType) {
System.out.println("Vectors: " + byType.getEmbeddings().getFloat_().size());
return null;
}
@Override
public Void _visitUnknown(Object unknown) {
return null;
}
});
Span created: “Cohere Embed” with kind EMBEDDING
Reranking
Cohere is the only Java integration with reranking. Uses FISpanKind.RERANKER.
import com.cohere.api.requests.RerankRequest;
import com.cohere.api.types.RerankResponse;
RerankResponse response = traced.rerank(RerankRequest.builder()
.query("What is the capital of France?")
.documents(List.of(
RerankRequestDocumentsItem.of("Paris is the capital of France."),
RerankRequestDocumentsItem.of("Berlin is the capital of Germany."),
RerankRequestDocumentsItem.of("The Eiffel Tower is in Paris.")
))
.model("rerank-english-v3.0")
.topN(2)
.build());
for (var result : response.getResults()) {
System.out.println("Index: " + result.getIndex() + ", Score: " + result.getRelevanceScore());
}
Span created: “Cohere Rerank” with kind RERANKER
What gets captured
Chat spans
| Attribute | Example |
|---|---|
llm.system | cohere |
llm.provider | cohere |
llm.request.model | command-r-plus |
llm.request.temperature | 0.7 |
llm.request.max_tokens | 1024 |
llm.token_count.prompt | 10 |
llm.token_count.completion | 25 |
llm.token_count.total | 35 |
cohere.preamble | Preamble text if provided |
| Input/output messages | Chat history + current message |
Embedding spans
| Attribute | Example |
|---|---|
embedding.model_name | embed-english-v3.0 |
embedding.vector_count | 2 |
cohere.input_type | search_document |
Reranker spans
| Attribute | Example |
|---|---|
gen_ai.reranker.query | The query text |
gen_ai.reranker.input_documents | Number of input documents |
cohere.rerank.top_score | 0.98 |
cohere.rerank.top_index | 0 |
cohere.rerank.search_units | Cohere search units consumed |
Accessing the original client
Cohere original = traced.unwrap(); Was this page helpful?