Skip to content

Commit 15c5095

Browse files
committed
partial update
1 parent ba6286e commit 15c5095

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

articles/machine-learning/prompt-flow/how-to-integrate-with-langchain.md

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@ ms.topic: how-to
1212
author: lgayhardt
1313
ms.author: lagayhar
1414
ms.reviewer: chenlujiao
15-
ms.date: 10/21/2024
15+
ms.date: 10/22/2024
1616
---
1717

1818
# Integrate LangChain in prompt flows
1919

20-
The [LangChain](https://python.langchain.com) Python library is a framework for developing applications powered by LLMs, agents, and dependency tools. You can use LangChain in Azure Machine Learning prompt flows. This article shows you how to supercharge your LangChain development with prompt flow.
20+
The [LangChain](https://python.langchain.com) Python library is a framework for developing applications powered by large language models (LLMs), agents, and dependency tools. You can use LangChain in Azure Machine Learning prompt flows. This article shows you how to supercharge your LangChain development with prompt flow.
2121

22-
The integration of LangChain with prompt flow is a powerful combination that can help you build and test your custom language models with ease. You can use LangChain modules to initially build the flow, and then use the prompt flow process to scale experiments for bulk testing, evaluation, and eventual deployment. For example, you can conduct larger scale experiments based on larger datasets.
22+
The integration of LangChain with prompt flow is a powerful combination that can help you build and test your custom language models with ease. You can use LangChain modules to initially build the flow, and then use the prompt flow process to scale experiments for bulk testing, evaluation, and eventual deployment. For example, you can conduct large scale experiments based on larger datasets.
2323

24-
If you already have a local prompt flow based on LangChain code, you can use streamlined prompt flow integration to easily convert it into a flow for further experimentation. Or, if you're already familiar with the LangChain Python SDK and prefer to use its classes and functions directly, you can easily build flows by using Python nodes that contain your custom code.
24+
If you already have a local prompt flow based on LangChain code, you can use streamlined prompt flow integration to easily convert it into an Azure Machine Learning prompt flow for further experimentation. Or, if you prefer to use LangChain SDK classes and functions directly, you can easily build flows that use Python nodes containing your custom LangChain code.
2525

2626
## Prerequisites
2727

28-
- Local LangChain flow code that's properly tested and ready for deployment.
29-
- A compute session that can run the prompt flow by adding packages listed in the *requirements.txt* file. For more information, see [Manage prompt flow compute session](how-to-manage-compute-session.md).
28+
- A local LangChain flow that's properly tested and ready for deployment.
29+
- A compute session that can run the Machine Learning prompt flow by adding packages listed in the *requirements.txt* file, including `langchain`. For more information, see [Manage prompt flow compute session](how-to-manage-compute-session.md).
3030

3131
## Convert LangChain code into prompt flows
3232

3333
The rest of the article describes how to convert your local LangChain code to a runnable Azure Machine Learning prompt flow.
3434

3535
### Convert credentials to a prompt flow connection
3636

37-
When you developed your LangChain code, you might have [defined environment variables](https://python.langchain.com/docs/integrations/platforms/microsoft) to store credentials such as the AzureOpenAI API key, which is necessary for invoking AzureOpenAI models. For example, the following code sets environmental variables for the OpenAI API type, key, base or endpoint, and version:
37+
Your LangChain code might [define environment variables](https://python.langchain.com/docs/integrations/platforms/microsoft) to store credentials, such as the AzureOpenAI API key necessary for invoking AzureOpenAI models. For example, the following image shows environmental variables being set for OpenAI API type, key, base, and version.
3838

3939
:::image type="content" source="./media/how-to-integrate-with-langchain/langchain-env-variables.png" alt-text="Screenshot of Azure OpenAI example in LangChain.":::
4040

41-
When you run LangChain code in an Azure Machine Learning prompt flow in the cloud, it's better not to expose credentials as environment variables. To securely store and manage credentials separately from your code, you should convert the environmental variables into a prompt flow connection.
41+
When you run an Azure Machine Learning prompt flow in the cloud, it's better not to expose credentials as environment variables. To securely store and manage credentials separately from your code, you should convert the environmental variables into a prompt flow connection.
4242

43-
To create a connection that securely stores your credentials, such as your LLM API key or other required credentials, follow these instructions:
43+
To create a connection that securely stores credentials such as your LLM API key or other required keys, follow these instructions:
4444

4545
1. On the **Prompt flow** page in your Azure Machine Learning workspace, select the **Connections** tab, and then select **Create**.
4646
1. Select a connection type from the dropdown list. For this example, select **Custom**.
@@ -51,46 +51,48 @@ To create a connection that securely stores your credentials, such as your LLM A
5151

5252
:::image type="content" source="./media/how-to-integrate-with-langchain/custom-connection-2.png" alt-text="Screenshot of adding custom connection key-value pairs.":::
5353

54-
1. To store an encrypted value for a key, select the **is secret** checkbox next to one or more key-value pairs. At least one value must be set as secret for the connection to create successfully.
54+
1. To store an encrypted value for a key, select the **is secret** checkbox next to one or more key-value pairs. At least one value must be set as secret for the connection creation to succeed.
5555

5656
1. Select **Save**.
5757

58-
The custom connection replaces the key and credential you explicitly defined in your LangChain code. To use the custom connection in your flow, see [Configure connection](#configure-connection).
58+
The custom connection can replace the keys and credentials or corresponding environmental variables explicitly defined in your LangChain code. To use the custom connection in the flow, see [Configure connection](#configure-connection).
5959

6060
### Convert LangChain code to a runnable flow
6161

62-
To create a flow, select **Create** on the **Prompt flow** page in Azure Machine Learning studio. For detailed instructions and information, see [Develop prompt flow](how-to-develop-a-standard-flow.md).
62+
To create a flow, select **Create** on the **Prompt flow** page in Azure Machine Learning studio, and choose a flow type. On the flow authoring page, start your compute session before you author the flow. Select tool types at the top of the pane to insert corresponding nodes into the flow. For detailed flow authoring instructions, see [Develop prompt flow](how-to-develop-a-standard-flow.md).
6363

64-
All your LangChain code can directly run in Python tools in your flow, as long as your compute session contains the dependency packages. There are two ways to convert your LangChain code into a flow:
64+
All your LangChain code can directly run in Python nodes in your flow, as long as your compute session contains the `langchain` package dependency.
6565

66-
- For a simple conversion process, you can directly initialize the LLM model and invoke it in a Python node by using the LangChain integrated LLM library.
67-
- For better experiment management, you can convert your LLM model to use Azure Machine Learning Python LLM tools in the flow.
66+
There are two ways to convert your LangChain code into a flow:
6867

69-
Which type of flow to implement depends on your use case.
68+
- For a simple conversion process, you can initialize and invoke the LLM model within a Python node by using the integrated LangChain LLM library.
69+
- For better experiment management, you can convert the LLM model to use Azure Machine Learning Python LLM tools in the flow.
70+
71+
The type of flow to implement depends on your use case.
7072

7173
| Flow type | Implementation | Use case |
7274
|-------| -------- | -------- | -------- |
73-
| A flow that includes both prompt nodes and Python nodes | Extract your prompt template from your code into a prompt node, and combine the remaining code in single or multiple Python nodes or tools. | Easily tune the prompt by running flow variants, and then choose the optimal prompt based on evaluation results.|
74-
| A flow that includes Python nodes only| Create a new flow with Python nodes only. All code, including prompt definitions, runs in Python nodes. | No explicit prompt tuning, but faster batch testing based on larger scale datasets. |
75+
| A flow that includes both prompt nodes and Python nodes | Extract your prompt template into a prompt node, and combine the remaining code in single or multiple Python nodes or tools. | Easy prompt tuning by running flow variants, to choose the optimal prompt based on evaluation results.|
76+
| A flow that includes Python nodes only| Create a new flow with Python nodes only. All code runs in Python nodes, including prompt definitions. | Faster batch testing based on larger scale datasets. |
7577

76-
The following examples show a flow that uses both prompt nodes and Python nodes:
78+
The following example shows a flow that uses both prompt nodes and Python nodes:
7779

7880
:::image type="content" source="./media/how-to-integrate-with-langchain/flow-node-a-1.png" alt-text="Screenshot of flows highlighting the prompt button and system template. " lightbox = "./media/how-to-integrate-with-langchain/flow-node-a-1.png":::
7981

80-
:::image type="content" source="./media/how-to-integrate-with-langchain/flow-node-a-2.png" alt-text="Screenshot of system template showing variant one and zero with the finish tuning button highlighted. " lightbox = "./media/how-to-integrate-with-langchain/flow-node-a-2.png":::
81-
8282
The following example shows a flow that uses Python nodes only:
8383

8484
:::image type="content" source="./media/how-to-integrate-with-langchain/flow-node-b.png" alt-text="Screenshot of flows showing the LangChain code node and graph. " lightbox = "./media/how-to-integrate-with-langchain/flow-node-b.png":::
8585

8686
### Configure connection
8787

88-
After you structure your flow and move your code to specific tool nodes, you need to replace your original environment variables with the corresponding keys in the connection. To do so, import the `promptflow.connections` library in your Python nodes.
88+
After you structure your flow and move your code to specific tool nodes, you need to replace your original environment variables with the corresponding keys from your connection. To use the custom connection you created, follow these steps:
89+
90+
1. In your Python code, import the custom connection library by entering `from promptflow.connections import CustomConnection`.
8991

90-
To use the custom connection you created, follow these steps:
92+
>[!NOTE]
93+
>For an Azure OpenAI connection, use `from promptflow.connections import AzureOpenAIConnection`.
9194
92-
1. In your Python node, import the custom connection library by entering `from promptflow.connections import CustomConnection`.
93-
1. Define an input parameter of the type `CustomConnection` in the tool function.
95+
1. In your tool function, define an input parameter of the type `CustomConnection`.
9496

9597
:::image type="content" source="./media/how-to-integrate-with-langchain/custom-connection-python-node-1.png" alt-text="Screenshot of doc search chain node highlighting the custom connection. " lightbox = "./media/how-to-integrate-with-langchain/custom-connection-python-node-1.png":::
9698

@@ -105,7 +107,7 @@ If you have a LangChain code that consumes an AzureOpenAI model, import the libr
105107

106108
:::image type="content" source="./media/how-to-integrate-with-langchain/code-consume-aoai.png" alt-text="Screenshot of LangChain code in prompt flow. " lightbox = "./media/how-to-integrate-with-langchain/code-consume-aoai.png":::
107109

108-
### Configure input and output**
110+
### Configure input and output
109111

110112
Before you run the flow, configure the node inputs and outputs and the overall flow inputs and outputs. This step is crucial to ensure that all the required data passes properly through the flow and produces desired results. For more information, see [Flow inputs and outputs](how-to-develop-flow.md#flow-input-and-output).
111113

-29.8 KB
Loading
-156 KB
Loading
-1.8 KB
Loading

0 commit comments

Comments
 (0)