-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs[minor]: Add Human-in-the-loop to tools use case #4314
Merged
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2d81b5e
docs[minor]: Add Human-in-the-loop to tools use case
bracesproul a0e47ae
add human feedback part
bracesproul ebb64d4
cr
bracesproul ff02ff7
cr
bracesproul 86c9a0d
bad opening & closing quotesgit status
bracesproul 9a863db
chore: lint files
bracesproul bd3aab3
merge main
bracesproul 5fc8cec
fix build commands
bracesproul fe062e0
cr
bracesproul f14a796
Merge branch 'main' into brace/human-in-loop
bracesproul 65883bb
cr
bracesproul a8e9699
cr
bracesproul 292e999
cr
bracesproul 42e78b8
Merge branch 'main' of https://github.com/langchain-ai/langchainjs in…
bracesproul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
193 changes: 193 additions & 0 deletions
193
docs/core_docs/docs/use_cases/tool_use/human_in_the_loop.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Human-in-the-loop\n", | ||
"\n", | ||
"There are certain tools that we don't trust a model to execute on its own. One thing we can do in such situations is require human approval before the tool is invoked." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Setup\n", | ||
"\n", | ||
"We'll need to install the following packages:\n", | ||
"\n", | ||
"```bash\n", | ||
"npm install langchain @langchain/core @langchain/openai readline zod\n", | ||
"```\n", | ||
"\n", | ||
"We'll use `readline` to handle accepting input from the user." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### LangSmith\n", | ||
"\n", | ||
"Many of the applications you build with LangChain will contain multiple steps with multiple invocations of LLM calls. As these applications get more and more complex, it becomes crucial to be able to inspect what exactly is going on inside your chain or agent. The best way to do this is with [LangSmith](https://smith.langchain.com/).\n", | ||
"\n", | ||
"Note that LangSmith is not needed, but it is helpful. If you do want to use LangSmith, after you sign up at the link above, make sure to set your environment variables to start logging traces:\n", | ||
"\n", | ||
"\n", | ||
"```bash\n", | ||
"export LANGCHAIN_TRACING_V2=true\n", | ||
"export LANGCHAIN_API_KEY=YOUR_KEY\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Chain\n", | ||
"\n", | ||
"Suppose we have the following (dummy) tools and tool-calling chain:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import { ChatOpenAI } from \"@langchain/openai\";\n", | ||
"import { Runnable, RunnableLambda, RunnablePassthrough } from \"@langchain/core/runnables\"\n", | ||
"import { StructuredTool } from \"@langchain/core/tools\";\n", | ||
"import { JsonOutputToolsParser } from \"langchain/output_parsers\";\n", | ||
"import { z } from \"zod\";\n", | ||
"\n", | ||
"class CountEmails extends StructuredTool {\n", | ||
" schema = z.object({\n", | ||
" lastNDays: z.number(),\n", | ||
" })\n", | ||
"\n", | ||
" name = \"count_emails\";\n", | ||
"\n", | ||
" description = \"Count the number of emails sent in the last N days.\";\n", | ||
"\n", | ||
" async _call(input: z.infer<typeof this.schema>): Promise<string> {\n", | ||
" return (input.lastNDays * 2).toString();\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"class SendEmail extends StructuredTool {\n", | ||
" schema = z.object({\n", | ||
" message: z.string(),\n", | ||
" recipient: z.string(),\n", | ||
" })\n", | ||
"\n", | ||
" name = \"send_email\";\n", | ||
"\n", | ||
" description = \"Send an email.\";\n", | ||
"\n", | ||
" async _call(input: z.infer<typeof this.schema>): Promise<string> {\n", | ||
" return `Successfully sent email to ${input.recipient}`;\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"const tools = [new CountEmails(), new SendEmail()];" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"const model = new ChatOpenAI({\n", | ||
" modelName: \"gpt-3.5-turbo\",\n", | ||
" temperature: 0\n", | ||
"}).bind({\n", | ||
" tools,\n", | ||
"});\n", | ||
"\n", | ||
"/**\n", | ||
" * Function for dynamically constructing the end of the chain based on the model-selected tool.\n", | ||
" */\n", | ||
"const callTool = (toolInvocation: Record<string, any>): Runnable => {\n", | ||
" const toolMap: Record<string, StructuredTool> = tools.reduce((acc, tool) => {\n", | ||
" acc[tool.name] = tool;\n", | ||
" return acc;\n", | ||
" }, {});\n", | ||
" const tool = toolMap[toolInvocation.type];\n", | ||
" return RunnablePassthrough.assign({ output: (input, config) => tool.invoke(input.args, config) });\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"// .map() allows us to apply a function to a list of inputs.\n", | ||
"const callToolList = new RunnableLambda({ func: callTool }).map();\n", | ||
"const chain = model.pipe(new JsonOutputToolsParser()).pipe(callToolList);" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[ { type: \u001b[32m\"count_emails\"\u001b[39m, args: { lastNDays: \u001b[33m5\u001b[39m }, output: \u001b[32m\"10\"\u001b[39m } ]" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"await chain.invoke(\"How many emails did I get in the last 5 days?\");" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Adding human approval\n", | ||
"\n", | ||
"We can add a simple human approval step to our toolChain function:\n", | ||
"\n", | ||
"import CodeBlock from \"@theme/CodeBlock\";\n", | ||
"import HumanFeedback from \"@examples/use_cases/human_in_the_loop/accept-feedback.ts\";\n", | ||
"\n", | ||
"<CodeBlock language=\"typescript\">{HumanFeedback}</CodeBlock>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"> #### Examine the LangSmith traces from the code above [here](https://smith.langchain.com/public/aac711ff-b1a1-4fd7-a298-0f20909259b6/r) and [here](https://smith.langchain.com/public/7b35ee77-b369-4b95-af4f-b83510f9a93b/r)." | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Deno", | ||
"language": "typescript", | ||
"name": "deno" | ||
}, | ||
"language_info": { | ||
"file_extension": ".ts", | ||
"mimetype": "text/x.typescript", | ||
"name": "typescript", | ||
"nb_converter": "script", | ||
"pygments_lexer": "typescript", | ||
"version": "5.3.3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const fs = require("node:fs/promises"); | ||
const { glob } = require("glob"); | ||
|
||
async function main() { | ||
const allIpynb = await glob("./docs/**/*.ipynb"); | ||
// iterate over each & rename to `.mdx` if a `.md` already exists | ||
const renamePromise = allIpynb.flatMap(async (ipynb) => { | ||
const md = ipynb.replace(".ipynb", ".md"); | ||
// verify file exists | ||
let fileExists = false; | ||
try { | ||
await fs.access(md, fs.constants.W_OK); | ||
fileExists = true; | ||
} catch (_) { | ||
// no-op | ||
} | ||
if (!fileExists) { | ||
return []; | ||
} | ||
// rename | ||
await fs.rename(md, md + "x"); | ||
|
||
// Quatro generates irregular quotes in the markdown files | ||
const badOpeningQuote = `“`; | ||
const badClosingQuote = `”`; | ||
const goodQuote = `"`; | ||
const contents = await fs.readFile(md + "x", "utf-8"); | ||
if ( | ||
contents.includes(badOpeningQuote) || | ||
contents.includes(badClosingQuote) | ||
) { | ||
// Regex search and replace all badQuotes | ||
const newContents = contents | ||
.replace(new RegExp(badOpeningQuote, "g"), goodQuote) | ||
.replace(new RegExp(badClosingQuote, "g"), goodQuote); | ||
await fs.writeFile(md + "x", newContents); | ||
} | ||
|
||
return [`${md}x`]; | ||
}); | ||
|
||
const allRenames = await Promise.all(renamePromise); | ||
const pathToRootGitignore = ".gitignore"; | ||
let gitignore = await fs.readFile(pathToRootGitignore, "utf-8"); | ||
gitignore = gitignore.split("# AUTO_GENERATED_DOCS")[0]; | ||
gitignore += "# AUTO_GENERATED_DOCS\n"; | ||
gitignore += allRenames.join("\n"); | ||
await fs.writeFile(pathToRootGitignore, gitignore); | ||
} | ||
|
||
main().catch((e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
examples/src/use_cases/human_in_the_loop/accept-feedback.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import * as readline from "readline"; | ||
import { JsonOutputToolsParser } from "langchain/output_parsers"; | ||
import { callToolList, model } from "./helpers.js"; | ||
|
||
// Use readline to ask the user for approval | ||
function askQuestion(question: string): Promise<string> { | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
return new Promise((resolve) => { | ||
rl.question(question, (answer) => { | ||
rl.close(); | ||
resolve(answer); | ||
}); | ||
}); | ||
} | ||
|
||
async function humanApproval(toolInvocations: any[]): Promise<any[]> { | ||
const toolStrs = toolInvocations | ||
.map((toolCall) => JSON.stringify(toolCall, null, 2)) | ||
.join("\n\n"); | ||
const msg = `Do you approve of the following tool invocations\n\n${toolStrs} | ||
|
||
Anything except 'Y'/'Yes' (case-insensitive) will be treated as a no.\n`; | ||
|
||
// Ask the user for approval | ||
const resp = await askQuestion(msg); | ||
if (!["yes", "y"].includes(resp.toLowerCase())) { | ||
throw new Error(`Tool invocations not approved:\n\n${toolStrs}`); | ||
} | ||
return toolInvocations; | ||
} | ||
|
||
const chain = model | ||
.pipe(new JsonOutputToolsParser()) | ||
.pipe(humanApproval) | ||
.pipe(callToolList); | ||
|
||
const response = await chain.invoke( | ||
"how many emails did i get in the last 5 days?" | ||
); | ||
console.log(response); | ||
/** | ||
Do you approve of the following tool invocations | ||
|
||
{ | ||
"type": "count_emails", | ||
"args": { | ||
"lastNDays": 5 | ||
} | ||
} | ||
|
||
Anything except 'Y'/'Yes' (case-insensitive) will be treated as a no. | ||
y | ||
[ { type: 'count_emails', args: { lastNDays: 5 }, output: '10' } ] | ||
*/ | ||
const response2 = await chain.invoke( | ||
"Send sally@gmail.com an email saying 'What's up homie'" | ||
); | ||
console.log(response2); | ||
/** | ||
Do you approve of the following tool invocations | ||
|
||
{ | ||
"type": "send_email", | ||
"args": { | ||
"message": "What's up homie", | ||
"recipient": "sally@gmail.com" | ||
} | ||
} | ||
|
||
Anything except 'Y'/'Yes' (case-insensitive) will be treated as a no. | ||
y | ||
[ | ||
{ | ||
type: 'send_email', | ||
args: { message: "What's up homie", recipient: 'sally@gmail.com' }, | ||
output: 'Successfully sent email to sally@gmail.com' | ||
} | ||
] | ||
*/ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there! I noticed that the recent change in the package.json file has modified the "build:vercel" script, potentially affecting the project's dependencies. This is just a heads-up for the maintainers to review the change and ensure it aligns with the project's dependency management.