Langchain Vertex Ai Tracing Tutorial
Tracing a LangChain and VertexAI Application
LLM orchestration frameworks such as LangChain provide abstractions that enable users to build powerful applications in a few lines of code. However, the same abstractions can make it difficult to understand what is going on under the hood and to pinpoint the cause of issues.
Phoenix makes your LLM applications observable by visualizing the underlying structure of each call to your chain and surfacing problematic "spans" of execution based on latency, token count, or other evaluation metrics.
In this tutorial, you will:
- Build a simple retrieval-augmented generation application over the Arize documentation using LangChain and VertexAI, in particular, using "textembedding-gecko" for embeddings and "chat-bison" for chat,
- Record trace data in OpenInference format,
- Inspect the traces and spans of your application to identify uncaught exceptions and sources of latency and cost.
ℹ️️ This notebook requires access to the Vertex AI API.
1. Install Dependencies and Import Libraries
Downgrade Colab's pre-installed version of shapely for compatibility reasons.
⚠️ If you run into a version compatibility error in a later cell, try restarting the runtime and re-running the notebook.
Install Phoenix, LangChain, and the Google Cloud AI Platform SDK.
Import libraries.
2. Set Configuration and Authenticate with Vertex AI
If you are running this notebook in Colab, sign in with your Gmail credentials. If running locally, you'll need to ensure that your gcloud is correctly configured to run Vertex AI.
Enter your project ID and location:
project_id: The default project to use when making API calls.location: The default location to use when making API calls. If not set defaults to us-central-1.
3. Launch Phoenix
You can run Phoenix in the background to collect trace data emitted by any LlamaIndex application that has been instrumented with the OpenInferenceTraceCallbackHandler.
Launch Phoenix and follow the instructions in the cell output to open the Phoenix UI (the UI should be empty because we have yet to run our LangChain application).
4. Instantiate LangChain Instrumentor
5. Build Your LLM Application
Define an LCEL retrieval chain leveraging "textembedding-gecko" and "chat-bison" from the VertexAI API. The knowledge base of this chain is built over the Arize documentation.
6. Run the Chain
Download a small dataset of user queries to ask your application.
Run your chain against ten queries. Notice that the tracer is attached in the run call.
Check out your traces in Phoenix!
7. Export and Evaluate Your Trace Data
You can export your trace data as a pandas dataframe for further analysis and evaluation.
In this case, we will export our retriever spans into two separate dataframes:
queries_df, in which the retrieved documents for each query are concatenated into a single column,retrieved_documents_df, in which each retrieved document is "exploded" into its own row to enable the evaluation of each query-document pair in isolation.
This will enable us to compute multiple kinds of evaluations, including:
- relevance: Are the retrieved documents grounded in the response?
- Q&A correctness: Are your application's responses grounded in the retrieved context?
- hallucinations: Is your application making up false information?
Next, define your evaluation model and your evaluators.
Evaluators are built on top of language models and prompt the LLM to assess the quality of responses, the relevance of retrieved documents, etc., and provide a quality signal even in the absence of human-labeled data. Pick an evaluator type and instantiate it with the language model you want to use to perform evaluations using our battle-tested evaluation templates.
Your evaluations should now appear as annotations on the appropriate spans in Phoenix.
8. Final Thoughts
LLM Traces and the accompanying OpenInference Tracing specification is designed to be a category of telemetry data that is used to understand the execution of LLMs and the surrounding application context such as retrieval from vector stores and the usage of external tools such as search engines or APIs. It lets you understand the inner workings of the individual steps your application takes wile also giving you visibility into how your system is running and performing as a whole.
LLM Evals are designed for simple, fast, and accurate LLM-based evaluations. They let you quickly benchmark the performance of your LLM application and help you identify the problematic spans of execution.
For more details on Phoenix, LLM Tracing, and LLM Evals, checkout our documentation.