Skip to content

Commit

Permalink
docs[minor]: Add Human-in-the-loop to tools use case (#4314)
Browse files Browse the repository at this point in the history
* docs[minor]: Add Human-in-the-loop to tools use case

* add human feedback part

* cr

* cr

* bad opening & closing quotesgit status

* chore: lint files

* fix build commands

* cr

* cr

* cr

* cr
  • Loading branch information
bracesproul committed Feb 8, 2024
1 parent 10e2dc9 commit 85f41f1
Show file tree
Hide file tree
Showing 11 changed files with 416 additions and 5 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,3 @@ langchain/api_refs_docs_build/dist/**/*
.docusaurus/
docs/build/
docs/api_refs/typedoc.json
docs/core_docs/**/*.md
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"zod": "npm:/zod",
"zod-to-json-schema": "npm:/zod-to-json-schema",
"@langchain/anthropic": "npm:@langchain/anthropic",
"node-llama-cpp": "npm:/node-llama-cpp"
"node-llama-cpp": "npm:/node-llama-cpp",
"readline": "https://deno.land/x/readline@v1.1.0/mod.ts"
}
}
13 changes: 12 additions & 1 deletion docs/core_docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,15 @@ yarn-error.log*
!.yarn/sdks
!.yarn/versions

/.quarto/
/.quarto/

# AUTO_GENERATED_DOCS
docs/use_cases/tool_use/human_in_the_loop.mdx
docs/use_cases/question_answering/streaming.mdx
docs/use_cases/question_answering/sources.mdx
docs/use_cases/question_answering/quickstart.mdx
docs/use_cases/question_answering/local_retrieval_qa.mdx
docs/use_cases/question_answering/conversational_retrieval_agents.mdx
docs/use_cases/question_answering/citations.mdx
docs/use_cases/question_answering/chat_history.mdx
docs/modules/model_io/output_parsers/types/openai_tools.mdx
193 changes: 193 additions & 0 deletions docs/core_docs/docs/use_cases/tool_use/human_in_the_loop.ipynb
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
}
6 changes: 4 additions & 2 deletions docs/core_docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"docusaurus": "docusaurus",
"start": "yarn build:typedoc && rimraf ./docs/api && NODE_OPTIONS=--max-old-space-size=7168 docusaurus start",
"build": "yarn clean && yarn build:typedoc && yarn quarto && rimraf ./build && NODE_OPTIONS=--max-old-space-size=7168 DOCUSAURUS_SSR_CONCURRENCY=4 docusaurus build",
"build:vercel": "yarn clean && yarn build:typedoc:vercel && bash ./vercel_build.sh && rimraf ./build && NODE_OPTIONS=--max-old-space-size=7168 DOCUSAURUS_SSR_CONCURRENCY=4 docusaurus build",
"build:vercel": "yarn clean && yarn build:typedoc:vercel && yarn quarto:vercel && rimraf ./build && NODE_OPTIONS=--max-old-space-size=7168 DOCUSAURUS_SSR_CONCURRENCY=4 docusaurus build",
"build:typedoc": "yarn workspace api_refs build",
"build:typedoc:vercel": "yarn workspace api_refs build:vercel",
"swizzle": "docusaurus swizzle",
Expand All @@ -22,7 +22,8 @@
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,md,mdx}\"",
"format:check": "prettier --check \"**/*.{js,jsx,ts,tsx,md,mdx}\"",
"clean": "rm -rf .docusaurus/ .turbo/ .build/",
"quarto": "quarto render docs/"
"quarto": "quarto render docs/ && node ./scripts/quatro-build.js",
"quarto:vercel": "bash ./scripts/vercel_build.sh && node ./scripts/quatro-build.js"
},
"dependencies": {
"@docusaurus/core": "2.4.3",
Expand All @@ -48,6 +49,7 @@
"eslint-plugin-jsx-a11y": "^6.6.0",
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
"glob": "^10.3.10",
"prettier": "^2.7.1",
"rimraf": "^5.0.1",
"swc-loader": "^0.2.3",
Expand Down
54 changes: 54 additions & 0 deletions docs/core_docs/scripts/quatro-build.js
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.
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"pg": "^8.11.0",
"pickleparser": "^0.2.1",
"prisma": "^4.11.0",
"readline": "^1.3.0",
"redis": "^4.6.13",
"sqlite3": "^5.1.4",
"typeorm": "^0.3.12",
Expand Down

0 comments on commit 85f41f1

Please sign in to comment.