You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/machine-learning/prompt-flow/how-to-integrate-with-langchain.md
+8-17Lines changed: 8 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ The [LangChain](https://python.langchain.com) Python library is a framework for
21
21
22
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.
23
23
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.
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 with Python nodes that contain your custom LangChain code.
25
25
26
26
## Prerequisites
27
27
@@ -63,17 +63,11 @@ To create a flow, select **Create** on the **Prompt flow** page in Azure Machine
63
63
64
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.
65
65
66
-
There are two ways to convert your LangChain code into a flow:
66
+
There are two ways to convert your LangChain code into an Azure Machine Learning prompt flow. The type of flow to implement depends on your use case.
67
67
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.
68
+
- For better experiment management, you can convert your code to use Azure Machine Learning Python, LLM, and prompt tools in the flow. You extract the prompt template from your code into a prompt node, and put the remaining code in single or multiple Python nodes or tools. This option allows easy prompt tuning by running flow variants. You can choose the optimal prompt based on evaluation results.
70
69
71
-
The type of flow to implement depends on your use case.
72
-
73
-
| Flow type | Implementation | Use case |
74
-
|-------| -------- | -------- | -------- |
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. |
70
+
- For a simpler conversion process, you can call the LangChain LLM library directly from within your Python nodes. All your code runs in Python nodes, including prompt definitions. This option permits faster batch testing based on larger scale datasets.
77
71
78
72
The following example shows a flow that uses both prompt nodes and Python nodes:
79
73
@@ -90,23 +84,20 @@ After you structure your flow and move your code to specific tool nodes, you nee
90
84
1. In your Python code, import the custom connection library by entering `from promptflow.connections import CustomConnection`.
91
85
92
86
>[!NOTE]
93
-
>For an Azure OpenAI connection, use `from promptflow.connections import AzureOpenAIConnection`.
87
+
>To import an Azure OpenAI connection, use `from promptflow.connections import AzureOpenAIConnection`.
94
88
95
89
1. In your tool function, define an input parameter of the type `CustomConnection`.
96
90
91
+
1. Replace the environment variables that originally defined the key and credential with the corresponding key added in the connection.
92
+
97
93
:::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":::
98
94
99
-
1. Parse the input to the input section, and then select your custom connection in the value dropdown.
95
+
1. Parse the input to the input section of the node UI, and then select your custom connection in the **Value** dropdown.
100
96
101
97
:::image type="content" source="./media/how-to-integrate-with-langchain/custom-connection-python-node-2.png" alt-text="Screenshot of the chain node highlighting the connection. " lightbox = "./media/how-to-integrate-with-langchain/custom-connection-python-node-2.png":::
102
98
103
-
1. In your code, replace the environment variables that originally defined the key and credential with the corresponding key added in the connection.
104
99
1. Save, return to the authoring page, and also configure the connection parameter in the node input.
105
100
106
-
If you have a LangChain code that consumes an AzureOpenAI model, import the library by entering `from promptflow.connections import AzureOpenAIConnection`. Replace the environment variables with the corresponding keys from the Azure OpenAI connection.
107
-
108
-
:::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":::
109
-
110
101
### Configure input and output
111
102
112
103
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).
0 commit comments