Skip to content

Commit

Permalink
Automated File Generation from Docs Notebook Changes (#682)
Browse files Browse the repository at this point in the history
Co-authored-by: joshreini1 <[email protected]>
Co-authored-by: Josh Reini <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2023
1 parent 190ab72 commit 6630bd0
Show file tree
Hide file tree
Showing 28 changed files with 544 additions and 606 deletions.
560 changes: 230 additions & 330 deletions trulens_eval/examples/quickstart/py_script_quickstarts/all_tools.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
# coding: utf-8

# # Langchain Quickstart
#
#
# In this quickstart you will create a simple LLM Chain and learn how to log it and get feedback on an LLM response.
#
#
# [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/main/trulens_eval/examples/quickstart/langchain_quickstart.ipynb)

# ## Setup
Expand All @@ -13,46 +13,45 @@

# In[ ]:


# ! pip install trulens_eval==0.19.0 openai==1.3.7


# In[ ]:


import os

os.environ["OPENAI_API_KEY"] = "..."
os.environ["HUGGINGFACE_API_KEY"] = "..."


# ### Import from LangChain and TruLens

# In[ ]:


from IPython.display import JSON

# Imports main tools:
from trulens_eval import TruChain, Feedback, Huggingface, Tru
from trulens_eval import Feedback
from trulens_eval import Huggingface
from trulens_eval import Tru
from trulens_eval import TruChain
from trulens_eval.schema import FeedbackResult

tru = Tru()

# Imports from langchain to build app. You may need to install langchain first
# with the following:
# ! pip install langchain>=0.0.170
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.prompts import ChatPromptTemplate, PromptTemplate
from langchain.prompts import ChatPromptTemplate
from langchain.prompts import HumanMessagePromptTemplate

from langchain.prompts import PromptTemplate

# ### Create Simple LLM Application
#
#
# This example uses a LangChain framework and OpenAI LLM

# In[ ]:


full_prompt = HumanMessagePromptTemplate(
prompt=PromptTemplate(
template=
Expand All @@ -67,28 +66,22 @@

chain = LLMChain(llm=llm, prompt=chat_prompt_template, verbose=True)


# ### Send your first request

# In[ ]:


prompt_input = '¿que hora es?'


# In[ ]:


llm_response = chain(prompt_input)

display(llm_response)


# ## Initialize Feedback Function(s)

# In[ ]:


# Initialize Huggingface-based feedback function collection class:
hugs = Huggingface()

Expand All @@ -97,67 +90,56 @@
# By default this will check language match on the main app input and main app
# output.


# ## Instrument chain for logging with TruLens

# In[ ]:


tru_recorder = TruChain(chain,
app_id='Chain1_ChatApplication',
feedbacks=[f_lang_match])

tru_recorder = TruChain(
chain, app_id='Chain1_ChatApplication', feedbacks=[f_lang_match]
)

# In[ ]:


with tru_recorder as recording:
llm_response = chain(prompt_input)

display(llm_response)


# ## Retrieve records and feedback

# In[ ]:


# The record of the ap invocation can be retrieved from the `recording`:

rec = recording.get() # use .get if only one record
rec = recording.get() # use .get if only one record
# recs = recording.records # use .records if multiple

display(rec)


# In[ ]:


# The results of the feedback functions can be rertireved from the record. These
# are `Future` instances (see `concurrent.futures`). You can use `as_completed`
# to wait until they have finished evaluating.

from concurrent.futures import as_completed

for feedback_future in as_completed(rec.feedback_results):
for feedback_future in as_completed(rec.feedback_results):
feedback, feedback_result = feedback_future.result()

feedback: Feedback
feedbac_result: FeedbackResult

display(feedback.name, feedback_result.result)


# ## Explore in a Dashboard

# In[ ]:


tru.run_dashboard() # open a local streamlit app to explore
tru.run_dashboard() # open a local streamlit app to explore

# tru.stop_dashboard() # stop if needed


# Alternatively, you can run `trulens-eval` from a command line in the same folder to start the dashboard.

# Note: Feedback functions evaluated in the deferred manner can be seen in the "Progress" page of the TruLens dashboard.
Expand All @@ -166,6 +148,5 @@

# In[ ]:


tru.get_records_and_feedback(app_ids=[])[0] # pass an empty list of app_ids to get all

tru.get_records_and_feedback(app_ids=[]
)[0] # pass an empty list of app_ids to get all
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,70 @@
# coding: utf-8

# # Llama-Index Quickstart
#
#
# In this quickstart you will create a simple Llama Index App and learn how to log it and get feedback on an LLM response.
#
#
# For evaluation, we will leverage the "hallucination triad" of groundedness, context relevance and answer relevance.
#
#
# [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/main/trulens_eval/examples/quickstart/llama_index_quickstart.ipynb)

# ## Setup
#
#
# ### Install dependencies
# Let's install some of the dependencies for this notebook if we don't have them already

# In[ ]:


# pip install trulens_eval==0.19.0 llama_index>=0.9.15post2 html2text>=2020.1.16

# pip install trulens_eval==0.19.0 llama_index>=0.9.15post2 html2text>=2020.1.16

# ### Add API keys
# For this quickstart, you will need Open AI and Huggingface keys. The OpenAI key is used for embeddings and GPT, and the Huggingface key is used for evaluation.

# In[ ]:


import os
os.environ["OPENAI_API_KEY"] = "..."

os.environ["OPENAI_API_KEY"] = "..."

# ### Import from LlamaIndex and TruLens

# In[ ]:


from trulens_eval import Feedback, Tru, TruLlama
from trulens_eval import Feedback
from trulens_eval import Tru
from trulens_eval import TruLlama
from trulens_eval.feedback import Groundedness
from trulens_eval.feedback.provider.openai import OpenAI

tru = Tru()


# ### Create Simple LLM Application
#
#
# This example uses LlamaIndex which internally uses an OpenAI LLM.

# In[ ]:


from llama_index import VectorStoreIndex
from llama_index.readers.web import SimpleWebPageReader

documents = SimpleWebPageReader(
html_to_text=True
).load_data(["http://paulgraham.com/worked.html"])
documents = SimpleWebPageReader(html_to_text=True).load_data(
["http://paulgraham.com/worked.html"]
)
index = VectorStoreIndex.from_documents(documents)

query_engine = index.as_query_engine()


# ### Send your first request

# In[ ]:


response = query_engine.query("What did the author do growing up?")
print(response)


# ## Initialize Feedback Function(s)

# In[ ]:


import numpy as np

# Initialize provider class
Expand All @@ -84,46 +76,40 @@
# Define a groundedness feedback function
f_groundedness = Feedback(grounded.groundedness_measure_with_cot_reasons).on(
TruLlama.select_source_nodes().node.text.collect()
).on_output(
).aggregate(grounded.grounded_statements_aggregator)
).on_output().aggregate(grounded.grounded_statements_aggregator)

# Question/answer relevance between overall question and answer.
f_qa_relevance = Feedback(openai.relevance).on_input_output()

# Question/statement relevance between question and each context chunk.
f_qs_relevance = Feedback(openai.qs_relevance).on_input().on(
TruLlama.select_source_nodes().node.text
).aggregate(np.mean)

).aggregate(np.mean)

# ## Instrument app for logging with TruLens

# In[ ]:


tru_query_engine_recorder = TruLlama(query_engine,
tru_query_engine_recorder = TruLlama(
query_engine,
app_id='LlamaIndex_App1',
feedbacks=[f_groundedness, f_qa_relevance, f_qs_relevance])

feedbacks=[f_groundedness, f_qa_relevance, f_qs_relevance]
)

# In[ ]:


# or as context manager
with tru_query_engine_recorder as recording:
query_engine.query("What did the author do growing up?")


# ## Explore in a Dashboard

# In[ ]:


tru.run_dashboard() # open a local streamlit app to explore
tru.run_dashboard() # open a local streamlit app to explore

# tru.stop_dashboard() # stop if needed


# Alternatively, you can run `trulens-eval` from a command line in the same folder to start the dashboard.

# Note: Feedback functions evaluated in the deferred manner can be seen in the "Progress" page of the TruLens dashboard.
Expand All @@ -132,6 +118,5 @@

# In[ ]:


tru.get_records_and_feedback(app_ids=[])[0] # pass an empty list of app_ids to get all

tru.get_records_and_feedback(app_ids=[]
)[0] # pass an empty list of app_ids to get all
Loading

0 comments on commit 6630bd0

Please sign in to comment.