Skip to content

Commit

Permalink
Fix TS in docs (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed May 20, 2024
1 parent 7190937 commit 963db63
Show file tree
Hide file tree
Showing 14 changed files with 1,676 additions and 2,412 deletions.
222 changes: 106 additions & 116 deletions examples/how-tos/branching.ipynb

Large diffs are not rendered by default.

171 changes: 65 additions & 106 deletions examples/how-tos/configuration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "8221c040",
"metadata": {},
"source": [
"# Configuration\n",
Expand Down Expand Up @@ -32,20 +33,32 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"id": "f0dcd657",
"metadata": {
"lines_to_next_cell": 2
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Configuration: LangGraphJS\n"
]
}
],
"source": [
"// Deno.env.set(\"OPENAI_API_KEY\", \"sk_...\");\n",
"// process.env.OPENAI_API_KEY = \"sk_...\";\n",
"\n",
"// Optional, add tracing in LangSmith\n",
"// Deno.env.set(\"LANGCHAIN_API_KEY\", \"ls__...\");\n",
"// Deno.env.set(\"LANGCHAIN_CALLBACKS_BACKGROUND\", \"true\");\n",
"Deno.env.set(\"LANGCHAIN_TRACING_V2\", \"true\");\n",
"Deno.env.set(\"LANGCHAIN_PROJECT\", \"Configuration: LangGraphJS\");\n"
"// process.env.LANGCHAIN_API_KEY = \"ls__...\";\n",
"// process.env.LANGCHAIN_CALLBACKS_BACKGROUND = \"true\";\n",
"process.env.LANGCHAIN_TRACING_V2 = \"true\";\n",
"process.env.LANGCHAIN_PROJECT = \"Configuration: LangGraphJS\";"
]
},
{
"cell_type": "markdown",
"id": "a04f018e",
"metadata": {},
"source": [
"## Define the graph\n",
Expand All @@ -56,72 +69,17 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "bdf2fe0f",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[WARN]: You have enabled LangSmith tracing without backgrounding callbacks.\n",
"[WARN]: If you are not using a serverless environment where you must wait for tracing calls to finish,\n",
"[WARN]: we suggest setting \"process.env.LANGCHAIN_CALLBACKS_BACKGROUND=true\" to avoid additional latency.\n",
"[WARN]: You have enabled LangSmith tracing without backgrounding callbacks.\n",
"[WARN]: If you are not using a serverless environment where you must wait for tracing calls to finish,\n",
"[WARN]: we suggest setting \"process.env.LANGCHAIN_CALLBACKS_BACKGROUND=true\" to avoid additional latency.\n"
]
},
{
"data": {
"text/plain": [
"StateGraph {\n",
" nodes: {\n",
" fetchUserInfo: RunnableLambda {\n",
" lc_serializable: \u001b[33mfalse\u001b[39m,\n",
" lc_kwargs: { func: \u001b[36m[AsyncFunction: fetchUserInformation]\u001b[39m },\n",
" lc_runnable: \u001b[33mtrue\u001b[39m,\n",
" name: \u001b[90mundefined\u001b[39m,\n",
" lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"runnables\"\u001b[39m ],\n",
" func: \u001b[36m[AsyncFunction: fetchUserInformation]\u001b[39m\n",
" },\n",
" agent: RunnableLambda {\n",
" lc_serializable: \u001b[33mfalse\u001b[39m,\n",
" lc_kwargs: { func: \u001b[36m[AsyncFunction: callModel]\u001b[39m },\n",
" lc_runnable: \u001b[33mtrue\u001b[39m,\n",
" name: \u001b[90mundefined\u001b[39m,\n",
" lc_namespace: [ \u001b[32m\"langchain_core\"\u001b[39m, \u001b[32m\"runnables\"\u001b[39m ],\n",
" func: \u001b[36m[AsyncFunction: callModel]\u001b[39m\n",
" }\n",
" },\n",
" edges: Set(3) {\n",
" [ \u001b[32m\"__start__\"\u001b[39m, \u001b[32m\"fetchUserInfo\"\u001b[39m ],\n",
" [ \u001b[32m\"fetchUserInfo\"\u001b[39m, \u001b[32m\"agent\"\u001b[39m ],\n",
" [ \u001b[32m\"agent\"\u001b[39m, \u001b[32m\"__end__\"\u001b[39m ]\n",
" },\n",
" branches: {},\n",
" entryPoint: \u001b[90mundefined\u001b[39m,\n",
" compiled: \u001b[33mtrue\u001b[39m,\n",
" supportMultipleEdges: \u001b[33mtrue\u001b[39m,\n",
" channels: {\n",
" messages: BinaryOperatorAggregate {\n",
" lc_graph_name: \u001b[32m\"BinaryOperatorAggregate\"\u001b[39m,\n",
" value: [],\n",
" operator: \u001b[36m[Function: value]\u001b[39m,\n",
" initialValueFactory: \u001b[36m[Function: default]\u001b[39m\n",
" },\n",
" userInfo: BinaryOperatorAggregate {\n",
" lc_graph_name: \u001b[32m\"BinaryOperatorAggregate\"\u001b[39m,\n",
" value: \u001b[32m\"N/A\"\u001b[39m,\n",
" operator: \u001b[36m[Function: value]\u001b[39m,\n",
" initialValueFactory: \u001b[36m[Function: default]\u001b[39m\n",
" }\n",
" },\n",
" waitingEdges: Set(0) {}\n",
"}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
Expand All @@ -130,21 +88,21 @@
"import { ChatAnthropic } from \"@langchain/anthropic\";\n",
"import { ChatPromptTemplate } from \"@langchain/core/prompts\";\n",
"import { RunnableConfig } from \"@langchain/core/runnables\";\n",
"import { END, MemorySaver, START, StateGraph } from \"@langchain/langgraph\";\n",
"import {\n",
" END,\n",
" MemorySaver,\n",
" START,\n",
" StateGraph,\n",
" StateGraphArgs,\n",
"} from \"@langchain/langgraph\";\n",
"\n",
"interface IState {\n",
" messages: {\n",
" value: (x: BaseMessage[], y: BaseMessage[]) => BaseMessage[];\n",
" default: () => BaseMessage[];\n",
" };\n",
" userInfo: {\n",
" value: (x: string, y: string) => string;\n",
" default: () => string;\n",
" };\n",
" messages: BaseMessage[];\n",
" userInfo: string;\n",
"}\n",
"\n",
"// This defines the agent state\n",
"const graphState: IState = {\n",
"const graphState: StateGraphArgs<IState>[\"channels\"] = {\n",
" messages: {\n",
" value: (x: BaseMessage[], y: BaseMessage[]) => x.concat(y),\n",
" default: () => [],\n",
Expand All @@ -163,7 +121,7 @@
"]);\n",
"\n",
"const callModel = async (\n",
" state: { messages: Array<BaseMessage>; userInfo: string },\n",
" state: { messages: BaseMessage[]; userInfo: string },\n",
" config: RunnableConfig,\n",
") => {\n",
" const { messages, userInfo } = state;\n",
Expand All @@ -182,12 +140,8 @@
" return { messages: [response] };\n",
"};\n",
"\n",
"const workflow = new StateGraph({\n",
" channels: graphState,\n",
"});\n",
"\n",
"const fetchUserInformation = async (\n",
" _: { messages: Array<BaseMessage> },\n",
" _: { messages: BaseMessage[] },\n",
" config: RunnableConfig,\n",
") => {\n",
" const userDB = {\n",
Expand All @@ -204,7 +158,7 @@
" };\n",
" const userId = config?.configurable?.user;\n",
" if (userId) {\n",
" const user = userDB[userId];\n",
" const user = userDB[userId as keyof typeof userDB];\n",
" if (user) {\n",
" return {\n",
" userInfo:\n",
Expand All @@ -215,15 +169,14 @@
" return { userInfo: \"N/A\" };\n",
"};\n",
"\n",
"// Define the two nodes we will cycle between\n",
"workflow.addNode(\"fetchUserInfo\", fetchUserInformation);\n",
"workflow.addNode(\"agent\", callModel);\n",
"\n",
"// Set the entrypoint as `fetchUserInfo`\n",
"// so we can always start from there\n",
"workflow.addEdge(START, \"fetchUserInfo\");\n",
"workflow.addEdge(\"fetchUserInfo\", \"agent\");\n",
"workflow.addEdge(\"agent\", END);\n",
"const workflow = new StateGraph<IState>({\n",
" channels: graphState,\n",
"})\n",
" .addNode(\"fetchUserInfo\", fetchUserInformation)\n",
" .addNode(\"agent\", callModel)\n",
" .addEdge(START, \"fetchUserInfo\")\n",
" .addEdge(\"fetchUserInfo\", \"agent\")\n",
" .addEdge(\"agent\", END);\n",
"\n",
"// Here we only save in-memory\n",
"let memory = new MemorySaver();\n",
Expand All @@ -232,6 +185,7 @@
},
{
"cell_type": "markdown",
"id": "9ae55d0e",
"metadata": {},
"source": [
"## Call with config\n"
Expand All @@ -240,6 +194,7 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "ca608969",
"metadata": {},
"outputs": [
{
Expand All @@ -251,14 +206,8 @@
"\n",
"Could you remind me of my email??\n",
"-----\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sure, John! Your email is [email protected]. How can I assist you further?\n",
"\n",
"Sure, John! Your email address is [email protected].\n",
"-----\n",
"\n"
]
Expand Down Expand Up @@ -294,6 +243,7 @@
},
{
"cell_type": "markdown",
"id": "1afdf011",
"metadata": {},
"source": [
"## Change the config\n",
Expand All @@ -304,6 +254,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "e568e8e3",
"metadata": {},
"outputs": [
{
Expand All @@ -316,7 +267,7 @@
"Could you remind me of my email??\n",
"-----\n",
"\n",
"Of course, Jane! Your email is [email protected]. If you need any further assistance, feel free to ask!\n",
"Of course, Jane! Your email address is [email protected].\n",
"-----\n",
"\n"
]
Expand Down Expand Up @@ -352,6 +303,7 @@
},
{
"cell_type": "markdown",
"id": "f000b97c",
"metadata": {},
"source": [
"Check out the\n",
Expand All @@ -361,25 +313,32 @@
},
{
"cell_type": "markdown",
"id": "d55f98e1",
"metadata": {},
"source": []
"source": [
"```\n",
"```"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Deno",
"display_name": "TypeScript",
"language": "typescript",
"name": "deno"
"name": "tslab"
},
"language_info": {
"codemirror_mode": {
"mode": "typescript",
"name": "javascript",
"typescript": true
},
"file_extension": ".ts",
"mimetype": "text/x.typescript",
"mimetype": "text/typescript",
"name": "typescript",
"nb_converter": "script",
"pygments_lexer": "typescript",
"version": "5.4.5"
"version": "3.7.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 5
}
Loading

0 comments on commit 963db63

Please sign in to comment.