Skip to content

Commit

Permalink
toolkit docs (langchain-ai#174)
Browse files Browse the repository at this point in the history
* toolkit docs

* Add sidebar label

---------

Co-authored-by: Nuno Campos <[email protected]>
  • Loading branch information
hwchase17 and nfcampos authored Mar 1, 2023
1 parent 2b205d3 commit 5f544cb
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/docs/modules/agents/agent_toolkits/_category_.yml
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
45 changes: 45 additions & 0 deletions docs/docs/modules/agents/agent_toolkits/json.md
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
)}`
);
};
```
47 changes: 47 additions & 0 deletions docs/docs/modules/agents/agent_toolkits/openapi.md
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
)}`
);
};
```
39 changes: 39 additions & 0 deletions docs/docs/modules/agents/agent_toolkits/sql.md
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();
};
```
4 changes: 4 additions & 0 deletions docs/docs/modules/agents/overview.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
sidebar_position: 1
---

# Agent Overview

Agents use an LLM to determine which actions to take and in what order. An action can either be using a tool and observing its output, or returning to the user.
Expand Down

0 comments on commit 5f544cb

Please sign in to comment.