Cohere (Java)

Trace Cohere chat, embedding, and reranking operations in Java with TracedCohereClient.

📝
TL;DR
  • TracedCohereClient wraps the Cohere Java SDK (com.cohere.api)
  • Three operations: chat(), embed(), and rerank()
  • Reranking uses RERANKER span 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

AttributeExample
llm.systemcohere
llm.providercohere
llm.request.modelcommand-r-plus
llm.request.temperature0.7
llm.request.max_tokens1024
llm.token_count.prompt10
llm.token_count.completion25
llm.token_count.total35
cohere.preamblePreamble text if provided
Input/output messagesChat history + current message

Embedding spans

AttributeExample
embedding.model_nameembed-english-v3.0
embedding.vector_count2
cohere.input_typesearch_document

Reranker spans

AttributeExample
gen_ai.reranker.queryThe query text
gen_ai.reranker.input_documentsNumber of input documents
cohere.rerank.top_score0.98
cohere.rerank.top_index0
cohere.rerank.search_unitsCohere search units consumed

Accessing the original client

Cohere original = traced.unwrap();
Was this page helpful?

Questions & Discussion