Observability for Predibase with Opik

Predibase is a platform for fine-tuning and serving open-source Large Language Models (LLMs). It’s built on top of open-source LoRAX.

Account Setup

Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

Tracking your LLM calls

Predibase can be used to serve open-source LLMs and is available as a model provider in LangChain. We will leverage the Opik integration with LangChain to track the LLM calls made using Predibase models.

Getting Started

Installation

To use the Opik integration with Predibase, you’ll need to have both the opik, predibase and langchain packages installed. You can install them using pip:

$pip install opik predibase langchain

Configuring Opik

Configure the Opik Python SDK for your deployment type. See the Python SDK Configuration guide for detailed instructions on:

  • CLI configuration: opik configure
  • Code configuration: opik.configure()
  • Self-hosted vs Cloud vs Enterprise setup
  • Configuration files and environment variables

Configuring Predibase

You will also need to set the PREDIBASE_API_TOKEN environment variable to your Predibase API token. You can set it as an environment variable:

$export PREDIBASE_API_TOKEN=<your-predibase-api-token>

Or set it programmatically:

1import os
2import getpass
3
4if "PREDIBASE_API_TOKEN" not in os.environ:
5 os.environ["PREDIBASE_API_TOKEN"] = getpass.getpass("Enter your Predibase API token: ")

Logging LLM calls

In order to log the LLM calls to Opik, you will need to wrap the Predibase model with the OpikTracer from the LangChain integration. When making calls with that wrapped model, all calls will be logged to Opik:

1import os
2from langchain_community.llms import Predibase
3from opik.integrations.langchain import OpikTracer
4
5os.environ["OPIK_PROJECT_NAME"] = "predibase-integration-demo"
6
7# Create the Opik tracer
8opik_tracer = OpikTracer(tags=["predibase", "langchain"])
9
10# Create Predibase model
11model = Predibase(
12 model="mistral-7b",
13 predibase_api_key=os.environ.get("PREDIBASE_API_TOKEN"),
14)
15
16# Test the model with Opik tracing
17response = model.invoke(
18 "Can you recommend me a nice dry wine?",
19 config={
20 "temperature": 0.5,
21 "max_new_tokens": 1024,
22 "callbacks": [opik_tracer]
23 }
24)
25print(response)

In addition to passing the OpikTracer to the invoke method, you can also define it during the creation of the Predibase object:

1model = Predibase(
2 model="mistral-7b",
3 predibase_api_key=os.environ.get("PREDIBASE_API_TOKEN"),
4).with_config({"callbacks": [opik_tracer]})

You can learn more about the Opik integration with LangChain in our LangChain integration guide.

The trace will now be available in the Opik UI for further analysis.

Advanced Usage

SequentialChain Example

Now, let’s create a more complex chain and run it with Opik tracing:

1from langchain.chains import LLMChain, SimpleSequentialChain
2from langchain_core.prompts import PromptTemplate
3
4# Synopsis chain
5template = """You are a playwright. Given the title of play, it is your job to write a synopsis for that title.
6
7Title: {title}
8Playwright: This is a synopsis for the above play:"""
9prompt_template = PromptTemplate(input_variables=["title"], template=template)
10synopsis_chain = LLMChain(llm=model, prompt=prompt_template)
11
12# Review chain
13template = """You are a play critic from the New York Times. Given the synopsis of play, it is your job to write a review for that play.
14
15Play Synopsis:
16{synopsis}
17Review from a New York Times play critic of the above play:"""
18prompt_template = PromptTemplate(input_variables=["synopsis"], template=template)
19review_chain = LLMChain(llm=model, prompt=prompt_template)
20
21# Overall chain
22overall_chain = SimpleSequentialChain(
23 chains=[synopsis_chain, review_chain], verbose=True
24)
25
26# Run the chain with Opik tracing
27review = overall_chain.run("Tragedy at sunset on the beach", callbacks=[opik_tracer])
28print(review)

Accessing Logged Traces

We can access the trace IDs collected by the Opik tracer:

1traces = opik_tracer.created_traces()
2print("Collected trace IDs:", [trace.id for trace in traces])
3
4# Flush traces to ensure all data is logged
5opik_tracer.flush()

Fine-tuned LLM Example

Finally, let’s use a fine-tuned model with Opik tracing:

Note: In order to use a fine-tuned model, you will need to have access to the model and the correct model ID. The code below will return a NotFoundError unless the model and adapter_id are updated.

1fine_tuned_model = Predibase(
2 model="my-base-LLM",
3 predibase_api_key=os.environ.get("PREDIBASE_API_TOKEN"),
4 predibase_sdk_version=None,
5 adapter_id="my-finetuned-adapter-id",
6 adapter_version=1,
7 **{
8 "api_token": os.environ.get("HUGGING_FACE_HUB_TOKEN"),
9 "max_new_tokens": 5,
10 },
11)
12
13# Configure the Opik tracer
14fine_tuned_model = fine_tuned_model.with_config({"callbacks": [opik_tracer]})
15
16# Invoke the fine-tuned model
17response = fine_tuned_model.invoke(
18 "Can you help categorize the following emails into positive, negative, and neutral?",
19 **{"temperature": 0.5, "max_new_tokens": 1024},
20)
21print(response)
22
23# Final flush to ensure all traces are logged
24opik_tracer.flush()

Tracking your fine-tuning training runs

If you are using Predibase to fine-tune an LLM, we recommend using Predibase’s integration with Comet’s Experiment Management functionality. You can learn more about how to set this up in the Comet integration guide in the Predibase documentation. If you are already using an Experiment Tracking platform, worth checking if it has an integration with Predibase.