-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples[minor]: Port langchai examples code to run ts files in examples
- Loading branch information
1 parent
002e68d
commit e0b4c03
Showing
6 changed files
with
1,746 additions
and
0 deletions.
There are no files selected for viewing
1,302 changes: 1,302 additions & 0 deletions
1,302
examples/how-tos/streaming-events-from-within-tools.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
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,56 @@ | ||
import path from "path"; | ||
import url from "url"; | ||
import { awaitAllCallbacks } from "@langchain/core/callbacks/promises"; | ||
|
||
const [exampleName, ...args] = process.argv.slice(2); | ||
|
||
if (!exampleName) { | ||
console.error("Please provide path to example to run"); | ||
process.exit(1); | ||
} | ||
|
||
// Allow people to pass all possible variations of a path to an example | ||
// ./src/foo.ts, ./dist/foo.js, src/foo.ts, dist/foo.js, foo.ts | ||
let exampleRelativePath = exampleName; | ||
|
||
if (exampleRelativePath.startsWith("./examples/")) { | ||
exampleRelativePath = exampleName.slice(11); | ||
} else if (exampleRelativePath.startsWith("examples/")) { | ||
exampleRelativePath = exampleName.slice(9); | ||
} | ||
|
||
if (exampleRelativePath.startsWith("./src/")) { | ||
exampleRelativePath = exampleRelativePath.slice(6); | ||
} else if (exampleRelativePath.startsWith("./dist/")) { | ||
exampleRelativePath = exampleRelativePath.slice(7); | ||
} else if (exampleRelativePath.startsWith("src/")) { | ||
exampleRelativePath = exampleRelativePath.slice(4); | ||
} else if (exampleRelativePath.startsWith("dist/")) { | ||
exampleRelativePath = exampleRelativePath.slice(5); | ||
} | ||
|
||
let runExample; | ||
try { | ||
({ run: runExample } = await import( | ||
path.join( | ||
path.dirname(url.fileURLToPath(import.meta.url)), | ||
exampleRelativePath | ||
) | ||
)); | ||
} catch (e) { | ||
console.log(e); | ||
throw new Error(`Could not load example ${exampleName}: ${e}`); | ||
} | ||
|
||
if (runExample) { | ||
const maybePromise = runExample(args); | ||
|
||
if (maybePromise instanceof Promise) { | ||
maybePromise | ||
.catch((e) => { | ||
console.error(`Example failed with:`); | ||
console.error(e); | ||
}) | ||
.finally(() => awaitAllCallbacks()); | ||
} | ||
} |
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,75 @@ | ||
import { z } from "zod"; | ||
import { tool } from "@langchain/core/tools"; | ||
import { ChatPromptTemplate } from "@langchain/core/prompts"; | ||
import { ChatAnthropic } from "@langchain/anthropic"; | ||
import { createReactAgent } from "@langchain/langgraph/prebuilt"; | ||
|
||
const model = new ChatAnthropic({ | ||
model: "claude-3-5-sonnet-20240620", | ||
temperature: 0, | ||
}); | ||
|
||
const getItems = tool( | ||
async (input, config) => { | ||
console.log("RUNNING TOOL"); | ||
const template = ChatPromptTemplate.fromMessages([ | ||
[ | ||
"human", | ||
"Can you tell me what kind of items i might find in the following place: '{place}'. " + | ||
"List at least 3 such items separating them by a comma. And include a brief description of each item..", | ||
], | ||
]); | ||
|
||
const modelWithConfig = model.withConfig({ | ||
runName: "Get Items LLM", | ||
tags: ["tool_llm"], | ||
}); | ||
|
||
const chain = template.pipe(modelWithConfig); | ||
const result = await chain.invoke(input, config); | ||
return result.content; | ||
}, | ||
{ | ||
name: "get_items", | ||
description: "Use this tool to look up which items are in the given place.", | ||
schema: z.object({ | ||
place: z.string(), | ||
}), | ||
} | ||
); | ||
|
||
const agent = createReactAgent({ | ||
llm: model, | ||
tools: [getItems], | ||
}); | ||
|
||
let finalEvent; | ||
for await (const event of agent.streamEvents( | ||
{ | ||
messages: [ | ||
[ | ||
"human", | ||
"what items are on the shelf? You should call the get_items tool.", | ||
], | ||
], | ||
}, | ||
{ | ||
version: "v2", | ||
}, | ||
{ | ||
includeTags: ["tool_llm"], | ||
} | ||
)) { | ||
console.log(event.data); | ||
finalEvent = event; | ||
} | ||
|
||
const finalMessages = finalEvent?.data.output.messages; | ||
console.dir( | ||
finalMessages.map((msg: any) => ({ | ||
type: msg._getType(), | ||
content: msg.content, | ||
tool_calls: msg.tool_calls, | ||
})), | ||
{ depth: null } | ||
); |
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,31 @@ | ||
{ | ||
"extends": "@tsconfig/recommended", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"lib": [ | ||
"ES2021", | ||
"ES2022.Object", | ||
"DOM" | ||
], | ||
"target": "ES2021", | ||
"module": "nodenext", | ||
"sourceMap": true, | ||
"allowSyntheticDefaultImports": true, | ||
"baseUrl": "./src", | ||
"declaration": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true, | ||
"noUnusedParameters": true, | ||
"useDefineForClassFields": true, | ||
"strictPropertyInitialization": false | ||
}, | ||
"exclude": [ | ||
"node_modules/", | ||
"dist/", | ||
"tests/" | ||
], | ||
"include": [ | ||
"*.ts", | ||
"./src" | ||
] | ||
} |
Oops, something went wrong.