Skip to content

Commit

Permalink
docs[minor]: Update init syntax docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bracesproul committed Aug 19, 2024
1 parent fd6233e commit 725d008
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 122 deletions.
199 changes: 103 additions & 96 deletions examples/agent_executor/base.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"id": "4499eb16-bca8-4a60-9a3a-2f34ae3f7078",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -133,23 +133,22 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"id": "c941fb10-dbe5-4d6a-ab7d-133d01c33cc4",
"metadata": {},
"outputs": [],
"source": [
"const agentState = {\n",
" input: {\n",
" value: null,\n",
" },\n",
" steps: {\n",
" value: (x, y) => x.concat(y),\n",
"import { Annotation } from \"@langchain/langgraph\";\n",
"import { AgentAction, AgentFinish, AgentStep } from \"@langchain/core/agents\";\n",
"\n",
"const AgentState = Annotation.Root({\n",
" input: Annotation<string>,\n",
" steps: Annotation<AgentStep[]>({\n",
" reducer: (x, y) => x.concat(y),\n",
" default: () => [],\n",
" },\n",
" agentOutcome: {\n",
" value: null,\n",
" },\n",
"};"
" }),\n",
" agentOutcome: Annotation<AgentAction | AgentFinish>,\n",
"});"
]
},
{
Expand Down Expand Up @@ -185,13 +184,12 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 13,
"id": "d61a970d-edf4-4eef-9678-28bab7c72331",
"metadata": {},
"outputs": [],
"source": [
"import { BaseMessage } from \"@langchain/core/messages\";\n",
"import { AgentAction, AgentFinish, AgentStep } from \"@langchain/core/agents\";\n",
"import { ToolExecutor } from \"@langchain/langgraph/prebuilt\";\n",
"import type { RunnableConfig } from \"@langchain/core/runnables\";\n",
"\n",
Expand Down Expand Up @@ -248,7 +246,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 14,
"id": "c4054dde-4618-49b7-998a-daa0c1d6d6c0",
"metadata": {},
"outputs": [],
Expand All @@ -257,49 +255,43 @@
"import { END, START, StateGraph } from \"@langchain/langgraph\";\n",
"\n",
"// Define a new graph\n",
"const workflow = new StateGraph({\n",
" channels: agentState,\n",
"});\n",
"\n",
"// Define the two nodes we will cycle between\n",
"workflow.addNode(\"agent\", new RunnableLambda({ func: runAgent }));\n",
"workflow.addNode(\"action\", new RunnableLambda({ func: executeTools }));\n",
"\n",
"// Set the entrypoint as `agent`\n",
"// This means that this node is the first one called\n",
"workflow.addEdge(START, \"agent\");\n",
"\n",
"// We now add a conditional edge\n",
"workflow.addConditionalEdges(\n",
" // First, we define the start node. We use `agent`.\n",
" // This means these are the edges taken after the `agent` node is called.\n",
" \"agent\",\n",
" // Next, we pass in the function that will determine which node is called next.\n",
" shouldContinue,\n",
" // Finally we pass in a mapping.\n",
" // The keys are strings, and the values are other nodes.\n",
" // END is a special node marking that the graph should finish.\n",
" // What will happen is we will call `should_continue`, and then the output of that\n",
" // will be matched against the keys in this mapping.\n",
" // Based on which one it matches, that node will then be called.\n",
" {\n",
" // If `tools`, then we call the tool node.\n",
" continue: \"action\",\n",
" // Otherwise we finish.\n",
" end: END,\n",
" },\n",
");\n",
"\n",
"// We now add a normal edge from `tools` to `agent`.\n",
"// This means that after `tools` is called, `agent` node is called next.\n",
"workflow.addEdge(\"action\", \"agent\");\n",
"const workflow = new StateGraph(AgentState)\n",
" // Define the two nodes we will cycle between\n",
" .addNode(\"agent\", new RunnableLambda({ func: runAgent }))\n",
" .addNode(\"action\", new RunnableLambda({ func: executeTools }))\n",
" // Set the entrypoint as `agent`\n",
" // This means that this node is the first one called\n",
" .addEdge(START, \"agent\")\n",
" // We now add a conditional edge\n",
" .addConditionalEdges(\n",
" // First, we define the start node. We use `agent`.\n",
" // This means these are the edges taken after the `agent` node is called.\n",
" \"agent\",\n",
" // Next, we pass in the function that will determine which node is called next.\n",
" shouldContinue,\n",
" // Finally we pass in a mapping.\n",
" // The keys are strings, and the values are other nodes.\n",
" // END is a special node marking that the graph should finish.\n",
" // What will happen is we will call `should_continue`, and then the output of that\n",
" // will be matched against the keys in this mapping.\n",
" // Based on which one it matches, that node will then be called.\n",
" {\n",
" // If `tools`, then we call the tool node.\n",
" continue: \"action\",\n",
" // Otherwise we finish.\n",
" end: END,\n",
" },\n",
" )\n",
" // We now add a normal edge from `tools` to `agent`.\n",
" // This means that after `tools` is called, `agent` node is called next.\n",
" .addEdge(\"action\", \"agent\");\n",
"\n",
"const app = workflow.compile();"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 19,
"id": "214ae46e-c297-465d-86db-2b0312ed3530",
"metadata": {},
"outputs": [
Expand All @@ -310,17 +302,33 @@
"{\n",
" agent: {\n",
" agentOutcome: {\n",
" tool: \"tavily_search_results_json\",\n",
" toolInput: { input: \"current weather in San Francisco\" },\n",
" tool: 'tavily_search_results_json',\n",
" toolInput: { input: 'current weather in San Francisco' },\n",
" log: 'Invoking \"tavily_search_results_json\" with {\"input\":\"current weather in San Francisco\"}\\n',\n",
" messageLog: [\n",
" AIMessage {\n",
" lc_serializable: true,\n",
" lc_kwargs: [Object],\n",
" lc_namespace: [Array],\n",
" content: \"\",\n",
" lc_kwargs: {\n",
" content: '',\n",
" tool_calls: [],\n",
" invalid_tool_calls: [],\n",
" additional_kwargs: [Object],\n",
" response_metadata: [Object],\n",
" id: 'chatcmpl-9y1NwDZx83JESI1fojFvUue4sk5YS'\n",
" },\n",
" lc_namespace: [ 'langchain_core', 'messages' ],\n",
" content: '',\n",
" name: undefined,\n",
" additional_kwargs: [Object]\n",
" additional_kwargs: { function_call: [Object], tool_calls: undefined },\n",
" response_metadata: {\n",
" tokenUsage: [Object],\n",
" finish_reason: 'function_call',\n",
" system_fingerprint: 'fp_3aa7262c27'\n",
" },\n",
" id: 'chatcmpl-9y1NwDZx83JESI1fojFvUue4sk5YS',\n",
" tool_calls: [],\n",
" invalid_tool_calls: [],\n",
" usage_metadata: { input_tokens: 84, output_tokens: 22, total_tokens: 106 }\n",
" }\n",
" ]\n",
" }\n",
Expand All @@ -333,12 +341,12 @@
" steps: [\n",
" {\n",
" action: {\n",
" tool: \"tavily_search_results_json\",\n",
" toolInput: [Object],\n",
" tool: 'tavily_search_results_json',\n",
" toolInput: { input: 'current weather in San Francisco' },\n",
" log: 'Invoking \"tavily_search_results_json\" with {\"input\":\"current weather in San Francisco\"}\\n',\n",
" messageLog: [Array]\n",
" messageLog: [ [AIMessage] ]\n",
" },\n",
" observation: '\"[{\\\\\"title\\\\\":\\\\\"Weather in San Francisco\\\\\",\\\\\"url\\\\\":\\\\\"https://www.weatherapi.com/\\\\\",\\\\\"content\\\\\":\\\\\"Weat'... 839 more characters\n",
" observation: `\"[{\\\\\"title\\\\\":\\\\\"Weather in San Francisco\\\\\",\\\\\"url\\\\\":\\\\\"https://www.weatherapi.com/\\\\\",\\\\\"content\\\\\":\\\\\"{'location': {'name': 'San Francisco', 'region': 'California', 'country': 'United States of America', 'lat': 37.78, 'lon': -122.42, 'tz_id': 'America/Los_Angeles', 'localtime_epoch': 1724091818, 'localtime': '2024-08-19 11:23'}, 'current': {'last_updated_epoch': 1724091300, 'last_updated': '2024-08-19 11:15', 'temp_c': 18.3, 'temp_f': 64.9, 'is_day': 1, 'condition': {'text': 'Partly cloudy', 'icon': '//cdn.weatherapi.com/weather/64x64/day/116.png', 'code': 1003}, 'wind_mph': 6.9, 'wind_kph': 11.2, 'wind_degree': 240, 'wind_dir': 'WSW', 'pressure_mb': 1019.0, 'pressure_in': 30.09, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 78, 'cloud': 25, 'feelslike_c': 18.3, 'feelslike_f': 64.9, 'windchill_c': 16.5, 'windchill_f': 61.7, 'heatindex_c': 16.5, 'heatindex_f': 61.8, 'dewpoint_c': 11.0, 'dewpoint_f': 51.8, 'vis_km': 16.0, 'vis_miles': 9.0, 'uv': 5.0, 'gust_mph': 7.3, 'gust_kph': 11.7}}\\\\\",\\\\\"score\\\\\":0.9972114,\\\\\"raw_content\\\\\":null}]\"`\n",
" }\n",
" ]\n",
" }\n",
Expand All @@ -349,33 +357,29 @@
" agent: {\n",
" agentOutcome: {\n",
" returnValues: {\n",
" output: \"The current weather in San Francisco is partly cloudy with a temperature of 11.7°C (53.1°F). The win\"... 278 more characters\n",
" },\n",
" log: \"The current weather in San Francisco is partly cloudy with a temperature of 11.7°C (53.1°F). The win\"... 278 more characters\n",
" }\n",
" }\n",
"}\n",
"----\n",
"\n",
"{\n",
" __end__: {\n",
" input: \"what is the weather in sf\",\n",
" steps: [\n",
" {\n",
" action: {\n",
" tool: \"tavily_search_results_json\",\n",
" toolInput: [Object],\n",
" log: 'Invoking \"tavily_search_results_json\" with {\"input\":\"current weather in San Francisco\"}\\n',\n",
" messageLog: [Array]\n",
" },\n",
" observation: '\"[{\\\\\"title\\\\\":\\\\\"Weather in San Francisco\\\\\",\\\\\"url\\\\\":\\\\\"https://www.weatherapi.com/\\\\\",\\\\\"content\\\\\":\\\\\"Weat'... 839 more characters\n",
" }\n",
" ],\n",
" agentOutcome: {\n",
" returnValues: {\n",
" output: \"The current weather in San Francisco is partly cloudy with a temperature of 11.7°C (53.1°F). The win\"... 278 more characters\n",
" output: 'The current weather in San Francisco is as follows:\\n' +\n",
" '\\n' +\n",
" '- **Temperature:** 18.3°C (64.9°F)\\n' +\n",
" '- **Condition:** Partly cloudy\\n' +\n",
" '- **Wind:** 6.9 mph (11.2 kph) from the WSW\\n' +\n",
" '- **Humidity:** 78%\\n' +\n",
" '- **Visibility:** 16 km (9 miles)\\n' +\n",
" '- **Pressure:** 1019.0 mb (30.09 in)\\n' +\n",
" '- **UV Index:** 5\\n' +\n",
" '\\n' +\n",
" '![Weather Icon](//cdn.weatherapi.com/weather/64x64/day/116.png)'\n",
" },\n",
" log: \"The current weather in San Francisco is partly cloudy with a temperature of 11.7°C (53.1°F). The win\"... 278 more characters\n",
" log: 'The current weather in San Francisco is as follows:\\n' +\n",
" '\\n' +\n",
" '- **Temperature:** 18.3°C (64.9°F)\\n' +\n",
" '- **Condition:** Partly cloudy\\n' +\n",
" '- **Wind:** 6.9 mph (11.2 kph) from the WSW\\n' +\n",
" '- **Humidity:** 78%\\n' +\n",
" '- **Visibility:** 16 km (9 miles)\\n' +\n",
" '- **Pressure:** 1019.0 mb (30.09 in)\\n' +\n",
" '- **UV Index:** 5\\n' +\n",
" '\\n' +\n",
" '![Weather Icon](//cdn.weatherapi.com/weather/64x64/day/116.png)'\n",
" }\n",
" }\n",
"}\n",
Expand All @@ -387,25 +391,28 @@
"source": [
"const inputs = { input: \"what is the weather in sf\" };\n",
"for await (const s of await app.stream(inputs)) {\n",
" console.log(s);\n",
" console.dir(s, { depth: 5 });\n",
" console.log(\"----\\n\");\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.3.3"
"version": "3.7.2"
}
},
"nbformat": 4,
Expand Down
5 changes: 4 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"@langchain/anthropic": "^0.2.15",
"@langchain/cloudflare": "^0.0.7",
"@langchain/community": "^0.2.27",
"@langchain/core": "^0.2.24",
"@langchain/core": "^0.2.27",
"@langchain/groq": "^0.0.16",
"@langchain/langgraph": "workspace:*",
"@langchain/mistralai": "^0.0.28",
Expand All @@ -20,5 +20,8 @@
"uuid": "^10.0.0",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.2"
},
"resolutions": {
"@langchain/core": "0.2.27"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"resolutions": {
"cheerio": "^1.0.0-rc.12",
"typescript": "4.9.5",
"semver": "^7.0.0"
"semver": "^7.0.0",
"@langchain/core": "latest"
},
"publishConfig": {
"access": "public",
Expand Down
29 changes: 5 additions & 24 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1840,28 +1840,9 @@ __metadata:
languageName: node
linkType: hard

"@langchain/core@npm:>0.1.0 <0.3.0, @langchain/core@npm:>=0.2.11 <0.3.0, @langchain/core@npm:>=0.2.16 <0.3.0, @langchain/core@npm:>=0.2.20 <0.3.0, @langchain/core@npm:>=0.2.21 <0.3.0":
version: 0.2.21
resolution: "@langchain/core@npm:0.2.21"
dependencies:
ansi-styles: ^5.0.0
camelcase: 6
decamelize: 1.2.0
js-tiktoken: ^1.0.12
langsmith: ~0.1.39
mustache: ^4.2.0
p-queue: ^6.6.2
p-retry: 4
uuid: ^10.0.0
zod: ^3.22.4
zod-to-json-schema: ^3.22.3
checksum: 661e40388156820a05a342db5b3bb65eb27dc2d44306b0946383d578857b96900c2c8a0975463fa3576e6c16dc034a83aa9288b9472f3f0ae0e92968033214ce
languageName: node
linkType: hard

"@langchain/core@npm:>0.1.0 <0.3.0, @langchain/core@npm:^0.2.24":
version: 0.2.24
resolution: "@langchain/core@npm:0.2.24"
"@langchain/core@npm:latest":
version: 0.2.27
resolution: "@langchain/core@npm:0.2.27"
dependencies:
ansi-styles: ^5.0.0
camelcase: 6
Expand All @@ -1874,7 +1855,7 @@ __metadata:
uuid: ^10.0.0
zod: ^3.22.4
zod-to-json-schema: ^3.22.3
checksum: 8f9dfa746750653a4fbfcdeca3d662c4461c085d97cfea7b68b310551e256c42496e5d364aabd10a641b53f96876ee0ffd072ee6ac844b0e89e21ad3dc4906b0
checksum: 188ab834a0a9f4462b81d38b92be61df91d87392b2bd86fb21d5cfe253c080700d09ffd954c90726704fc5fe6c0f06cb2f5bd7705f17632819473c9d8ed57b39
languageName: node
linkType: hard

Expand Down Expand Up @@ -6036,7 +6017,7 @@ __metadata:
"@langchain/anthropic": ^0.2.15
"@langchain/cloudflare": ^0.0.7
"@langchain/community": ^0.2.27
"@langchain/core": ^0.2.24
"@langchain/core": ^0.2.27
"@langchain/groq": ^0.0.16
"@langchain/langgraph": "workspace:*"
"@langchain/mistralai": ^0.0.28
Expand Down

0 comments on commit 725d008

Please sign in to comment.