Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 67 additions & 38 deletions 13-LangChain-Expression-Language/10-Binding.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id": "8f9cbe9d",
"metadata": {},
"source": [
"# Runtime Arguments Binding\n",
"# Binding\n",
"\n",
"- Author: [Wonyoung Lee](https://github.com/BaBetterB)\n",
"- Peer Review: \n",
Expand All @@ -17,18 +17,21 @@
"\n",
"## Overview\n",
"\n",
"This tutorial covers a scenario where, when calling a Runnable inside a Runnable sequence, we need to pass constant arguments that are not included in the output of the previous Runnable or user input. \n",
"In such cases, `Runnable.bind()` can be used to easily pass these arguments.\n",
"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\n",
"\n",
"\n",
"### Table of Contents\n",
"\n",
"- [Overview](#overview)\n",
"- [Environement Setup](#environment-setup)\n",
"- [Runtime Arguments Binding](#runtime-arguments-binding)\n",
"- [Connecting OpenAI Functions](#connecting-openai-functions)\n",
"- [Connecting OpenAI Tools](#connecting-openai-tools)\n",
"\n",
"### References\n",
"\n",
"- [LangChain RunnablePassthrough API reference](https://python.langchain.com/api_reference/core/runnables/langchain_core.runnables.passthrough.RunnablePassthrough.html)\n",
"- [LangChain ChatPromptTemplate API reference](https://python.langchain.com/api_reference/core/prompts/langchain_core.prompts.chat.ChatPromptTemplate.html)\n",
"\n",
"----\n",
"\n",
Expand Down Expand Up @@ -103,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"id": "c38e67c0",
"metadata": {},
"outputs": [
Expand All @@ -125,7 +128,7 @@
" \"LANGCHAIN_API_KEY\": \"\",\n",
" \"LANGCHAIN_TRACING_V2\": \"true\",\n",
" \"LANGCHAIN_ENDPOINT\": \"https://api.smith.langchain.com\",\n",
" \"LANGCHAIN_PROJECT\": \"Runtime Arguments Binding\", # title\n",
" \"LANGCHAIN_PROJECT\": \"Binding\", # title\n",
" }\n",
")"
]
Expand All @@ -142,10 +145,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "5bc25432",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Configuration File for Managing API Keys as Environment Variables\n",
"from dotenv import load_dotenv\n",
Expand All @@ -159,14 +173,20 @@
"id": "3fb790a1",
"metadata": {},
"source": [
"Use `RunnablePassthrough` to pass the `{equation_statement}` variable to the prompt, and use `StrOutputParser` to parse the model's output into a string, creating a `runnable` object.\n",
"## Runtime Arguments Binding\n",
"\n",
"The `runnable.invoke()` method is called to pass the equation statement \"x raised to the third plus seven equals 12\" and output the result."
"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 use input.\n",
"\n",
"**Passing variables to prompts:**\n",
"\n",
"1. Use `RunnablePassthrough` to pass the `{equation_statement}` variable to the prompt.\n",
"2. Use `StrOutputParser` to parse the model's output into a string, creating a `runnable` object.\n",
"3. Call the `runnable.invoke()` method to pass the equation statement (e.g., \\\"x raised to the third plus seven equals 12\\\") get the result."
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 5,
"id": "649b24d7",
"metadata": {},
"outputs": [
Expand All @@ -175,9 +195,17 @@
"output_type": "stream",
"text": [
"EQUATION: x^3 + 7 = 12\n",
"SOLUTION: x^3 = 12 - 7\n",
"x^3 = 5\n",
"x = ∛5\n"
"\n",
"SOLUTION:\n",
"1. Subtract 7 from both sides of the equation to isolate the x^3 term:\n",
" x^3 + 7 - 7 = 12 - 7\n",
" x^3 = 5\n",
"\n",
"2. Take the cube root of both sides to solve for x:\n",
" x = 5^(1/3)\n",
"\n",
"Therefore, the solution is:\n",
"x ≈ 1.71 (rounded to two decimal places)\n"
]
}
],
Expand Down Expand Up @@ -220,14 +248,14 @@
"id": "ed4ced2f",
"metadata": {},
"source": [
"Using bind() Method with Stop Word.\n",
"You may want to call the model using a specific `stop` word. \n",
"`model.bind()` can be used to call the language model and stop the generation at the \"SOLUTION\" token."
"**Using bind() method with stop words**\n",
"\n",
"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 like `SOLUTION`."
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 6,
"id": "9d94b8e3",
"metadata": {},
"outputs": [
Expand All @@ -236,6 +264,7 @@
"output_type": "stream",
"text": [
"EQUATION: x^3 + 7 = 12\n",
"\n",
"\n"
]
}
Expand All @@ -261,14 +290,14 @@
"source": [
"## Connecting OpenAI Functions\n",
"\n",
"One particularly useful way to use bind() is to connect OpenAI Functions with compatible OpenAI models.\n",
"`bind()` is particularly useful for connecting OpenAI Functions with compatible OpenAI models.\n",
"\n",
"Below is the code that defines `OpenAI Functions` according to a schema.\n"
"Let's define `openai_function` according to a schema."
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 7,
"id": "0b7cd50c",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -302,23 +331,24 @@
"id": "b3294828",
"metadata": {},
"source": [
"Binding the solver Function.\n",
"We use the `bind()` method to bind the function call named `solver` to the model."
"**Binding a solver function.**\n",
"\n",
"We can then use the `bind()` method to associate a function call (like `solver`) with the language model."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "edb2c5a4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n\"equation\": \"x^3 + 7 = 12\",\\n\"solution\": \"x = ∛5\"\\n}', 'name': 'solver'}, 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 28, 'prompt_tokens': 95, 'total_tokens': 123, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4-0613', 'system_fingerprint': None, 'finish_reason': 'stop', 'logprobs': None}, id='run-6fa11a89-41ec-4316-8a75-beab094d7803-0', usage_metadata={'input_tokens': 95, 'output_tokens': 28, 'total_tokens': 123, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})"
"AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\"equation\":\"x^3 + 7 = 12\",\"solution\":\"x^3 = 12 - 7; x^3 = 5; x = 5^(1/3)\"}', 'name': 'solver'}, 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 44, 'prompt_tokens': 95, 'total_tokens': 139, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_50cad350e4', 'finish_reason': 'stop', 'logprobs': None}, id='run-bb333525-2117-4a09-bf1c-c6bdca21b50c-0', usage_metadata={'input_tokens': 95, 'output_tokens': 44, 'total_tokens': 139, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})"
]
},
"execution_count": 17,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -358,16 +388,14 @@
"source": [
"## Connecting OpenAI Tools\n",
"\n",
"Here’s how you can connect and use OpenAI tools.\n",
"\n",
"The tools object helps you use various OpenAI features easily.\n",
"\n",
"For example, by calling the `tool.run` method with a natural language question, the model can generate an answer to that question."
"This section explains how to connect and use OpenAI tools within your LangChain applications.\n",
"The `tools` object simplifies using various OpenAI features.\n",
"For example, calling the `tool.run` method with a natural language query allows the model to utilize the spcified tool to generate a response."
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 9,
"id": "2fce5808",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -400,24 +428,25 @@
"id": "8a51880d",
"metadata": {},
"source": [
"Binding Tools and Invoking the Model\n",
"- Use `bind()` to bind `tools` to the model.\n",
"- Call the `invoke()` method with a question like \"Tell me the current weather in San Francisco, New York, and Los Angeles?\""
"**Binding tools and invoking the model:**\n",
"\n",
"1. Use `bind()` to associate `tools` with the language model.\n",
"2. Call the `invoke()` method on the bound model, providing a natural language question as input.\n"
]
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 10,
"id": "3b5c0149",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_q8WYveGpmjfckyAU3TENgNMi', 'function': {'arguments': '{\\n \"location\": \"San Francisco, CA\"\\n}', 'name': 'get_current_weather'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 20, 'prompt_tokens': 92, 'total_tokens': 112, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4-0613', 'system_fingerprint': None, 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-9c4e4e91-244c-49d1-a54b-b0922a3bc228-0', tool_calls=[{'name': 'get_current_weather', 'args': {'location': 'San Francisco, CA'}, 'id': 'call_q8WYveGpmjfckyAU3TENgNMi', 'type': 'tool_call'}], usage_metadata={'input_tokens': 92, 'output_tokens': 20, 'total_tokens': 112, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})"
"AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_ixydz9CyFSyB0LHUATftfdXA', 'function': {'arguments': '{\"location\": \"San Francisco, CA\"}', 'name': 'get_current_weather'}, 'type': 'function'}, {'id': 'call_VFAGF4YanFQVg1lJQ9x1miR3', 'function': {'arguments': '{\"location\": \"New York, NY\"}', 'name': 'get_current_weather'}, 'type': 'function'}, {'id': 'call_RMYL8pWFlMarWkOPkigKQSug', 'function': {'arguments': '{\"location\": \"Los Angeles, CA\"}', 'name': 'get_current_weather'}, 'type': 'function'}], 'refusal': None}, response_metadata={'token_usage': {'completion_tokens': 71, 'prompt_tokens': 90, 'total_tokens': 161, 'completion_tokens_details': {'accepted_prediction_tokens': 0, 'audio_tokens': 0, 'reasoning_tokens': 0, 'rejected_prediction_tokens': 0}, 'prompt_tokens_details': {'audio_tokens': 0, 'cached_tokens': 0}}, 'model_name': 'gpt-4o-2024-08-06', 'system_fingerprint': 'fp_50cad350e4', 'finish_reason': 'tool_calls', 'logprobs': None}, id='run-50665b85-c055-413a-8a40-3c1070aa3c45-0', tool_calls=[{'name': 'get_current_weather', 'args': {'location': 'San Francisco, CA'}, 'id': 'call_ixydz9CyFSyB0LHUATftfdXA', 'type': 'tool_call'}, {'name': 'get_current_weather', 'args': {'location': 'New York, NY'}, 'id': 'call_VFAGF4YanFQVg1lJQ9x1miR3', 'type': 'tool_call'}, {'name': 'get_current_weather', 'args': {'location': 'Los Angeles, CA'}, 'id': 'call_RMYL8pWFlMarWkOPkigKQSug', 'type': 'tool_call'}], usage_metadata={'input_tokens': 90, 'output_tokens': 71, 'total_tokens': 161, 'input_token_details': {'audio': 0, 'cache_read': 0}, 'output_token_details': {'audio': 0, 'reasoning': 0}})"
]
},
"execution_count": 25,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -434,7 +463,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "langchain-opentutorial-EWknDWEP-py3.11",
"display_name": "langchain-opentutorial-HDS-w_h3-py3.11",
"language": "python",
"name": "python3"
},
Expand Down
Loading