LlamaIndex Stream¶
This notebook demonstrates how to monitor Llama-index streaming apps with TruLens.
Import from LlamaIndex and TruLens¶
In [ ]:
Copied!
# !pip install trulens trulens-apps-llamaindex trulens-providers-openai 'llama_index==0.10.11' llama-index-readers-web openai
# !pip install trulens trulens-apps-llamaindex trulens-providers-openai 'llama_index==0.10.11' llama-index-readers-web openai
In [ ]:
Copied!
from llama_index.core import VectorStoreIndex
from llama_index.readers.web import SimpleWebPageReader
from trulens.core import Feedback
from trulens.core import TruSession
from trulens.apps.llamaindex import TruLlama
from trulens.providers.openai import OpenAI
session = TruSession()
from llama_index.core import VectorStoreIndex
from llama_index.readers.web import SimpleWebPageReader
from trulens.core import Feedback
from trulens.core import TruSession
from trulens.apps.llamaindex import TruLlama
from trulens.providers.openai import OpenAI
session = TruSession()
Add API keys¶
For this example you need an OpenAI key
In [ ]:
Copied!
import os
os.environ["OPENAI_API_KEY"] = "..."
import os
os.environ["OPENAI_API_KEY"] = "..."
Create Async App¶
In [ ]:
Copied!
documents = SimpleWebPageReader(html_to_text=True).load_data(
["http://paulgraham.com/worked.html"]
)
index = VectorStoreIndex.from_documents(documents)
chat_engine = index.as_chat_engine()
documents = SimpleWebPageReader(html_to_text=True).load_data(
["http://paulgraham.com/worked.html"]
)
index = VectorStoreIndex.from_documents(documents)
chat_engine = index.as_chat_engine()
In [ ]:
Copied!
stream = chat_engine.stream_chat("What did the author do growing up?")
for chunk in stream.response_gen:
print(chunk, end="")
stream = chat_engine.stream_chat("What did the author do growing up?")
for chunk in stream.response_gen:
print(chunk, end="")
Set up Evaluation¶
In [ ]:
Copied!
# Initialize OpenAI-based feedback function collection class:
openai = OpenAI()
# Question/answer relevance between overall question and answer.
f_qa_relevance = Feedback(
openai.relevance, name="QA Relevance"
).on_input_output()
# Initialize OpenAI-based feedback function collection class:
openai = OpenAI()
# Question/answer relevance between overall question and answer.
f_qa_relevance = Feedback(
openai.relevance, name="QA Relevance"
).on_input_output()
Create tracked app¶
In [ ]:
Copied!
tru_chat_engine_recorder = TruLlama(chat_engine, feedbacks=[f_qa_relevance])
tru_chat_engine_recorder = TruLlama(chat_engine, feedbacks=[f_qa_relevance])
Run Async Application with TruLens¶
In [ ]:
Copied!
with tru_chat_engine_recorder as recording:
stream = chat_engine.stream_chat("What did the author do growing up?")
for chunk in stream.response_gen:
print(chunk, end="")
record = recording.get()
with tru_chat_engine_recorder as recording:
stream = chat_engine.stream_chat("What did the author do growing up?")
for chunk in stream.response_gen:
print(chunk, end="")
record = recording.get()
In [ ]:
Copied!
# Check recorded input and output:
print(record.main_input)
print(record.main_output)
# Check recorded input and output:
print(record.main_input)
print(record.main_output)
In [ ]:
Copied!
# Check costs
record.cost
# Check costs
record.cost
In [ ]:
Copied!
# Check feedback results:
record.feedback_results[0].result()
# Check feedback results:
record.feedback_results[0].result()
In [ ]:
Copied!
from trulens.dashboard import run_dashboard
run_dashboard(session)
from trulens.dashboard import run_dashboard
run_dashboard(session)