📓 Text to Text Quickstart - 🦑 TruLens

📓 Text to Text Quickstart

In this quickstart you will create a simple text to text application and learn how to log it and get feedback.

Setup

Add API keys

For this quickstart you will need an OpenAI Key.

# !pip install trulens trulens-providers-openai openai
import os

if "OPENAI_API_KEY" not in os.environ:
    os.environ["OPENAI_API_KEY"] = "sk-proj-..."

Import from TruLens

# Create openai client
from openai import OpenAI

# Imports main tools:
from trulens.core import Metric
from trulens.core import Selector
from trulens.core import TruSession
from trulens.providers.openai import OpenAI as fOpenAI

client = OpenAI()
session = TruSession()
session.reset_database()

Create Simple Text to Text Application

This example uses a bare bones OpenAI LLM, and a non-LLM just for demonstration purposes.

def llm_standalone(prompt):
    return (
        client.chat.completions.create(
            model="gpt-4.1-mini",
            messages=[
                {
                    "role": "system",
                    "content": "You are a question and answer bot, and you answer super upbeat.",
                },
                {"role": "user", "content": prompt},
            ],
        )
        .choices[0]
        .message.content
    )

Send your first request

prompt_input = "How good is language AI?"
prompt_output = llm_standalone(prompt_input)
prompt_output

Initialize Feedback Function(s)

# Initialize OpenAI-based feedback function collection class:
fopenai = fOpenAI()

# Define a relevance function from openai
f_answer_relevance = Metric(
    implementation=fopenai.relevance,
    selectors={
        "prompt": Selector.select_record_input(),
        "response": Selector.select_record_output(),
    },
)

Instrument the callable for logging with TruLens

from trulens.apps.basic import TruBasicApp

tru_llm_standalone_recorder = TruBasicApp(
    llm_standalone, app_name="Happy Bot", feedbacks=[f_answer_relevance]
)
with tru_llm_standalone_recorder as recording:
    tru_llm_standalone_recorder.app(prompt_input)

Explore in a Dashboard

from trulens.dashboard import run_dashboard

run_dashboard(session)  # open a local streamlit app to explore

# stop_dashboard(session) # stop if needed

Or view results directly in your notebook

session.get_records_and_feedback()[0]