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
278 lines
6.8 KiB
ReStructuredText
278 lines
6.8 KiB
ReStructuredText
Opik
|
|
====
|
|
|
|
=============
|
|
Main features
|
|
=============
|
|
|
|
The Comet Opik platform is a suite of tools that allow you to evaluate the output of an LLM powered application.
|
|
|
|
In includes the following features:
|
|
|
|
- `Tracing <https://www.comet.com/docs/opik/tracing/log_traces>`_: Ability to log LLM calls and traces to the Opik platform.
|
|
- `LLM evaluation metrics <https://www.comet.com/docs/opik/evaluation/metrics/heuristic_metrics>`_: A set of functions that evaluate the output of an LLM, these are both heuristic metrics and LLM as a Judge.
|
|
- `Evaluation <https://www.comet.com/docs/opik//evaluation/evaluate_your_llm>`_: Ability to log test datasets in Opik and evaluate using some of our LLM evaluation metrics.
|
|
|
|
For a more detailed overview of the platform, you can refer to the `Comet Opik documentation <https://www.comet.com/docs/opik>`_.
|
|
|
|
============
|
|
Installation
|
|
============
|
|
|
|
To get start with the package, you can install it using pip::
|
|
|
|
pip install opik
|
|
|
|
To finish configuring the Opik Python SDK, we recommend running the `opik configure` command from the command line:
|
|
|
|
.. code-block:: bash
|
|
|
|
opik configure
|
|
|
|
You can also call the configure function from the Python SDK:
|
|
|
|
.. code-block:: python
|
|
|
|
import opik
|
|
|
|
opik.configure(use_local=False)
|
|
|
|
=============
|
|
Using the SDK
|
|
=============
|
|
|
|
-----------------
|
|
Logging LLM calls
|
|
-----------------
|
|
|
|
To log your first trace, you can use the `track` decorator::
|
|
|
|
from opik import track
|
|
|
|
@track
|
|
def llm_function(input: str) -> str:
|
|
# Your LLM call
|
|
# ...
|
|
|
|
return "Hello, world!"
|
|
|
|
llm_function("Hello")
|
|
|
|
**Note:** The `track` decorator supports nested functions, if you track multiple functions, each functionc call will be associated with the parent trace.
|
|
|
|
**Integrations**: If you are using LangChain or OpenAI, Comet Opik as `built-in integrations <https://www.comet.com/docs/opik/integrations/langchain>`_ for these libraries.
|
|
|
|
----------------------------
|
|
Using LLM evaluation metrics
|
|
----------------------------
|
|
|
|
The opik package includes a number of LLM evaluation metrics, these are both heuristic metrics and LLM as a Judge.
|
|
|
|
All available metrics are listed in the `metrics section <evaluation/metrics/index.html>`_.
|
|
|
|
These evaluation metrics can be used as::
|
|
|
|
from opik.evaluation.metrics import Hallucination
|
|
|
|
metric = Hallucination()
|
|
|
|
input = "What is the capital of France?"
|
|
output = "The capital of France is Paris, a city known for its iconic Eiffel Tower."
|
|
context = "Paris is the capital and most populous city of France."
|
|
|
|
score = metric.score(input, output, context)
|
|
print(f"Hallucination score: {score}")
|
|
|
|
-------------------
|
|
Running evaluations
|
|
-------------------
|
|
|
|
Evaluations are run using the `evaluate` function, this function takes a dataset, a task and a list of metrics and returns a dictionary of scores::
|
|
|
|
from opik import Opik, track
|
|
from opik.evaluation import evaluate
|
|
from opik.evaluation.metrics import EqualsMetric, HallucinationMetric
|
|
from opik.integrations.openai import track_openai
|
|
from typing import Dict
|
|
|
|
from typing import Dict
|
|
|
|
# Define the task to evaluate
|
|
openai_client = track_openai(openai.OpenAI())
|
|
|
|
@track()
|
|
def your_llm_application(input: str) -> str:
|
|
response = openai_client.chat.completions.create(
|
|
model="gpt-3.5-turbo",
|
|
messages=[{"role": "user", "content": input}],
|
|
)
|
|
|
|
return response.choices[0].message.content
|
|
|
|
@track()
|
|
def your_context_retriever(input: str) -> str:
|
|
return ["..."]
|
|
|
|
# Fetch the dataset
|
|
client = Opik()
|
|
dataset = client.get_dataset(name="your-dataset-name")
|
|
|
|
# Define the metrics
|
|
equals_metric = EqualsMetric()
|
|
hallucination_metric = HallucinationMetric()
|
|
|
|
# Define and run the evaluation
|
|
def evaluation_task(x: Dict):
|
|
return {
|
|
"input": x.input['user_question'],
|
|
"output": your_llm_application(x.input['user_question']),
|
|
"context": your_context_retriever(x.input['user_question'])
|
|
}
|
|
|
|
evaluation = evaluate(
|
|
dataset=dataset,
|
|
task=evaluation_task,
|
|
metrics=[equals_metric, hallucination_metric],
|
|
)
|
|
|
|
---------------
|
|
Storing prompts
|
|
---------------
|
|
|
|
You can store prompts in the Opik library using the `Prompt` and `ChatPrompt` objects:
|
|
|
|
**Text Prompts:**
|
|
|
|
.. code-block:: python
|
|
|
|
import opik
|
|
|
|
prompt = opik.Prompt(name="my-prompt", prompt="Write a summary of the following text: {{text}}")
|
|
|
|
**Chat Prompts:**
|
|
|
|
.. code-block:: python
|
|
|
|
import opik
|
|
|
|
messages = [
|
|
{"role": "system", "content": "You are a helpful assistant."},
|
|
{"role": "user", "content": "Hello, {{name}}!"}
|
|
]
|
|
chat_prompt = opik.ChatPrompt(name="my-chat-prompt", messages=messages)
|
|
|
|
=========
|
|
Reference
|
|
=========
|
|
|
|
You can learn more about the `opik` python SDK in the following sections:
|
|
|
|
.. toctree::
|
|
:maxdepth: 1
|
|
|
|
Opik
|
|
track
|
|
configure
|
|
opik_context/index
|
|
context_manager/index
|
|
|
|
.. toctree::
|
|
:caption: Integrations
|
|
:maxdepth: 1
|
|
|
|
integrations/anthropic/index
|
|
integrations/bedrock/index
|
|
integrations/crewai/index
|
|
integrations/dspy/index
|
|
integrations/guardrails/index
|
|
integrations/haystack/index
|
|
integrations/langchain/index
|
|
integrations/llama_index/index
|
|
integrations/openai/index
|
|
integrations/adk/index
|
|
|
|
.. toctree::
|
|
:caption: Evaluation
|
|
:maxdepth: 1
|
|
|
|
evaluation/Dataset
|
|
evaluation/evaluate
|
|
evaluation/evaluate_prompt
|
|
evaluation/evaluate_experiment
|
|
evaluation/evaluate_threads
|
|
evaluation/metrics/index
|
|
message_processing_emulation/index
|
|
|
|
.. toctree::
|
|
:caption: Prompt management
|
|
:maxdepth: 1
|
|
|
|
library/Prompt
|
|
library/ChatPrompt
|
|
|
|
.. toctree::
|
|
:caption: Guardrails
|
|
:maxdepth: 1
|
|
|
|
guardrails/guardrail
|
|
guardrails/topic
|
|
guardrails/pii
|
|
guardrails/validation_response
|
|
|
|
.. toctree::
|
|
:caption: Testing
|
|
:maxdepth: 1
|
|
|
|
testing/llm_unit
|
|
|
|
.. toctree::
|
|
:caption: Simulation
|
|
:maxdepth: 1
|
|
|
|
simulation/index
|
|
|
|
.. toctree::
|
|
:caption: REST API Reference
|
|
:maxdepth: 1
|
|
|
|
rest_api/overview
|
|
rest_api/clients/index
|
|
rest_api/objects
|
|
|
|
.. toctree::
|
|
:caption: Objects
|
|
:maxdepth: 1
|
|
|
|
Objects/Trace.rst
|
|
Objects/TraceData.rst
|
|
Objects/TracePublic.rst
|
|
Objects/Span.rst
|
|
Objects/SpanData.rst
|
|
Objects/SpanPublic.rst
|
|
Objects/Attachment.rst
|
|
Objects/AttachmentClient.rst
|
|
Objects/FeedbackScoreDict.rst
|
|
Objects/Experiment.rst
|
|
Objects/ExperimentItemContent.rst
|
|
Objects/ExperimentItemReferences.rst
|
|
Objects/EvaluationResult.rst
|
|
Objects/TestResult.rst
|
|
Objects/Prompt.rst
|
|
Objects/ChatPrompt.rst
|
|
Objects/ScoreResult.rst
|
|
Objects/OpikBaseModel.rst
|
|
Objects/LiteLLMChatModel.rst
|
|
Objects/DistributedTraceHeadersDict.rst
|
|
|
|
.. toctree::
|
|
:maxdepth: 1
|
|
:caption: Command Line Interface
|
|
|
|
cli
|
|
|
|
.. toctree::
|
|
:caption: Documentation Guides
|
|
:maxdepth: 1
|
|
|
|
Opik Documentation <https://www.comet.com/docs/opik/>
|