forked from langchain-ai/langchainjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* toolkit docs * Add sidebar label --------- Co-authored-by: Nuno Campos <[email protected]>
- Loading branch information
Showing
5 changed files
with
138 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
label: "Agent Toolkits" | ||
collapsible: true # make the category collapsible | ||
collapsed: false # keep the category open by default |
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,45 @@ | ||
# JSON Agent Toolkit | ||
|
||
This example shows how to load and use an agent with a JSON toolkit. | ||
|
||
```typescript | ||
import { OpenAI } from "langchain"; | ||
import * as fs from "fs"; | ||
import * as yaml from "js-yaml"; | ||
import { JsonSpec, JsonObject } from "langchain/tools"; | ||
import { JsonToolkit, createJsonAgent } from "langchain/agents"; | ||
|
||
export const run = async () => { | ||
let data: JsonObject; | ||
try { | ||
const yamlFile = fs.readFileSync("openai_openapi.yaml", "utf8"); | ||
data = yaml.load(yamlFile) as JsonObject; | ||
if (!data) { | ||
throw new Error("Failed to load OpenAPI spec"); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
return; | ||
} | ||
|
||
const toolkit = new JsonToolkit(new JsonSpec(data)); | ||
const model = new OpenAI({ temperature: 0 }); | ||
const executor = createJsonAgent(model, toolkit); | ||
|
||
const input = `What are the required parameters in the request body to the /completions endpoint?`; | ||
|
||
console.log(`Executing with input "${input}"...`); | ||
|
||
const result = await executor.call({ input }); | ||
|
||
console.log(`Got output ${result.output}`); | ||
|
||
console.log( | ||
`Got intermediate steps ${JSON.stringify( | ||
result.intermediateSteps, | ||
null, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# OpenAPI Agent Toolkit | ||
|
||
This example shows how to load and use an agent with a OpenAPI toolkit. | ||
|
||
```typescript | ||
import * as fs from "fs"; | ||
import * as yaml from "js-yaml"; | ||
import { JsonSpec, JsonObject } from "langchain/tools"; | ||
import { createOpenApiAgent, OpenApiToolkit } from "langchain/agents"; | ||
import { OpenAI } from "langchain"; | ||
|
||
export const run = async () => { | ||
let data: JsonObject; | ||
try { | ||
const yamlFile = fs.readFileSync("openai_openapi.yaml", "utf8"); | ||
data = yaml.load(yamlFile) as JsonObject; | ||
if (!data) { | ||
throw new Error("Failed to load OpenAPI spec"); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
return; | ||
} | ||
|
||
const headers = { | ||
"Content-Type": "application/json", | ||
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`, | ||
}; | ||
const model = new OpenAI({ temperature: 0 }); | ||
const toolkit = new OpenApiToolkit(new JsonSpec(data), model, headers); | ||
const executor = createOpenApiAgent(model, toolkit); | ||
|
||
const input = `Make a POST request to openai /completions. The prompt should be 'tell me a joke.'`; | ||
console.log(`Executing with input "${input}"...`); | ||
|
||
const result = await executor.call({ input }); | ||
console.log(`Got output ${result.output}`); | ||
|
||
console.log( | ||
`Got intermediate steps ${JSON.stringify( | ||
result.intermediateSteps, | ||
null, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# SQL Agent Toolkit | ||
|
||
This example shows how to load and use an agent with a SQL toolkit. | ||
|
||
```typescript | ||
import { OpenAI } from "langchain"; | ||
import { SqlDatabase } from "langchain/tools"; | ||
import { createSqlAgent, SqlToolkit } from "langchain/agents"; | ||
import sqlite3 from "sqlite3"; | ||
|
||
/** This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc. | ||
* To set it up follow the instructions on https://database.guide/2-sample-databases-sqlite/, placing the .db file | ||
* in the examples folder. | ||
*/ | ||
export const run = async () => { | ||
const db = await new sqlite3.Database("Chinook.db"); | ||
const tookit = new SqlToolkit(new SqlDatabase(db)); | ||
const model = new OpenAI({ temperature: 0 }); | ||
const executor = createSqlAgent(model, tookit); | ||
|
||
const input = `List the total sales per country. Which country's customers spent the most?`; | ||
|
||
console.log(`Executing with input "${input}"...`); | ||
|
||
const result = await executor.call({ input }); | ||
|
||
console.log(`Got output ${result.output}`); | ||
|
||
console.log( | ||
`Got intermediate steps ${JSON.stringify( | ||
result.intermediateSteps, | ||
null, | ||
2 | ||
)}` | ||
); | ||
|
||
db.close(); | ||
}; | ||
``` |
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