docs/docs/genai/flavors/langchain/notebooks/langchain-quickstart.ipynb
Welcome to this interactive tutorial designed to introduce you to LangChain and its integration with MLflow. This tutorial is structured as a notebook to provide a hands-on, practical learning experience with the simplest and most core features of LangChain.
chains in LangChain, which are sequences of actions or operations orchestrated to perform complex tasks.LangChain is a Python-based framework that simplifies the development of applications using language models. It is designed to enhance context-awareness and reasoning in applications, allowing for more sophisticated and interactive functionalities.
chain refers to a series of interconnected components or steps designed to accomplish a specific task.In this tutorial, you will:
By the end of this tutorial, you will have a solid foundation in using LangChain with MLflow and an understanding of how to construct and manage chains for practical applications.
Let's dive in and explore the world of LangChain and MLflow!
In order to get started with this tutorial, we're going to need a few things first.
With rapidly changing libraries such as langchain, examples can become outdated rather quickly and will no longer work. For the purposes of demonstration, here are the critical dependencies that are recommended to use to effectively run this notebook:
| Package | Version |
|---|---|
| langchain | 0.1.16 |
| lanchain-community | 0.0.33 |
| langchain-openai | 0.0.8 |
| openai | 1.12.0 |
| tiktoken | 0.6.0 |
| mlflow | 2.12.1 |
If you attempt to execute this notebook with different versions, it may function correctly, but it is recommended to use the precise versions above to ensure that your code executes properly.
To install the dependent packages simply run:
pip install openai==1.12.0 tiktoken==0.6.0 langchain==0.1.16 langchain-openai==0.0.33 langchain-community==0.0.33 mlflow==2.12.1
NOTE: This tutorial does not support openai<1 and is not guaranteed to work with versions of langchain<1.16.0
API keys, especially for SaaS Large Language Models (LLMs), are as sensitive as financial information due to their connection to billing.
If you're interested in learning more about an alternative MLflow solution that securely manages your access keys, read about MLflow AI Gateway here.
For secure usage, set API keys as environment variables.
macOS/Linux: Refer to Apple's guide on using environment variables in Terminal for detailed instructions.
Windows: Follow the steps outlined in Microsoft's documentation on environment variables.
import os
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain_openai import OpenAI
import mlflow
assert "OPENAI_API_KEY" in os.environ, "Please set the OPENAI_API_KEY environment variable."
NOTE: If you'd like to use Azure OpenAI with LangChain, you need to install
openai>=1.10.0andlangchain-openai>=0.0.6, as well as to specify the following credentials and parameters:
# NOTE: Only run this cell if you are using Azure interfaces with OpenAI. If you have a direct account with
# OpenAI, ignore this cell.
from langchain_openai import AzureOpenAI, AzureOpenAIEmbeddings
# Set this to `azure`
os.environ["OPENAI_API_TYPE"] = "azure"
# The API version you want to use: set this to `2023-05-15` for the released version.
os.environ["OPENAI_API_VERSION"] = "2023-05-15"
assert "AZURE_OPENAI_ENDPOINT" in os.environ, (
"Please set the AZURE_OPENAI_ENDPOINT environment variable. It is the base URL for your Azure OpenAI resource. You can find this in the Azure portal under your Azure OpenAI resource."
)
assert "OPENAI_API_KEY" in os.environ, (
"Please set the OPENAI_API_KEY environment variable. It is the API key for your Azure OpenAI resource. You can find this in the Azure portal under your Azure OpenAI resource."
)
azure_openai_llm = AzureOpenAI(
deployment_name="<your-deployment-name>",
model_name="gpt-4o-mini",
)
azure_openai_embeddings = AzureOpenAIEmbeddings(
azure_deployment="<your-deployment-name>",
)
In this section of the tutorial, we have configured the OpenAI model with specific parameters suitable for generating language completions. We're using a Completions model, not ChatCompletions, which means each request is independent, and the entire prompt needs to be included every time to generate a response.
Completions Model: This model does not maintain contextual information across requests. It's ideal for tasks where each request is standalone and doesn't depend on past interactions. Offers flexibility for a variety of non-conversational applications.
No Contextual Memory: The lack of memory of previous interactions means the model is best suited for one-off requests or scenarios where continuity of the conversation is not required.
Comparisons with the ChatCompletions Model Type: Tailored for conversational AI, maintaining context across multiple exchanges for a continuous conversation. Suitable for chatbots or applications where dialogue history is crucial.
In this tutorial, we use the Completions model for its simplicity and effectiveness in handling individual, independent requests, aligning with our tutorial's focus on preparation steps before cooking.
llm = OpenAI(temperature=0.1, max_tokens=1000)
In this part of the tutorial, we have crafted a detailed prompt template that simulates the role of a fine dining sous chef. This template is designed to guide the LangChain model in preparing for a dish, focusing exclusively on the mise-en-place process.
Sous Chef Roleplay: The prompt places the language model in the role of a sous chef, emphasizing meticulous preparation.
Task Outline:
Scope Limitation: The template is explicitly designed to stop at the preparation stage, avoiding the actual cooking process. It focuses on setting up everything needed for the chef to begin cooking.
Dynamic Inputs: The template is adaptable to different recipes and customer counts, as indicated by placeholders {recipe} and {customer_count}.
This template instruction is a key component of the tutorial, demonstrating how to leverage LangChain declaring instructive prompts with parametrized features geared toward single-purpose completions-style applications.
template_instruction = (
"Imagine you are a fine dining sous chef. Your task is to meticulously prepare for a dish, focusing on the mise-en-place process."
"Given a recipe, your responsibilities are: "
"1. List the Ingredients: Carefully itemize all ingredients required for the dish, ensuring every element is accounted for. "
"2. Preparation Techniques: Describe the techniques and operations needed for preparing each ingredient. This includes cutting, "
"processing, or any other form of preparation. Focus on the art of mise-en-place, ensuring everything is perfectly set up before cooking begins."
"3. Ingredient Staging: Provide detailed instructions on how to stage and arrange each ingredient. Explain where each item should be placed for "
"efficient access during the cooking process. Consider the timing and sequence of use for each ingredient. "
"4. Cooking Implements Preparation: Enumerate all the cooking tools and implements needed for each phase of the dish's preparation. "
"Detail any specific preparation these tools might need before the actual cooking starts and describe what pots, pans, dishes, and "
"other tools will be needed for the final preparation."
"Remember, your guidance stops at the preparation stage. Do not delve into the actual cooking process of the dish. "
"Your goal is to set the stage flawlessly for the chef to execute the cooking seamlessly."
"The recipe you are given is for: {recipe} for {customer_count} people. "
)
We start by setting up a PromptTemplate in LangChain, tailored to our sous chef scenario. The template is designed to dynamically accept inputs like the recipe name and customer count. Then, we initialize an LLMChain by combining our OpenAI language model with the prompt template, creating a chain that can simulate the sous chef's preparation process.
With the chain ready, we proceed to log it in MLflow. This is done within an MLflow run, which not only logs the chain model under a specified name but also tracks various details about the model. The logging process ensures that all aspects of the chain are recorded, allowing for efficient version control and future retrieval.
prompt = PromptTemplate(
input_variables=["recipe", "customer_count"],
template=template_instruction,
)
chain = LLMChain(llm=llm, prompt=prompt)
mlflow.set_experiment("Cooking Assistant")
with mlflow.start_run():
model_info = mlflow.langchain.log_model(chain, name="langchain_model")
If we navigate to the MLflow UI, we'll see our logged LangChain model.
In this part of our tutorial, we demonstrate the practical application of the logged LangChain model using MLflow. We load the model and run a prediction for a specific dish, showcasing the model's ability to assist in culinary preparation.
After logging our LangChain chain with MLflow, we proceed to load the model using MLflow's pyfunc.load_model function. This step is crucial as it brings our previously logged model into an executable state.
We then input a specific recipe along with the customer count into our model. In this case, we use the recipe for "boeuf bourginon" and specify that it's for 12 customers. The model, acting as a sous chef, processes this information and generates detailed preparation instructions.
The model's output provides a comprehensive guide on preparing "boeuf bourginon," covering several critical aspects:
This example demonstrates the power and utility of combining LangChain and MLflow in a practical scenario. It highlights how such an integration can effectively translate complex requirements into actionable steps, aiding in tasks that require precision and careful planning.
loaded_model = mlflow.pyfunc.load_model(model_info.model_uri)
dish1 = loaded_model.predict({"recipe": "boeuf bourginon", "customer_count": "4"})
print(dish1[0])
dish2 = loaded_model.predict({"recipe": "Okonomiyaki", "customer_count": "12"})
print(dish2[0])
In the final step of our tutorial, we execute another prediction using our LangChain model. This time, we explore the preparation for "Okonomiyaki," a Japanese dish, for 12 customers. This demonstrates the model's adaptability and versatility across various cuisines.
The model processes the input for "Okonomiyaki" and outputs detailed preparation steps. This includes listing the ingredients, explaining the preparation techniques, guiding ingredient staging, and detailing the required cooking implements, showcasing the model's capability to handle diverse recipes with precision.
This tutorial offered an insightful journey through creating, managing, and utilizing a LangChain model with MLflow for culinary preparation. It showcased the practical applications and adaptability of LangChain in complex scenarios. We hope this experience has provided valuable knowledge and encourages you to further explore and innovate using LangChain and MLflow in your projects. Happy coding!
To continue learning about the capabilities of MLflow and LangChain in more complex examples, we encourage you to continue your learning with the additional LangChain tutorials.