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
289 lines
10 KiB
Plaintext
289 lines
10 KiB
Plaintext
---
|
|
headline: Evaluate single prompts | Opik Documentation
|
|
og:description: Evaluate and compare prompt performance in Opik using the playground
|
|
or Python SDK to enhance your prompt engineering process.
|
|
og:site_name: Opik Documentation
|
|
og:title: Evaluate Single Prompts Effectively - Opik
|
|
subtitle: Step by step guide on how to evaluate LLM prompts
|
|
title: Evaluate single prompts
|
|
canonical-url: https://www.comet.com/docs/opik/evaluation/getting-started
|
|
---
|
|
|
|
<Note>
|
|
In Opik 2.0, Experiments and Evaluation Suites are project-scoped. Make sure to specify a `project_name` when creating datasets and running experiments.
|
|
</Note>
|
|
|
|
When developing prompts and performing prompt engineering, it can be challenging to know if a new
|
|
prompt is better than the previous version.
|
|
|
|
Opik Experiments allow you to evaluate the prompt on multiple samples, score each LLM output and
|
|
compare the performance of different prompts.
|
|
|
|
<Frame>
|
|
<img src="/img/evaluation/experiment_items.png" />
|
|
</Frame>
|
|
|
|
There are two way to evaluate a prompt in Opik:
|
|
|
|
1. Using the prompt playground
|
|
2. Using the `evaluate_prompt` function in the Python SDK
|
|
|
|
## Using the prompt playground
|
|
|
|
The Opik playground allows you to quickly test different prompts and see how they perform.
|
|
|
|
You can compare multiple prompts to each other by clicking the `+ Add prompt` button in the top
|
|
right corner of the playground. This will allow you to enter multiple prompts and compare them side
|
|
by side.
|
|
|
|
In order to evaluate the prompts on samples, you can add variables to the prompt messages using the
|
|
`{{variable}}` syntax. You can then connect a dataset and run the prompts on each dataset item.
|
|
|
|

|
|
|
|
## Programmatically evaluating prompts
|
|
|
|
The Opik SDKs provide a simple way to evaluate prompts using the `evaluate prompt` methods. This
|
|
method allows you to specify a dataset, a prompt and a model. The prompt is then evaluated on each
|
|
dataset item and the output can then be reviewed and annotated in the Opik UI.
|
|
|
|
To run the experiment, you can use the following code:
|
|
|
|
<CodeBlocks>
|
|
```typescript title="TypeScript" language="typescript"
|
|
import { Opik, evaluatePrompt } from 'opik';
|
|
|
|
// Create a dataset that contains the samples you want to evaluate
|
|
const opikClient = new Opik();
|
|
const dataset = await opikClient.getOrCreateDataset({
|
|
name: "my_dataset",
|
|
});
|
|
await dataset.insert([
|
|
{ input: "Hello, world!", expected_output: "Hello, world!" },
|
|
{ input: "What is the capital of France?", expected_output: "Paris" },
|
|
]);
|
|
|
|
// Run the evaluation
|
|
await evaluatePrompt({
|
|
dataset,
|
|
messages: [
|
|
{ role: "user", content: "Translate the following text to French: {{input}}" },
|
|
],
|
|
model: "gpt-4o",
|
|
projectName: "my-project",
|
|
});
|
|
```
|
|
|
|
```python title="Python" language="python"
|
|
import opik
|
|
from opik.evaluation import evaluate_prompt
|
|
|
|
# Create a dataset that contains the samples you want to evaluate
|
|
opik_client = opik.Opik()
|
|
dataset = opik_client.get_or_create_dataset("my_dataset", project_name="my-project")
|
|
dataset.insert([
|
|
{"input": "Hello, world!", "expected_output": "Hello, world!"},
|
|
{"input": "What is the capital of France?", "expected_output": "Paris"},
|
|
])
|
|
|
|
# Run the evaluation
|
|
evaluate_prompt(
|
|
dataset=dataset,
|
|
messages=[
|
|
{"role": "user", "content": "Translate the following text to French: {{input}}"},
|
|
],
|
|
model="gpt-3.5-turbo",
|
|
project_name="my-project",
|
|
)
|
|
```
|
|
|
|
</CodeBlocks>
|
|
Once the evaluation is complete, you can view the responses in the Opik UI and score each LLM output.
|
|
|
|
<Frame>
|
|
<img src="/img/evaluation/experiment_items.png" />
|
|
</Frame>
|
|
|
|
### Automate the scoring process
|
|
|
|
Manually reviewing each LLM output can be time-consuming and error-prone. The `evaluate_prompt`
|
|
function allows you to specify a list of scoring metrics which allows you to score each LLM output.
|
|
Opik has a set of built-in metrics that allow you to detect hallucinations, answer relevance, etc
|
|
and if we don't have the metric you need, you can easily create your own.
|
|
|
|
You can find a full list of all the Opik supported metrics in the
|
|
[Metrics Overview](/v1/evaluation/metrics/overview) section or you can define your own metric using
|
|
[Custom Metrics](/v1/evaluation/metrics/custom_metric) section.
|
|
|
|
By adding the `scoring_metrics` parameter to the `evaluate_prompt` function, you can specify a list
|
|
of metrics to use for scoring. We will update the example above to use the `Hallucination` metric
|
|
for scoring:
|
|
|
|
<CodeBlocks>
|
|
```python title="Python" language="python"
|
|
import opik
|
|
from opik.evaluation import evaluate_prompt
|
|
from opik.evaluation.metrics import Hallucination
|
|
|
|
# Create a dataset that contains the samples you want to evaluate
|
|
opik_client = opik.Opik()
|
|
dataset = opik_client.get_or_create_dataset("my_dataset", project_name="my-project")
|
|
dataset.insert([
|
|
{"input": "Hello, world!", "expected_output": "Hello, world!"},
|
|
{"input": "What is the capital of France?", "expected_output": "Paris"},
|
|
])
|
|
|
|
# Run the evaluation
|
|
evaluate_prompt(
|
|
dataset=dataset,
|
|
messages=[
|
|
{"role": "user", "content": "Translate the following text to French: {{input}}"},
|
|
],
|
|
model="gpt-3.5-turbo",
|
|
scoring_metrics=[Hallucination()],
|
|
project_name="my-project",
|
|
)
|
|
```
|
|
|
|
```typescript title="TypeScript" language="typescript"
|
|
import { Opik, evaluatePrompt, Hallucination } from 'opik';
|
|
|
|
// Create a dataset that contains the samples you want to evaluate
|
|
const opikClient = new Opik();
|
|
const dataset = await opikClient.getOrCreateDataset({
|
|
name: "my_dataset",
|
|
});
|
|
await dataset.insert([
|
|
{ input: "Hello, world!", expected_output: "Hello, world!" },
|
|
{ input: "What is the capital of France?", expected_output: "Paris" },
|
|
]);
|
|
|
|
// Run the evaluation
|
|
await evaluatePrompt({
|
|
dataset,
|
|
messages: [
|
|
{ role: "user", content: "Translate the following text to French: {{input}}" },
|
|
],
|
|
model: "gpt-4o",
|
|
scoringMetrics: [new Hallucination()],
|
|
projectName: "my-project",
|
|
});
|
|
```
|
|
|
|
</CodeBlocks>
|
|
|
|
### Customizing the model used
|
|
|
|
You can customize the model used by create a new model using the [`LiteLLMChatModel`](https://www.comet.com/docs/opik/python-sdk-reference/Objects/LiteLLMChatModel.html) class. This supports passing additional parameters to the model like the `temperature` or base url to use for the model.
|
|
|
|
<CodeBlocks>
|
|
```python
|
|
import opik
|
|
from opik.evaluation import evaluate_prompt
|
|
from opik.evaluation.metrics import Hallucination
|
|
from opik.evaluation import models
|
|
|
|
# Create a dataset that contains the samples you want to evaluate
|
|
opik_client = opik.Opik()
|
|
dataset = opik_client.get_or_create_dataset("my_dataset", project_name="my-project")
|
|
dataset.insert([
|
|
{"input": "Hello, world!", "expected_output": "Hello, world!"},
|
|
{"input": "What is the capital of France?", "expected_output": "Paris"},
|
|
])
|
|
|
|
# Run the evaluation
|
|
evaluate_prompt(
|
|
dataset=dataset,
|
|
messages=[
|
|
{"role": "user", "content": "Translate the following text to French: {{input}}"},
|
|
],
|
|
model=models.LiteLLMChatModel(model="gpt-3.5-turbo", temperature=0),
|
|
scoring_metrics=[Hallucination()],
|
|
project_name="my-project",
|
|
)
|
|
```
|
|
|
|
```typescript title="TypeScript" language="typescript"
|
|
import { Opik, evaluatePrompt, Hallucination } from 'opik';
|
|
import { openai } from '@ai-sdk/openai';
|
|
|
|
// Create a dataset that contains the samples you want to evaluate
|
|
const opikClient = new Opik();
|
|
const dataset = await opikClient.getOrCreateDataset({
|
|
name: "my_dataset",
|
|
});
|
|
await dataset.insert([
|
|
{ input: "Hello, world!", expected_output: "Hello, world!" },
|
|
{ input: "What is the capital of France?", expected_output: "Paris" },
|
|
]);
|
|
|
|
|
|
// Define a custom model proivider with specific configuration
|
|
const openai = createOpenAI({
|
|
// custom settings https://ai-sdk.dev/providers/ai-sdk-providers/openai#setup
|
|
baseURL: "https://api.openai.com/v1"
|
|
});
|
|
|
|
// Run the evaluation
|
|
await evaluatePrompt({
|
|
dataset,
|
|
messages: [
|
|
{ role: "user", content: "Translate the following text to French: {{input}}" },
|
|
],
|
|
model: openai('gpt-4o'),
|
|
scoringMetrics: [new Hallucination()],
|
|
temperature: 0,
|
|
projectName: "my-project",
|
|
});
|
|
```
|
|
|
|
</CodeBlocks>
|
|
|
|
### Filtering dataset items
|
|
|
|
You can evaluate only a subset of your dataset items by using the `dataset_filter_string` parameter. This is useful when you want to run experiments on specific categories of data:
|
|
|
|
<CodeBlocks>
|
|
```python title="Python" language="python"
|
|
import opik
|
|
from opik.evaluation import evaluate_prompt
|
|
|
|
# Create or get a dataset
|
|
opik_client = opik.Opik()
|
|
dataset = opik_client.get_or_create_dataset("my_dataset", project_name="my-project")
|
|
|
|
# Evaluate only items with specific tags
|
|
evaluate_prompt(
|
|
dataset=dataset,
|
|
messages=[
|
|
{"role": "user", "content": "Translate the following text to French: {{input}}"},
|
|
],
|
|
model="gpt-3.5-turbo",
|
|
dataset_filter_string='tags contains "production"',
|
|
project_name="my-project",
|
|
)
|
|
|
|
# Evaluate items matching multiple conditions
|
|
evaluate_prompt(
|
|
dataset=dataset,
|
|
messages=[
|
|
{"role": "user", "content": "Answer the question: {{question}}"},
|
|
],
|
|
model="gpt-4",
|
|
dataset_filter_string='data.category = "finance" AND data.difficulty = "hard"',
|
|
project_name="my-project",
|
|
)
|
|
```
|
|
|
|
</CodeBlocks>
|
|
|
|
The filter uses Opik Query Language (OQL) syntax. For more details on filter syntax and supported columns, see [Filtering syntax](/v1/evaluation/manage_datasets#filter-syntax).
|
|
|
|
## Next steps
|
|
|
|
To evaluate complex LLM applications like RAG applications or agents, you can use the [`evaluate`](/v1/evaluation/evaluate_your_llm) function.
|
|
|
|
<Tip>
|
|
You can also compute experiment-level aggregate metrics when evaluating prompts using the `experiment_scoring_functions` parameter.
|
|
Learn more about [experiment-level metrics](/v1/evaluation/evaluate_your_llm#computing-experiment-level-metrics).
|
|
</Tip> |