5a558eb09e
TypeScript SDK Compatibility V1.x E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / TypeScript SDK Compatibility V1.x E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
TypeScript SDK E2E Tests / TypeScript SDK E2E Tests Node ${{matrix.node_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
Python SDK E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK E2E Tests / Python SDK E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK E2E Tests / build-opik (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Select Python version matrix (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / Python SDK Compatibility V1.x E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Python SDK Compatibility V1.x E2E Tests / build-opik (push) Has been cancelled
TypeScript SDK E2E Tests / Select Node version matrix (push) Has been cancelled
TypeScript SDK E2E Tests / build-opik (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer E2E Tests Python ${{matrix.python_version}} (push) Has been cancelled
Opik Optimizer - E2E Tests / Opik Optimizer Integration Smoke Tests (push) Has been cancelled
🐙 Code Quality / detect (push) Has been cancelled
🐙 Code Quality / lint (${{ matrix.leg.name }}) (push) Has been cancelled
🐙 Code Quality / summary (push) Has been cancelled
TypeScript SDK Library Integration Tests / Check Secrets (push) Has been cancelled
TypeScript SDK Library Integration Tests / opik-vercel (Vercel AI SDK / eve) (push) Has been cancelled
SDK Library Integration Tests Runner / Check Secrets (push) Has been cancelled
SDK Library Integration Tests Runner / Missed OpenAI API Key Warning (push) Has been cancelled
SDK Library Integration Tests Runner / Build (push) Has been cancelled
SDK Library Integration Tests Runner / openai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_tests (push) Has been cancelled
SDK Library Integration Tests Runner / langchain_legacy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / llama_index_tests (push) Has been cancelled
SDK Library Integration Tests Runner / anthropic_tests (push) Has been cancelled
SDK Library Integration Tests Runner / mistral_tests (push) Has been cancelled
SDK Library Integration Tests Runner / groq_tests (push) Has been cancelled
SDK Library Integration Tests Runner / aisuite_tests (push) Has been cancelled
SDK Library Integration Tests Runner / haystack_tests (push) Has been cancelled
SDK Library Integration Tests Runner / dspy_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / crewai_v1_tests (push) Has been cancelled
SDK Library Integration Tests Runner / genai_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_tests (push) Has been cancelled
SDK Library Integration Tests Runner / adk_legacy_1_3_0_tests (push) Has been cancelled
SDK Library Integration Tests Runner / evaluation_metrics_tests (push) Has been cancelled
SDK Library Integration Tests Runner / bedrock_tests (push) Has been cancelled
SDK Library Integration Tests Runner / litellm_tests (push) Has been cancelled
SDK Library Integration Tests Runner / harbor_tests (push) Has been cancelled
SDK Library Integration Tests Runner / Slack Notification (push) Has been cancelled
Lint Opik Helm Chart / render-equality (push) Has been cancelled
Opik Optimizer - Unit Tests / Opik Optimizer Unit Tests Python ${{matrix.python_version}} (push) Has been cancelled
Python BE E2E Tests / Python BE E2E (push) Has been cancelled
Python Backend Tests / run-python-backend-tests (push) Has been cancelled
Python SDK Unit Tests / Python SDK Unit Tests ${{matrix.python_version}} (push) Has been cancelled
Release Drafter / update_release_draft (push) Has been cancelled
SDK E2E Libraries Integration Tests / Check Secrets (push) Has been cancelled
SDK E2E Libraries Integration Tests / Missed OpenAI API Key Warning (push) Has been cancelled
SDK E2E Libraries Integration Tests / build-opik (push) Has been cancelled
SDK E2E Libraries Integration Tests / E2E Lib Integration Python ${{matrix.python_version}} (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-gemini) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-langchain) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-openai) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-otel) (push) Has been cancelled
TypeScript SDK Integration Build & Publish / build-and-publish (opik-vercel) (push) Has been cancelled
TypeScript SDK Build & Publish / build-and-publish (push) Has been cancelled
TypeScript SDK Unit Tests / Test on Node ${{ matrix.node-version }} (push) Has been cancelled
Backend Tests / discover-tests (push) Has been cancelled
Backend Tests / ${{ matrix.name }} (push) Has been cancelled
Build and Publish SDK / build-and-publish (push) Has been cancelled
Build Opik Docker Images / set-version (push) Has been cancelled
Build Opik Docker Images / build-backend (push) Has been cancelled
Build Opik Docker Images / build-sandbox-executor-python (push) Has been cancelled
Build Opik Docker Images / build-python-backend (push) Has been cancelled
Build Opik Docker Images / build-frontend (push) Has been cancelled
Build Opik Docker Images / create-git-tag (push) Has been cancelled
ClickHouse Migration Cluster Check / validate-clickhouse-migrations (push) Has been cancelled
Docs - Publish / run (push) Has been cancelled
E2E Tests - Post Merge (v2) / 🧪 E2E v2 Tests (${{ github.event.inputs.tier || 't1' }}) (push) Has been cancelled
E2E Tests - Post Merge (v2) / 📢 Slack Notification (push) Has been cancelled
Frontend Unit Tests / Test on Node 20 (push) Has been cancelled
Guardrails E2E Tests / Select Python version matrix (push) Has been cancelled
Guardrails E2E Tests / Guardrails E2E Tests ${{matrix.python_version}} (push) Has been cancelled
Guardrails E2E Tests / 📢 Slack Notification (push) Has been cancelled
Guardrails Backend Unit Tests / Guardrails Backend Unit Tests (push) Has been cancelled
Guardrails Backend Unit Tests / 📢 Slack Notification (push) Has been cancelled
Lint Opik Helm Chart / lint-helm-chart (Helm v3.21.0) (push) Has been cancelled
Lint Opik Helm Chart / lint-helm-chart (Helm v4.2.0) (push) Has been cancelled
Lint Opik Helm Chart / unittest-helm-chart (push) Has been cancelled
144 lines
4.6 KiB
Plaintext
144 lines
4.6 KiB
Plaintext
---
|
|
description: Start here to integrate Opik into your Cohere-based genai application
|
|
for end-to-end LLM observability, unit testing, and optimization.
|
|
headline: Cohere | Opik Documentation
|
|
og:description: Learn to integrate Opik with Cohere using OpenAI SDK for efficient
|
|
tracking and evaluation of your API calls in Opik projects.
|
|
og:site_name: Opik Documentation
|
|
og:title: Integrate Cohere Models with Opik for Enhanced Tracking
|
|
title: Observability for Cohere with Opik
|
|
---
|
|
|
|
[Cohere](https://cohere.com/) provides state-of-the-art large language models that excel at text generation, summarization, classification, and retrieval-augmented generation.
|
|
|
|
This guide explains how to integrate Opik with Cohere using the OpenAI SDK Compatibility API. By using the `track_openai` method provided by opik with Cohere's compatibility endpoint, you can easily track and evaluate your Cohere API calls within your Opik projects as Opik will automatically log the input prompt, model used, token usage, and response generated.
|
|
|
|
## Getting Started
|
|
|
|
### Configuring Opik
|
|
|
|
To start tracking your Cohere LLM calls, you'll need to have both `opik` and `openai` packages installed. You can install them using pip:
|
|
|
|
```bash
|
|
pip install opik openai
|
|
```
|
|
|
|
In addition, you can configure Opik using the `opik configure` command which will prompt you for the correct local server address or if you are using the Cloud platform your API key:
|
|
|
|
```bash
|
|
opik configure
|
|
```
|
|
|
|
### Configuring Cohere
|
|
|
|
You'll need to set your Cohere API key as an environment variable:
|
|
|
|
```bash
|
|
export COHERE_API_KEY="YOUR_API_KEY"
|
|
```
|
|
|
|
## Tracking Cohere API calls
|
|
|
|
Leverage the OpenAI Compatibility API by replacing the base URL with Cohere's endpoint when initializing the client:
|
|
|
|
```python
|
|
import os
|
|
from opik.integrations.openai import track_openai
|
|
from openai import OpenAI
|
|
|
|
client = OpenAI(
|
|
api_key=os.environ.get("COHERE_API_KEY"),
|
|
base_url="https://api.cohere.ai/compatibility/v1" # Cohere Compatibility API endpoint
|
|
)
|
|
|
|
client = track_openai(client)
|
|
|
|
response = client.chat.completions.create(
|
|
model="command-r7b-12-2024", # Replace with the desired Cohere model
|
|
messages=[
|
|
{"role": "system", "content": "You are an assistant."},
|
|
{"role": "user", "content": "Why is tracking and evaluation of LLMs important?"}
|
|
],
|
|
temperature=0.7,
|
|
max_tokens=100
|
|
)
|
|
|
|
print(response.choices[0].message.content)
|
|
```
|
|
|
|
The `track_openai` will automatically track and log the API call, including the input prompt, model used, and response generated. You can view these logs in your Opik project dashboard.
|
|
|
|
<Frame>
|
|
<img src="/img/tracing/cohere_integration.png" />
|
|
</Frame>
|
|
|
|
## Using Cohere within a tracked function
|
|
|
|
If you are using Cohere within a function tracked with the [`@track`](/tracing/advanced/log_traces#using-function-decorators) decorator, you can use the tracked client as normal:
|
|
|
|
```python
|
|
from opik import track
|
|
from opik.integrations.openai import track_openai
|
|
from openai import OpenAI
|
|
import os
|
|
|
|
client = OpenAI(
|
|
api_key=os.environ.get("COHERE_API_KEY"),
|
|
base_url="https://api.cohere.ai/compatibility/v1"
|
|
)
|
|
tracked_client = track_openai(client)
|
|
|
|
@track
|
|
def generate_story(prompt):
|
|
response = tracked_client.chat.completions.create(
|
|
model="command-r7b-12-2024",
|
|
messages=[
|
|
{"role": "user", "content": prompt}
|
|
]
|
|
)
|
|
return response.choices[0].message.content
|
|
|
|
@track
|
|
def generate_topic():
|
|
prompt = "Generate a topic for a story about Opik."
|
|
response = tracked_client.chat.completions.create(
|
|
model="command-r7b-12-2024",
|
|
messages=[
|
|
{"role": "user", "content": prompt}
|
|
]
|
|
)
|
|
return response.choices[0].message.content
|
|
|
|
@track
|
|
def generate_opik_story():
|
|
topic = generate_topic()
|
|
story = generate_story(topic)
|
|
return story
|
|
|
|
generate_opik_story()
|
|
```
|
|
|
|
<Frame>
|
|
<img src="/img/tracing/cohere_trace_decorator.png" />
|
|
</Frame>
|
|
|
|
## Supported Cohere models
|
|
|
|
The `track_openai` wrapper with Cohere's compatibility API supports the following Cohere models:
|
|
|
|
- `command-r7b-12-2024` - Command R 7B model
|
|
- `command-r-plus` - Command R Plus model
|
|
- `command-r` - Command R model
|
|
- `command-light` - Command Light model
|
|
- `command` - Command model
|
|
|
|
## Supported OpenAI methods
|
|
|
|
The `track_openai` wrapper supports the following OpenAI methods when used with Cohere:
|
|
|
|
- `client.chat.completions.create()`, including support for stream=True mode
|
|
- `client.beta.chat.completions.parse()`
|
|
- `client.beta.chat.completions.stream()`
|
|
- `client.responses.create()`
|
|
|
|
If you would like to track another OpenAI method, please let us know by opening an issue on [GitHub](https://github.com/comet-ml/opik/issues). |