Langchain summarize
Summarization¶
In this example, you will learn how to create a summarization app and evaluate + track it in TruLens
Import libraries¶
In [ ]:
Copied!
# !pip install trulens trulens-apps-langchain trulens-providers-openai langchain==0.0.283 langchain_community
# !pip install trulens trulens-apps-langchain trulens-providers-openai langchain==0.0.283 langchain_community
In [ ]:
Copied!
from langchain.chains.summarize import load_summarize_chain
from langchain.text_splitter import RecursiveCharacterTextSplitter
from trulens.apps.langchain import Feedback
from trulens.apps.langchain import FeedbackMode
from trulens.apps.langchain import Query
from trulens.apps.langchain import TruSession
from trulens.apps.langchain import TruChain
from trulens.providers.openai import OpenAI
session = TruSession()
from langchain.chains.summarize import load_summarize_chain
from langchain.text_splitter import RecursiveCharacterTextSplitter
from trulens.apps.langchain import Feedback
from trulens.apps.langchain import FeedbackMode
from trulens.apps.langchain import Query
from trulens.apps.langchain import TruSession
from trulens.apps.langchain import TruChain
from trulens.providers.openai import OpenAI
session = TruSession()
Set API Keys¶
For this example, we need API keys for the Huggingface and OpenAI
In [ ]:
Copied!
import os
os.environ["OPENAI_API_KEY"] = "..."
os.environ["HUGGINGFACE_API_KEY"] = "..."
import os
os.environ["OPENAI_API_KEY"] = "..."
os.environ["HUGGINGFACE_API_KEY"] = "..."
In [ ]:
Copied!
provider = OpenAI()
# Define a moderation feedback function using HuggingFace.
mod_not_hate = Feedback(provider.moderation_not_hate).on(
text=Query.RecordInput[:].page_content
)
def wrap_chain_trulens(chain):
return TruChain(
chain,
app_name="ChainOAI",
feedbacks=[mod_not_hate],
feedback_mode=FeedbackMode.WITH_APP, # calls to TruChain will block until feedback is done evaluating
)
def get_summary_model(text):
"""
Produce summary chain, given input text.
"""
llm = OpenAI(temperature=0, openai_api_key="")
text_splitter = RecursiveCharacterTextSplitter(
separators=["\n\n", "\n", " "], chunk_size=8000, chunk_overlap=350
)
docs = text_splitter.create_documents([text])
print(f"You now have {len(docs)} docs instead of 1 piece of text.")
return docs, load_summarize_chain(llm=llm, chain_type="map_reduce")
provider = OpenAI()
# Define a moderation feedback function using HuggingFace.
mod_not_hate = Feedback(provider.moderation_not_hate).on(
text=Query.RecordInput[:].page_content
)
def wrap_chain_trulens(chain):
return TruChain(
chain,
app_name="ChainOAI",
feedbacks=[mod_not_hate],
feedback_mode=FeedbackMode.WITH_APP, # calls to TruChain will block until feedback is done evaluating
)
def get_summary_model(text):
"""
Produce summary chain, given input text.
"""
llm = OpenAI(temperature=0, openai_api_key="")
text_splitter = RecursiveCharacterTextSplitter(
separators=["\n\n", "\n", " "], chunk_size=8000, chunk_overlap=350
)
docs = text_splitter.create_documents([text])
print(f"You now have {len(docs)} docs instead of 1 piece of text.")
return docs, load_summarize_chain(llm=llm, chain_type="map_reduce")
In [ ]:
Copied!
from datasets import load_dataset
billsum = load_dataset("billsum", split="ca_test")
text = billsum["text"][0]
docs, chain = get_summary_model(text)
# use wrapped chain as context manager
with wrap_chain_trulens(chain) as recording:
chain(docs)
from datasets import load_dataset
billsum = load_dataset("billsum", split="ca_test")
text = billsum["text"][0]
docs, chain = get_summary_model(text)
# use wrapped chain as context manager
with wrap_chain_trulens(chain) as recording:
chain(docs)
Run the TruLens dashboard¶
In [ ]:
Copied!
from trulens.dashboard import run_dashboard
run_dashboard(session)
from trulens.dashboard import run_dashboard
run_dashboard(session)