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
Metadata
Release files for traceAI-cohere 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| traceai_cohere-0.1.0.tar.gz | 10.1 kB | Without provenance | Details for traceai_cohere-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| traceai_cohere-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for traceai_cohere-0.1.0-py3-none-any.whl |
Total release size: 21.4 kB
Release files / traceai_cohere-0.1.0.tar.gz
| Download URL | traceai_cohere-0.1.0.tar.gz |
|---|---|
| Size | 10.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1fd1686870fa7b254bd1d9b7868c6ad0b96da4a28ec1f0412348c472b3c8b7a8
|
|
BLAKE2b-256 checksum How to use checksums |
c311db1df89012fc6df788bc40b9401edf8a6fc042b785715f138c78063ca2bf
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / traceai_cohere-0.1.0-py3-none-any.whl
| Download URL | traceai_cohere-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b7ccdc747c64333b2f6f4fe890ba9e8360839ffd4117af1edfb90b6d1b665f68
|
|
BLAKE2b-256 checksum How to use checksums |
2fcc280a734e0b0db2ad87dd5157d11d23280e5b7e481c3e6d1254ad70c17a6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Security for traceAI-cohere 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.