Open Inference
Use Confident AI for LLM observability and evals for OpenInference
Overview
OpenInference is an open standard for capturing and storing AI model inferences. Confident AI allows you to trace and evaluate any application instrumented with OpenInference in just a few lines of code.
Tracing Quickstart
Install Dependencies
Run the following command to install the required packages:
pip install -U deepeval opentelemetry-sdk opentelemetry-exporter-otlp-proto-httpnpm install deepevalInstrument OpenInference
Call
instrument_openinferenceonce at startup, before your agent runs. It attaches to the active OpenTelemetryTracerProviderand begins forwarding spans to Confident AI.main.py import os from openinference.instrumentation.langchain import LangChainInstrumentor from langchain_openai import ChatOpenAI from deepeval.integrations.openinference import instrument_openinference LangChainInstrumentor().instrument() instrument_openinference() llm = ChatOpenAI(model="gpt-4o-mini") result = llm.invoke("What are LLMs?")import { instrumentOpenInference } from "deepeval/integrations/openinference"; import { registerInstrumentations } from "@opentelemetry/instrumentation"; import { OpenAIInstrumentation } from "@arizeai/openinference-instrumentation-openai"; instrumentOpenInference({ name: "OpenInference-TS-App", }); registerInstrumentations({ instrumentations: [new OpenAIInstrumentation()], }); async function main() { const { default: OpenAI } = await import("openai"); const client = new OpenAI(); await client.chat.completions.create({ model: "gpt-4o-mini", messages: [{ role: "user", content: "What is OpenInference?" }], }); }import { registerOTel } from "@vercel/otel"; import { createOpenInferenceProcessors } from "deepeval/integrations/openinference"; const deepevalProcessors = createOpenInferenceProcessors(); export function register() { registerOTel({ serviceName: "deepeval-next-app", spanProcessors: [ // Your existing processors ...deepevalProcessors, ], }); }Run your code
Get your traces by running your code as shown here:
python main.pynpx tsx file-name.tsYou can directly view the traces on Confident AI's observatory page
Advanced Usage
Logging prompts
If you are managing prompts on Confident AI and wish to log them, pass your Prompt object to instrument_openinference.
import os
from deepeval.prompt import Prompt
from langchain_openai import ChatOpenAI
from openinference.instrumentation.langchain import LangChainInstrumentor
from deepeval.integrations.openinference import instrument_openinference
prompt = Prompt(alias="my-prompt")
prompt.pull(version="00.00.01")
system_prompt = prompt.interpolate()
LangChainInstrumentor().instrument()
instrument_openinference()
llm = ChatOpenAI(model="gpt-4o-mini")
result = llm.invoke(system_prompt)import { instrumentOpenInference } from "deepeval/integrations/openinference";
import { Prompt } from "deepeval";
const prompt = new Prompt({ alias: "my-prompt" });
await prompt.pull();
instrumentOpenInference({
prompt: prompt
});Logging threads
Threads are used to group related traces together, and are useful for chat apps, agents, or any multi-turn interactions. You can learn more about threads here. Pass the thread_id to instrument_openinference.
import os
from openinference.instrumentation.langchain import LangChainInstrumentor
from langchain_openai import ChatOpenAI
from deepeval.integrations.openinference import instrument_openinference
LangChainInstrumentor().instrument()
instrument_openinference(
thread_id="thread_1",
user_id="user_1"
)
llm = ChatOpenAI(model="gpt-4o-mini")
result = llm.invoke("What are LLMs?")import { instrumentOpenInference } from "deepeval/integrations/openinference";
instrumentOpenInference({
threadId: "thread_123",
userId: "user_456",
});Trace attributes
Other trace attributes can also be passed to instrument_openinference.
import os
from openinference.instrumentation.langchain import LangChainInstrumentor
from langchain_openai import ChatOpenAI
from deepeval.integrations.openinference import instrument_openinference
LangChainInstrumentor().instrument()
instrument_openinference(
name="Name of Trace",
tags=["Tag 1", "Tag 2"],
metadata={"Key": "Value"},
user_id="user_1",
thread_id="conversation-abc123",
test_case_id="test-case-001",
turn_id="turn-1",
environment="production",
)
llm = ChatOpenAI(model="gpt-4o-mini")
result = llm.invoke("What are LLMs?")import { instrumentOpenInference } from "deepeval/integrations/openinference";
instrumentOpenInference({
name: "Custom Trace Name",
threadId: "thread_123",
userId: "user_456",
testCaseId: "test-case-001",
turnId: "turn-1",
tags: ["production", "v1"],
metadata: { "internal_version": "1.0.2" },
environment: "production"
});View Trace Attributes
namestr
The name of the trace. Learn more.
tagsList[str]
Tags are string labels that help you group related traces. Learn more.
metadataDict
Attach any metadata to the trace. Learn more.
thread_idstr
Supply the thread or conversation ID to view and evaluate conversations. Learn more.
user_idstr
Supply the user ID to enable user analytics. Learn more.
test_case_idstr
Associate this trace with a specific test case ID for offline evaluation linkage.
turn_idstr
Associate this trace with a specific conversation turn.
environmentstr
The deployment environment. Accepted values: "production", "staging", "development", "testing". Defaults to "development".
Evals Usage
Online evals
You can run online evals on your OpenInference instrumentation, which will run evaluations on all incoming traces on Confident AI's servers.
Create metric collection
Create a metric collection on Confident AI with the metrics you wish to use to evaluate your traces.
Create metric collection Run evals
Pass the
metric_collectionname (Python) ormetricCollection/traceMetricCollectionname (TypeScript) toinstrument_openinferenceto enable online evaluations at the trace level.main.py import os from openinference.instrumentation.langchain import LangChainInstrumentor from langchain_openai import ChatOpenAI from deepeval.integrations.openinference import instrument_openinference LangChainInstrumentor().instrument() instrument_openinference( name="openinference_application", metric_collection="TRACE-METRIC-COLLECTION" ) llm = ChatOpenAI(model="gpt-4o-mini") result = llm.invoke("What are LLMs?")import { instrumentOpenInference } from "deepeval/integrations/openinference"; instrumentOpenInference({ name: "Custom Trace Name", traceMetricCollection: "TRACE-METRIC-COLLECTION" });In TypeScript, pass the
llmMetricCollectionparameter toinstrumentOpenInferenceto evaluate all LLM spans against a specific metric collection.import { instrumentOpenInference } from "deepeval/integrations/openinference"; instrumentOpenInference({ name: "Custom Trace Name", llmMetricCollection: "LLM-METRIC-COLLECTION" });In TypeScript, pass the
toolMetricCollectionMapparameter toinstrumentOpenInferenceto map each tool name to its own metric collection.import { instrumentOpenInference } from "deepeval/integrations/openinference"; instrumentOpenInference({ name: "Custom Trace Name", toolMetricCollectionMap: { "get_weather": "TOOL-METRIC-COLLECTION", }, });
You can view evals on Confident AI by clicking on the link in the output printed in the console.
Last updated on