diff --git a/README.md b/README.md index 425a26af3..7126c2254 100644 --- a/README.md +++ b/README.md @@ -43,24 +43,24 @@ TruLens supports the evaluation of tracking for any LLM app framework. Choose a **Langchain** -[langchain_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/langchain_quickstart_colab.ipynb) +[langchain_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/langchain_quickstart_colab.ipynb) -[langchain_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/quickstart.py). +[langchain_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/quickstart.py). **Llama-Index** -[llama_index_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb) +[llama_index_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb) -[llama_index_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/llama_index_quickstart.py) +[llama_index_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/llama_index_quickstart.py) **No Framework** -[no_framework_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/no_framework_quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/no_framework_quickstart_colab.ipynb) +[no_framework_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/no_framework_quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/no_framework_quickstart_colab.ipynb) -[no_framework_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/no_framework_quickstart.py) +[no_framework_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/no_framework_quickstart.py) ## TruLens-Explain diff --git a/docs/trulens_eval/gh_top_intro.md b/docs/trulens_eval/gh_top_intro.md index 2bbf38022..7b2c9a205 100644 --- a/docs/trulens_eval/gh_top_intro.md +++ b/docs/trulens_eval/gh_top_intro.md @@ -43,21 +43,21 @@ TruLens supports the evaluation of tracking for any LLM app framework. Choose a **Langchain** -[langchain_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/langchain_quickstart_colab.ipynb) +[langchain_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/langchain_quickstart_colab.ipynb) -[langchain_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/quickstart.py). +[langchain_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/quickstart.py). **Llama-Index** -[llama_index_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb) +[llama_index_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb) -[llama_index_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/llama_index_quickstart.py) +[llama_index_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/llama_index_quickstart.py) **No Framework** -[no_framework_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/no_framework_quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/no_framework_quickstart_colab.ipynb) +[no_framework_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/no_framework_quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/no_framework_quickstart_colab.ipynb) -[no_framework_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/no_framework_quickstart.py) +[no_framework_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/no_framework_quickstart.py) diff --git a/docs/trulens_eval/intro.md b/docs/trulens_eval/intro.md index 08eb32355..70e516f81 100644 --- a/docs/trulens_eval/intro.md +++ b/docs/trulens_eval/intro.md @@ -46,24 +46,24 @@ TruLens supports the evaluation of tracking for any LLM app framework. Choose a **Langchain** -[langchain_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/langchain_quickstart_colab.ipynb) +[langchain_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/langchain_quickstart_colab.ipynb) -[langchain_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/quickstart.py). +[langchain_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/quickstart.py). **Llama-Index** -[llama_index_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb) +[llama_index_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb) -[llama_index_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/llama_index_quickstart.py) +[llama_index_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/llama_index_quickstart.py) **No Framework** -[no_framework_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/no_framework_quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/no_framework_quickstart_colab.ipynb) +[no_framework_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/no_framework_quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/no_framework_quickstart_colab.ipynb) -[no_framework_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/no_framework_quickstart.py) +[no_framework_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/no_framework_quickstart.py) ### 💡 Contributing diff --git a/trulens_eval/README.md b/trulens_eval/README.md index 08eb32355..70e516f81 100644 --- a/trulens_eval/README.md +++ b/trulens_eval/README.md @@ -46,24 +46,24 @@ TruLens supports the evaluation of tracking for any LLM app framework. Choose a **Langchain** -[langchain_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/langchain_quickstart_colab.ipynb) +[langchain_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/langchain_quickstart_colab.ipynb) -[langchain_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/quickstart.py). +[langchain_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/quickstart.py). **Llama-Index** -[llama_index_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb) +[llama_index_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb) -[llama_index_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/llama_index_quickstart.py) +[llama_index_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/llama_index_quickstart.py) **No Framework** -[no_framework_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/no_framework_quickstart.ipynb). -[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/colab/quickstarts/no_framework_quickstart_colab.ipynb) +[no_framework_quickstart.ipynb](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/no_framework_quickstart.ipynb). +[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/colab/quickstarts/no_framework_quickstart_colab.ipynb) -[no_framework_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.11.0/trulens_eval/examples/no_framework_quickstart.py) +[no_framework_quickstart.py](https://github.com/truera/trulens/blob/releases/rc-trulens-eval-0.12.0/trulens_eval/examples/no_framework_quickstart.py) ### 💡 Contributing diff --git a/trulens_eval/examples/all_tools.py b/trulens_eval/examples/all_tools.py index a17eaef12..d917a56f7 100644 --- a/trulens_eval/examples/all_tools.py +++ b/trulens_eval/examples/all_tools.py @@ -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/langchain_quickstart.ipynb) # ## Setup @@ -12,18 +12,13 @@ # For this quickstart you will need Open AI and Huggingface keys import os - os.environ["OPENAI_API_KEY"] = "..." os.environ["HUGGINGFACE_API_KEY"] = "..." # ### Import from LangChain and TruLens # Imports main tools: -from trulens_eval import Feedback -from trulens_eval import Huggingface -from trulens_eval import Tru -from trulens_eval import TruChain - +from trulens_eval import TruChain, Feedback, Huggingface, Tru tru = Tru() # Imports from langchain to build app. You may need to install langchain first @@ -31,12 +26,11 @@ # ! pip install langchain>=0.0.170 from langchain.chains import LLMChain from langchain.llms import OpenAI -from langchain.prompts.chat import ChatPromptTemplate +from langchain.prompts.chat import ChatPromptTemplate, PromptTemplate from langchain.prompts.chat import HumanMessagePromptTemplate -from langchain.prompts.chat import PromptTemplate # ### Create Simple LLM Application -# +# # This example uses a LangChain framework and OpenAI LLM full_prompt = HumanMessagePromptTemplate( @@ -73,12 +67,10 @@ # ## Instrument chain for logging with TruLens -tru_chain = TruChain( - chain, +tru_chain = TruChain(chain, app_id='Chain1_ChatApplication', feedbacks=[f_lang_match], - tags="prototype" -) + tags = "prototype") # Instrumented chain can operate like the original: llm_response = tru_chain(prompt_input) @@ -91,54 +83,57 @@ # ## Explore in a Dashboard -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. # ### Chain Leaderboard -# +# # Understand how your LLM application is performing at a glance. Once you've set up logging and evaluation in your application, you can view key performance statistics including cost and average feedback value across all of your LLM apps using the chain leaderboard. As you iterate new versions of your LLM application, you can compare their performance across all of the different quality metrics you've set up. -# +# # Note: Average feedback values are returned and printed in a range from 0 (worst) to 1 (best). -# +# # ![Chain Leaderboard](https://www.trulens.org/Assets/image/Leaderboard.png) -# +# # To dive deeper on a particular chain, click "Select Chain". -# +# # ### Understand chain performance with Evaluations -# +# # To learn more about the performance of a particular chain or LLM model, we can select it to view its evaluations at the record level. LLM quality is assessed through the use of feedback functions. Feedback functions are extensible methods for determining the quality of LLM responses and can be applied to any downstream LLM task. Out of the box we provide a number of feedback functions for assessing model agreement, sentiment, relevance and more. -# +# # The evaluations tab provides record-level metadata and feedback on the quality of your LLM application. -# +# # ![Evaluations](https://www.trulens.org/Assets/image/Leaderboard.png) -# +# # ### Deep dive into full chain metadata -# +# # Click on a record to dive deep into all of the details of your chain stack and underlying LLM, captured by tru_chain. -# +# # ![Explore a Chain](https://www.trulens.org/Assets/image/Chain_Explore.png) -# +# # If you prefer the raw format, you can quickly get it using the "Display full chain json" or "Display full record json" buttons at the bottom of the page. # Note: Feedback functions evaluated in the deferred manner can be seen in the "Progress" page of the TruLens dashboard. # ## Or view results directly in your notebook -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 # # Logging Methods -# +# # ## Automatic Logging -# +# # The simplest method for logging with TruLens is by wrapping with TruChain and including the tru argument, as shown in the quickstart. -# +# # This is done like so: -truchain = TruChain(chain, app_id='Chain1_ChatApplication', tru=tru) +truchain = TruChain( + chain, + app_id='Chain1_ChatApplication', + tru=tru +) truchain("This will be automatically logged.") # Feedback functions can also be logged automatically by providing them in a list to the feedbacks arg. @@ -146,21 +141,21 @@ truchain = TruChain( chain, app_id='Chain1_ChatApplication', - feedbacks=[f_lang_match], # feedback functions + feedbacks=[f_lang_match], # feedback functions tru=tru ) truchain("This will be automatically logged.") # ## Manual Logging -# +# # ### Wrap with TruChain to instrument your chain tc = TruChain(chain, app_id='Chain1_ChatApplication') # ### Set up logging and instrumentation -# +# # Making the first call to your wrapped LLM Application will now also produce a log or "record" of the chain execution. -# +# prompt_input = 'que hora es?' gpt3_response, record = tc.call_with_record(prompt_input) @@ -177,21 +172,22 @@ # Capturing app feedback such as user feedback of the responses can be added with one call. thumb_result = True -tru.add_feedback( - name="👍 (1) or 👎 (0)", record_id=record.record_id, result=thumb_result -) +tru.add_feedback(name="👍 (1) or 👎 (0)", + record_id=record.record_id, + result=thumb_result) # ### Evaluate Quality -# +# # Following the request to your app, you can then evaluate LLM quality using feedback functions. This is completed in a sequential call to minimize latency for your application, and evaluations will also be logged to your local machine. -# +# # To get feedback on the quality of your LLM, you can use any of the provided feedback functions or add your own. -# +# # To assess your LLM quality, you can provide the feedback functions to `tru.run_feedback()` in a list provided to `feedback_functions`. -# +# feedback_results = tru.run_feedback_functions( - record=record, feedback_functions=[f_lang_match] + record=record, + feedback_functions=[f_lang_match] ) print(feedback_results) @@ -200,9 +196,9 @@ tru.add_feedbacks(feedback_results) # ### Out-of-band Feedback evaluation -# +# # In the above example, the feedback function evaluation is done in the same process as the chain evaluation. The alternative approach is the use the provided persistent evaluator started via `tru.start_deferred_feedback_evaluator`. Then specify the `feedback_mode` for `TruChain` as `deferred` to let the evaluator handle the feedback functions. -# +# # For demonstration purposes, we start the evaluator here but it can be started in another process. truchain: TruChain = TruChain( @@ -218,22 +214,17 @@ tru.stop_evaluator() # # Custom Functions -# +# # Feedback functions are an extensible framework for evaluating LLMs. You can add your own feedback functions to evaluate the qualities required by your application by updating `trulens_eval/feedback.py`, or simply creating a new provider class and feedback function in youre notebook. If your contributions would be useful for others, we encourage you to contribute to TruLens! -# +# # Feedback functions are organized by model provider into Provider classes. -# +# # The process for adding new feedback functions is: # 1. Create a new Provider class or locate an existing one that applies to your feedback function. If your feedback function does not rely on a model provider, you can create a standalone class. Add the new feedback function method to your selected class. Your new method can either take a single text (str) as a parameter or both prompt (str) and response (str). It should return a float between 0 (worst) and 1 (best). -from trulens_eval import Feedback -from trulens_eval import Provider -from trulens_eval import Select -from trulens_eval import Tru - +from trulens_eval import Provider, Feedback, Select, Tru class StandAlone(Provider): - def custom_feedback(self, my_text_field: str) -> float: """ A dummy function of text inputs to float outputs. @@ -246,67 +237,57 @@ def custom_feedback(self, my_text_field: str) -> float: """ return 1.0 / (1.0 + len(my_text_field) * len(my_text_field)) - # 2. Instantiate your provider and feedback functions. The feedback function is wrapped by the trulens-eval Feedback class which helps specify what will get sent to your function parameters (For example: Select.RecordInput or Select.RecordOutput) standalone = StandAlone() -f_custom_function = Feedback(standalone.custom_feedback - ).on(my_text_field=Select.RecordOutput) +f_custom_function = Feedback(standalone.custom_feedback).on( + my_text_field=Select.RecordOutput +) # 3. Your feedback function is now ready to use just like the out of the box feedback functions. Below is an example of it being used. tru = Tru() feedback_results = tru.run_feedback_functions( - record=record, feedback_functions=[f_custom_function] + record=record, + feedback_functions=[f_custom_function] ) tru.add_feedbacks(feedback_results) # ## Multi-Output Feedback functions # Trulens also supports multi-output feedback functions. As a typical feedback function will output a float between 0 and 1, multi-output should output a dictionary of `output_key` to a float between 0 and 1. The feedbacks table will print the feedback with column `feedback_name:::outputkey` -multi_output_feedback = Feedback( - lambda input_param: { - 'output_key1': 0.1, - 'output_key2': 0.9 - }, name="multi" -).on(input_param=Select.RecordOutput) +multi_output_feedback = Feedback(lambda input_param: {'output_key1': 0.1, 'output_key2': 0.9}, name="multi").on( + input_param=Select.RecordOutput +) feedback_results = tru.run_feedback_functions( - record=record, feedback_functions=[multi_output_feedback] + record=record, + feedback_functions=[multi_output_feedback] ) tru.add_feedbacks(feedback_results) # Aggregators will run on the same dict keys. import numpy as np - -multi_output_feedback = Feedback( - lambda input_param: { - 'output_key1': 0.1, - 'output_key2': 0.9 - }, - name="multi-agg" -).on(input_param=Select.RecordOutput).aggregate(np.mean) +multi_output_feedback = Feedback(lambda input_param: {'output_key1': 0.1, 'output_key2': 0.9}, name="multi-agg").on( + input_param=Select.RecordOutput +).aggregate(np.mean) feedback_results = tru.run_feedback_functions( - record=record, feedback_functions=[multi_output_feedback] + record=record, + feedback_functions=[multi_output_feedback] ) tru.add_feedbacks(feedback_results) - # For multi-context chunking, an aggregator can operate on a list of multi output dictionaries. def dict_aggregator(list_dict_input): agg = 0 for dict_input in list_dict_input: agg += dict_input['output_key1'] return agg - - -multi_output_feedback = Feedback( - lambda input_param: { - 'output_key1': 0.1, - 'output_key2': 0.9 - }, - name="multi-agg-dict" -).on(input_param=Select.RecordOutput).aggregate(dict_aggregator) +multi_output_feedback = Feedback(lambda input_param: {'output_key1': 0.1, 'output_key2': 0.9}, name="multi-agg-dict").on( + input_param=Select.RecordOutput +).aggregate(dict_aggregator) feedback_results = tru.run_feedback_functions( - record=record, feedback_functions=[multi_output_feedback] + record=record, + feedback_functions=[multi_output_feedback] ) tru.add_feedbacks(feedback_results) + diff --git a/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb b/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb index f212bd9a2..220f098a8 100644 --- a/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb +++ b/trulens_eval/examples/colab/quickstarts/llama_index_quickstart_colab.ipynb @@ -42,7 +42,7 @@ "metadata": {}, "outputs": [], "source": [ - "#! pip install trulens-eval==0.11.0 llama_index==0.8.5.post2 html2text==2020.1.16" + "#! pip install trulens-eval==0.12.0 llama_index==0.8.5.post2 html2text==2020.1.16" ] }, { diff --git a/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb b/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb index 977f76275..5e20ba428 100644 --- a/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb +++ b/trulens_eval/examples/frameworks/llama_index/llama_index_quickstart.ipynb @@ -29,7 +29,7 @@ "metadata": {}, "outputs": [], "source": [ - "#! pip install trulens-eval==0.11.0 llama_index==0.8.5.post2 html2text==2020.1.16" + "#! pip install trulens-eval==0.12.0 llama_index==0.8.5.post2 html2text==2020.1.16" ] }, { diff --git a/trulens_eval/examples/langchain_quickstart.py b/trulens_eval/examples/langchain_quickstart.py index 05945afbe..2b7d3c897 100644 --- a/trulens_eval/examples/langchain_quickstart.py +++ b/trulens_eval/examples/langchain_quickstart.py @@ -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/langchain_quickstart.ipynb) # ## Setup @@ -12,18 +12,13 @@ # For this quickstart you will need Open AI and Huggingface keys import os - os.environ["OPENAI_API_KEY"] = "..." os.environ["HUGGINGFACE_API_KEY"] = "..." # ### Import from LangChain and TruLens # Imports main tools: -from trulens_eval import Feedback -from trulens_eval import Huggingface -from trulens_eval import Tru -from trulens_eval import TruChain - +from trulens_eval import TruChain, Feedback, Huggingface, Tru tru = Tru() # Imports from langchain to build app. You may need to install langchain first @@ -31,12 +26,11 @@ # ! pip install langchain>=0.0.170 from langchain.chains import LLMChain from langchain.llms import OpenAI -from langchain.prompts.chat import ChatPromptTemplate +from langchain.prompts.chat import ChatPromptTemplate, PromptTemplate from langchain.prompts.chat import HumanMessagePromptTemplate -from langchain.prompts.chat import PromptTemplate # ### Create Simple LLM Application -# +# # This example uses a LangChain framework and OpenAI LLM full_prompt = HumanMessagePromptTemplate( @@ -73,12 +67,10 @@ # ## Instrument chain for logging with TruLens -tru_chain = TruChain( - chain, +tru_chain = TruChain(chain, app_id='Chain1_ChatApplication', feedbacks=[f_lang_match], - tags="prototype" -) + tags = "prototype") # Instrumented chain can operate like the original: llm_response = tru_chain(prompt_input) @@ -91,41 +83,41 @@ # ## Explore in a Dashboard -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. # ### Chain Leaderboard -# +# # Understand how your LLM application is performing at a glance. Once you've set up logging and evaluation in your application, you can view key performance statistics including cost and average feedback value across all of your LLM apps using the chain leaderboard. As you iterate new versions of your LLM application, you can compare their performance across all of the different quality metrics you've set up. -# +# # Note: Average feedback values are returned and printed in a range from 0 (worst) to 1 (best). -# +# # ![Chain Leaderboard](https://www.trulens.org/Assets/image/Leaderboard.png) -# +# # To dive deeper on a particular chain, click "Select Chain". -# +# # ### Understand chain performance with Evaluations -# +# # To learn more about the performance of a particular chain or LLM model, we can select it to view its evaluations at the record level. LLM quality is assessed through the use of feedback functions. Feedback functions are extensible methods for determining the quality of LLM responses and can be applied to any downstream LLM task. Out of the box we provide a number of feedback functions for assessing model agreement, sentiment, relevance and more. -# +# # The evaluations tab provides record-level metadata and feedback on the quality of your LLM application. -# +# # ![Evaluations](https://www.trulens.org/Assets/image/Leaderboard.png) -# +# # ### Deep dive into full chain metadata -# +# # Click on a record to dive deep into all of the details of your chain stack and underlying LLM, captured by tru_chain. -# +# # ![Explore a Chain](https://www.trulens.org/Assets/image/Chain_Explore.png) -# +# # If you prefer the raw format, you can quickly get it using the "Display full chain json" or "Display full record json" buttons at the bottom of the page. # Note: Feedback functions evaluated in the deferred manner can be seen in the "Progress" page of the TruLens dashboard. # ## Or view results directly in your notebook -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 + diff --git a/trulens_eval/examples/llama_index_quickstart.py b/trulens_eval/examples/llama_index_quickstart.py index 11eb56f4c..6c3948b67 100644 --- a/trulens_eval/examples/llama_index_quickstart.py +++ b/trulens_eval/examples/llama_index_quickstart.py @@ -2,46 +2,41 @@ # 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. -# +# # [![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/frameworks/llama_index/llama_index_quickstart.ipynb) # ## Setup -# +# # ### Install dependencies # Let's install some of the dependencies for this notebook if we don't have them already -#! pip install trulens-eval==0.11.0 llama_index==0.8.5.post2 html2text==2020.1.16 +#! pip install trulens-eval==0.12.0 llama_index==0.8.5.post2 html2text==2020.1.16 # ### Add API keys # For this quickstart, you will need Open AI and Huggingface keys import os - os.environ["OPENAI_API_KEY"] = "..." os.environ["HUGGINGFACE_API_KEY"] = "..." # ### Import from LlamaIndex and TruLens # Imports main tools: -from trulens_eval import Feedback -from trulens_eval import feedback -from trulens_eval import Tru -from trulens_eval import TruLlama - +from trulens_eval import TruLlama, Feedback, Tru, feedback tru = Tru() # ### Create Simple LLM Application -# +# # This example uses LlamaIndex which internally uses an OpenAI LLM. -from llama_index import SimpleWebPageReader -from llama_index import VectorStoreIndex +from llama_index import VectorStoreIndex, SimpleWebPageReader # llama_index 0.8.15 bug: need to provide metadata_fn documents = SimpleWebPageReader( - html_to_text=True, metadata_fn=lambda url: dict(url=url) + html_to_text=True, + metadata_fn=lambda url: dict(url=url) ).load_data(["http://paulgraham.com/worked.html"]) index = VectorStoreIndex.from_documents(documents) @@ -75,11 +70,9 @@ # ## Instrument chain for logging with TruLens -tru_query_engine = TruLlama( - query_engine, +tru_query_engine = TruLlama(query_engine, app_id='LlamaIndex_App1', - feedbacks=[f_lang_match, f_qa_relevance, f_qs_relevance] -) + feedbacks=[f_lang_match, f_qa_relevance, f_qs_relevance]) # Instrumented query engine can operate like the original: llm_response = tru_query_engine.query("What did the author do growing up?") @@ -91,41 +84,41 @@ # ## Explore in a Dashboard -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. # ### Leaderboard -# +# # Understand how your LLM application is performing at a glance. Once you've set up logging and evaluation in your application, you can view key performance statistics including cost and average feedback value across all of your LLM apps using the chain leaderboard. As you iterate new versions of your LLM application, you can compare their performance across all of the different quality metrics you've set up. -# +# # Note: Average feedback values are returned and printed in a range from 0 (worst) to 1 (best). -# +# # ![Chain Leaderboard](https://www.trulens.org/Assets/image/Leaderboard.png) -# +# # To dive deeper on a particular chain, click "Select Chain". -# +# # ### Understand chain performance with Evaluations -# +# # To learn more about the performance of a particular chain or LLM model, we can select it to view its evaluations at the record level. LLM quality is assessed through the use of feedback functions. Feedback functions are extensible methods for determining the quality of LLM responses and can be applied to any downstream LLM task. Out of the box we provide a number of feedback functions for assessing model agreement, sentiment, relevance and more. -# +# # The evaluations tab provides record-level metadata and feedback on the quality of your LLM application. -# +# # ![Evaluations](https://www.trulens.org/Assets/image/Leaderboard.png) -# +# # ### Deep dive into full chain metadata -# +# # Click on a record to dive deep into all of the details of your chain stack and underlying LLM, captured by tru_chain. -# +# # ![Explore a Chain](https://www.trulens.org/Assets/image/Chain_Explore.png) -# +# # If you prefer the raw format, you can quickly get it using the "Display full chain json" or "Display full record json" buttons at the bottom of the page. # Note: Feedback functions evaluated in the deferred manner can be seen in the "Progress" page of the TruLens dashboard. # ## Or view results directly in your notebook -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 + diff --git a/trulens_eval/examples/models/azure_openai_llama_index.ipynb b/trulens_eval/examples/models/azure_openai_llama_index.ipynb index 7385d55b6..0b8a05f67 100644 --- a/trulens_eval/examples/models/azure_openai_llama_index.ipynb +++ b/trulens_eval/examples/models/azure_openai_llama_index.ipynb @@ -29,7 +29,7 @@ "metadata": {}, "outputs": [], "source": [ - "#! pip install trulens-eval==0.11.0 llama_index==0.8.21 langchain==0.0.268 html2text==2020.1.16" + "#! pip install trulens-eval==0.12.0 llama_index==0.8.21 langchain==0.0.268 html2text==2020.1.16" ] }, { diff --git a/trulens_eval/examples/no_framework_quickstart.py b/trulens_eval/examples/no_framework_quickstart.py index 9c3a21bfe..e2bfdf0b9 100644 --- a/trulens_eval/examples/no_framework_quickstart.py +++ b/trulens_eval/examples/no_framework_quickstart.py @@ -2,9 +2,9 @@ # coding: utf-8 # # No Framework Quickstart -# +# # In this quickstart you will create a simple text to text application and learn how to log it and get feedback. -# +# # [![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/no_framework_quickstart.ipynb) # ## Setup @@ -12,56 +12,39 @@ # For this quickstart you will need Open AI and Huggingface keys import os - os.environ["OPENAI_API_KEY"] = "..." os.environ["HUGGINGFACE_API_KEY"] = "..." import openai - openai.api_key = os.environ["OPENAI_API_KEY"] # ### Import from TruLens # Imports main tools: -from trulens_eval import Feedback -from trulens_eval import Huggingface -from trulens_eval import Tru - +from trulens_eval import Feedback, Huggingface, Tru tru = Tru() # ### 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 openai.ChatCompletion.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 - } + 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"] - import hashlib - - def simple_hash_callable(prompt): h = hashlib.shake_256(prompt.encode('utf-8')) return str(h.hexdigest(20)) - # ### Send your first request -prompt_input = "How good is language AI?" +prompt_input="How good is language AI?" prompt_output = llm_standalone(prompt_input) prompt_output @@ -78,13 +61,8 @@ def simple_hash_callable(prompt): # ## Instrument the callable for logging with TruLens from trulens_eval import TruBasicApp - -tru_llm_standalone = TruBasicApp( - llm_standalone, app_id="Happy Bot", feedbacks=[f_sentiment] -) -tru_simple_hash_callable = TruBasicApp( - simple_hash_callable, app_id="Hasher", feedbacks=[f_sentiment] -) +tru_llm_standalone = TruBasicApp(llm_standalone, app_id="Happy Bot", feedbacks=[f_sentiment]) +tru_simple_hash_callable = TruBasicApp(simple_hash_callable, app_id="Hasher", feedbacks=[f_sentiment]) with tru_llm_standalone as recording: llm_standalone(prompt_input) @@ -94,7 +72,7 @@ def simple_hash_callable(prompt): # ## Explore in a Dashboard -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 @@ -102,5 +80,5 @@ def simple_hash_callable(prompt): # ## Or view results directly in your notebook -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 + diff --git a/trulens_eval/examples/vector-dbs/milvus/milvus_simple.ipynb b/trulens_eval/examples/vector-dbs/milvus/milvus_simple.ipynb index 5d2945360..334e688ae 100644 --- a/trulens_eval/examples/vector-dbs/milvus/milvus_simple.ipynb +++ b/trulens_eval/examples/vector-dbs/milvus/milvus_simple.ipynb @@ -34,7 +34,7 @@ "metadata": {}, "outputs": [], "source": [ - "#! pip install trulens-eval==0.11.0 llama_index==0.8.4 pymilvus==2.3.0 nltk==3.8.1 html2text==2020.1.16" + "#! pip install trulens-eval==0.12.0 llama_index==0.8.4 pymilvus==2.3.0 nltk==3.8.1 html2text==2020.1.16" ] }, { diff --git a/trulens_eval/examples/vector-dbs/pinecone/pinecone_simple.ipynb b/trulens_eval/examples/vector-dbs/pinecone/pinecone_simple.ipynb index 229d07e67..1119af213 100644 --- a/trulens_eval/examples/vector-dbs/pinecone/pinecone_simple.ipynb +++ b/trulens_eval/examples/vector-dbs/pinecone/pinecone_simple.ipynb @@ -29,7 +29,7 @@ "metadata": {}, "outputs": [], "source": [ - "#! pip install trulens-eval==0.11.0 llama_index==0.8.4 pinecone-client==2.2.2 nltk==3.8.1 html2text==2020.1.16" + "#! pip install trulens-eval==0.12.0 llama_index==0.8.4 pinecone-client==2.2.2 nltk==3.8.1 html2text==2020.1.16" ] }, { diff --git a/trulens_eval/trulens_eval/__init__.py b/trulens_eval/trulens_eval/__init__.py index bf68e4320..6f6f48a9e 100644 --- a/trulens_eval/trulens_eval/__init__.py +++ b/trulens_eval/trulens_eval/__init__.py @@ -78,7 +78,7 @@ """ -__version__ = "0.11.0" +__version__ = "0.12.0" from trulens_eval.feedback import Feedback from trulens_eval.feedback import Huggingface