📓 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.
In [ ]:
Copied!
# !pip install trulens trulens-providers-openai openai
# !pip install trulens trulens-providers-openai openai
In [ ]:
Copied!
import os
os.environ["OPENAI_API_KEY"] = "sk-..."
import os
os.environ["OPENAI_API_KEY"] = "sk-..."
Import from TruLens¶
In [ ]:
Copied!
# Create openai client
from openai import OpenAI
# Imports main tools:
from trulens.core import Feedback
from trulens.core import TruSession
from trulens.providers.openai import OpenAI as fOpenAI
client = OpenAI()
session = TruSession()
session.reset_database()
# Create openai client
from openai import OpenAI
# Imports main tools:
from trulens.core import Feedback
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.
In [ ]:
Copied!
def llm_standalone(prompt):
return (
client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "You are a question and answer bot, and you answer super upbeat.",
},
{"role": "user", "content": prompt},
],
)
.choices[0]
.message.content
)
def llm_standalone(prompt):
return (
client.chat.completions.create(
model="gpt-3.5-turbo",
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¶
In [ ]:
Copied!
prompt_input = "How good is language AI?"
prompt_output = llm_standalone(prompt_input)
prompt_output
prompt_input = "How good is language AI?"
prompt_output = llm_standalone(prompt_input)
prompt_output
Initialize Feedback Function(s)¶
In [ ]:
Copied!
# Initialize OpenAI-based feedback function collection class:
fopenai = fOpenAI()
# Define a relevance function from openai
f_answer_relevance = Feedback(fopenai.relevance).on_input_output()
# Initialize OpenAI-based feedback function collection class:
fopenai = fOpenAI()
# Define a relevance function from openai
f_answer_relevance = Feedback(fopenai.relevance).on_input_output()
Instrument the callable for logging with TruLens¶
In [ ]:
Copied!
from trulens.apps.basic import TruBasicApp
tru_llm_standalone_recorder = TruBasicApp(
llm_standalone, app_name="Happy Bot", feedbacks=[f_answer_relevance]
)
from trulens.apps.basic import TruBasicApp
tru_llm_standalone_recorder = TruBasicApp(
llm_standalone, app_name="Happy Bot", feedbacks=[f_answer_relevance]
)
In [ ]:
Copied!
with tru_llm_standalone_recorder as recording:
tru_llm_standalone_recorder.app(prompt_input)
with tru_llm_standalone_recorder as recording:
tru_llm_standalone_recorder.app(prompt_input)
Explore in a Dashboard¶
In [ ]:
Copied!
from trulens.dashboard import run_dashboard
run_dashboard(session) # open a local streamlit app to explore
# stop_dashboard(session) # stop if needed
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¶
In [ ]:
Copied!
session.get_records_and_feedback()[0]
session.get_records_and_feedback()[0]