Skip to main content

OpenTelemetry instrumentation for Cohere - embeddings, chat, and rerank

Project description

TraceAI Cohere Instrumentation

OpenTelemetry instrumentation for Cohere - embeddings, chat, and rerank APIs.

Installation

pip install traceai-cohere

Features

  • Automatic tracing of Cohere API calls
  • Support for chat, embed, and rerank endpoints
  • Streaming response support
  • Token usage tracking
  • Rerank relevance scores
  • Full OpenTelemetry semantic conventions compliance

Usage

Basic Setup

import cohere
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

from traceai_cohere import CohereInstrumentor

# Set up tracing
provider = TracerProvider()
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)

# Instrument Cohere
CohereInstrumentor().instrument(tracer_provider=provider)

# Use Cohere
client = cohere.Client(api_key="your-api-key")
response = client.chat(message="Hello!")
print(response.text)

Chat

import cohere

client = cohere.Client()

# Simple chat
response = client.chat(
    model="command-r-plus",
    message="What is machine learning?"
)
print(response.text)

# With chat history
response = client.chat(
    model="command-r-plus",
    message="What's my name?",
    chat_history=[
        {"role": "USER", "message": "My name is Alice"},
        {"role": "CHATBOT", "message": "Hello Alice!"}
    ]
)

# With preamble (system prompt)
response = client.chat(
    model="command-r-plus",
    message="Write a poem",
    preamble="You are a creative poet who writes in haiku format."
)

Streaming Chat

import cohere

client = cohere.Client()

# Streaming
for event in client.chat_stream(
    model="command-r-plus",
    message="Tell me a story"
):
    if event.event_type == "text-generation":
        print(event.text, end="", flush=True)

Embeddings

import cohere

client = cohere.Client()

# Generate embeddings
response = client.embed(
    model="embed-english-v3.0",
    texts=["Hello world", "Machine learning is great"],
    input_type="search_document"
)
print(f"Generated {len(response.embeddings)} embeddings")
print(f"Dimensions: {len(response.embeddings[0])}")

Rerank (for RAG)

import cohere

client = cohere.Client()

# Rerank documents for a query
query = "What is the capital of France?"
documents = [
    "Paris is the capital of France.",
    "London is the capital of England.",
    "The Eiffel Tower is in Paris.",
    "France is a country in Europe."
]

response = client.rerank(
    model="rerank-english-v3.0",
    query=query,
    documents=documents,
    top_n=3
)

for result in response.results:
    print(f"Index: {result.index}, Score: {result.relevance_score:.4f}")
    print(f"  {documents[result.index]}")

RAG with Cohere

import cohere

client = cohere.Client()

# Documents for context
documents = [
    {"title": "Paris", "text": "Paris is the capital of France."},
    {"title": "London", "text": "London is the capital of England."},
]

response = client.chat(
    model="command-r-plus",
    message="What is the capital of France?",
    documents=documents
)

print(response.text)
if response.citations:
    print("\nCitations:")
    for citation in response.citations:
        print(f"  - {citation}")

Tool Use

import cohere

client = cohere.Client()

tools = [
    {
        "name": "get_weather",
        "description": "Get the weather for a location",
        "parameter_definitions": {
            "location": {
                "type": "str",
                "description": "The city name",
                "required": True
            }
        }
    }
]

response = client.chat(
    model="command-r-plus",
    message="What's the weather in Paris?",
    tools=tools
)

if response.tool_calls:
    for tool_call in response.tool_calls:
        print(f"Tool: {tool_call.name}")
        print(f"Parameters: {tool_call.parameters}")

Configuration Options

TraceConfig

from fi_instrumentation import TraceConfig
from traceai_cohere import CohereInstrumentor

config = TraceConfig(
    hide_inputs=False,
    hide_outputs=False,
)

CohereInstrumentor().instrument(
    tracer_provider=provider,
    config=config
)

Captured Attributes

Chat Attributes

Attribute Description
fi.span.kind "LLM"
llm.system "cohere"
llm.model Model name (command-r-plus, etc.)
llm.token_count.prompt Input token count
llm.token_count.completion Output token count
cohere.finish_reason Response finish reason
cohere.citations_count Number of citations
cohere.tool_calls_count Number of tool calls

Embed Attributes

Attribute Description
fi.span.kind "EMBEDDING"
embedding.model Embedding model name
cohere.texts_count Number of texts embedded
cohere.embedding_dimensions Vector dimensions

Rerank Attributes

Attribute Description
fi.span.kind "RERANKER"
reranker.model Rerank model name
reranker.query Search query
reranker.top_k Top N results requested
cohere.results_count Number of results
cohere.rerank.scores Relevance scores

Available Models

Category Models
Chat command-r-plus, command-r, command
Embed embed-english-v3.0, embed-multilingual-v3.0
Rerank rerank-english-v3.0, rerank-multilingual-v3.0

Real-World Use Cases

Semantic Search

import cohere

client = cohere.Client()

# Index documents
documents = ["doc1", "doc2", "doc3"]
doc_embeddings = client.embed(
    model="embed-english-v3.0",
    texts=documents,
    input_type="search_document"
).embeddings

# Search query
query_embedding = client.embed(
    model="embed-english-v3.0",
    texts=["search query"],
    input_type="search_query"
).embeddings[0]

# Compute similarity (not shown: use vector DB)

Two-Stage RAG

import cohere

client = cohere.Client()

# Stage 1: Semantic search (not shown)
initial_results = ["doc1", "doc2", "doc3", "doc4", "doc5"]

# Stage 2: Rerank for precision
reranked = client.rerank(
    model="rerank-english-v3.0",
    query="user question",
    documents=initial_results,
    top_n=3
)

# Stage 3: Generate with top results
top_docs = [initial_results[r.index] for r in reranked.results]
response = client.chat(
    model="command-r-plus",
    message="user question",
    documents=[{"text": d} for d in top_docs]
)

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

traceai_cohere-0.1.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

traceai_cohere-0.1.0-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file traceai_cohere-0.1.0.tar.gz.

File metadata

  • Download URL: traceai_cohere-0.1.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for traceai_cohere-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1fd1686870fa7b254bd1d9b7868c6ad0b96da4a28ec1f0412348c472b3c8b7a8
MD5 9bd02c85a3ceb89e0d0f2f26e854cd69
BLAKE2b-256 c311db1df89012fc6df788bc40b9401edf8a6fc042b785715f138c78063ca2bf

See more details on using hashes here.

File details

Details for the file traceai_cohere-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: traceai_cohere-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for traceai_cohere-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7ccdc747c64333b2f6f4fe890ba9e8360839ffd4117af1edfb90b6d1b665f68
MD5 6ec27657c5285065290596a50b51d300
BLAKE2b-256 2fcc280a734e0b0db2ad87dd5157d11d23280e5b7e481c3e6d1254ad70c17a6d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page