Binding
Author: Wonyoung Lee
Peer Review:
Proofread : Chaeyoon Kim
This is a part of LangChain Open Tutorial
Overview
This tutorial covers a scenario where you need to pass constant arguments(not included in the output of the previous Runnable or user input) when calling a Runnable inside a Runnable sequence. In such cases, Runnable.bind() is a convenient way to pass these arguments
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup tools, useful functions and utilities for tutorials.You can check out the
langchain-opentutorialfor more details.
Load sample text and output the content.
You can alternatively set OPENAI_API_KEY in .env file and load it.
[Note] This is not necessary if you've already set OPENAI_API_KEY in previous steps.
Runtime Arguments Binding
This section explains how to use Runnable.bind() to pass constant arguments to a Runnable within a sequence, especially when those arguments aren't part of the previous Runnable's output or user input.
Passing variables to prompts:
Use
RunnablePassthroughto pass the{equation_statement}variable to the prompt.Use
StrOutputParserto parse the model's output into a string, creating arunnableobject.Call the
runnable.invoke()method to pass the equation statement (e.g., "x raised to the third plus seven equals 12") and get the result.
Using bind() method with stop words
For controlling the end of the model's output using a specific stop word, you can use model.bind() to instruct the model to halt its generation upon encountering the stop token such as SOLUTION.
Connecting OpenAI Functions
bind() is particularly useful for connecting OpenAI Functions with compatible OpenAI models.
Let's define openai_function according to a schema.
Binding the solver function.
We can then use the bind() method to associate a function call (like solver) with the language model.
Connecting OpenAI Tools
This section explains how to connect and use OpenAI tools within your LangChain applications.
The tools object simplifies using various OpenAI features.
For example, calling the tool.run method with a natural language query allows the model to utilize the specified tool to generate a response.
Binding tools and invoking the model:
Use
bind()to associatetoolswith the language model.Call the
invoke()method on the bound model, by providing a natural language question as input.
Last updated