Skip to content

Commit 7b9b67d

Browse files
authored
Merge pull request langchain-ai#890 from hwchase17/nc/struct-tools
Nc/struct tools and nc/autogpt
2 parents b45313c + f9af254 commit 7b9b67d

File tree

35 files changed

+985
-16
lines changed

35 files changed

+985
-16
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { AutoGPT } from "langchain/experimental/autogpt";
2+
import { ReadFileTool, WriteFileTool, SerpAPI } from "langchain/tools";
3+
import { NodeFileStore } from "langchain/stores/file/node";
4+
import { HNSWLib } from "langchain/vectorstores/hnswlib";
5+
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
6+
import { ChatOpenAI } from "langchain/chat_models/openai";
7+
8+
const store = new NodeFileStore();
9+
10+
const tools = [
11+
new ReadFileTool({ store }),
12+
new WriteFileTool({ store }),
13+
new SerpAPI(process.env.SERPAPI_API_KEY, {
14+
location: "San Francisco,California,United States",
15+
hl: "en",
16+
gl: "us",
17+
}),
18+
];
19+
20+
const vectorStore = new HNSWLib(new OpenAIEmbeddings(), {
21+
space: "cosine",
22+
numDimensions: 1536,
23+
});
24+
25+
const autogpt = AutoGPT.fromLLMAndTools(
26+
new ChatOpenAI({ temperature: 0 }),
27+
tools,
28+
{
29+
memory: vectorStore.asRetriever(),
30+
aiName: "Tom",
31+
aiRole: "Assistant",
32+
}
33+
);
34+
35+
await autogpt.run(["write a weather report for SF today"]);
36+
/*
37+
{
38+
"thoughts": {
39+
"text": "I need to write a weather report for SF today. I should use a search engine to find the current weather conditions.",
40+
"reasoning": "I don't have the current weather information for SF in my short term memory, so I need to use a search engine to find it.",
41+
"plan": "- Use the search command to find the current weather conditions for SF\n- Write a weather report based on the information found",
42+
"criticism": "I need to make sure that the information I find is accurate and up-to-date.",
43+
"speak": "I will use the search command to find the current weather conditions for SF."
44+
},
45+
"command": {
46+
"name": "search",
47+
"args": {
48+
"input": "current weather conditions San Francisco"
49+
}
50+
}
51+
}
52+
{
53+
"thoughts": {
54+
"text": "I have found the current weather conditions for SF. I need to write a weather report based on this information.",
55+
"reasoning": "I have the information I need to write a weather report, so I should use the write_file command to save it to a file.",
56+
"plan": "- Use the write_file command to save the weather report to a file",
57+
"criticism": "I need to make sure that the weather report is clear and concise.",
58+
"speak": "I will use the write_file command to save the weather report to a file."
59+
},
60+
"command": {
61+
"name": "write_file",
62+
"args": {
63+
"file_path": "weather_report.txt",
64+
"text": "San Francisco Weather Report:\n\nMorning: 53°, Chance of Rain 1%\nAfternoon: 59°, Chance of Rain 0%\nEvening: 52°, Chance of Rain 3%\nOvernight: 48°, Chance of Rain 2%"
65+
}
66+
}
67+
}
68+
{
69+
"thoughts": {
70+
"text": "I have completed all my objectives. I will use the finish command to signal that I am done.",
71+
"reasoning": "I have completed the task of writing a weather report for SF today, so I don't need to do anything else.",
72+
"plan": "- Use the finish command to signal that I am done",
73+
"criticism": "I need to make sure that I have completed all my objectives before using the finish command.",
74+
"speak": "I will use the finish command to signal that I am done."
75+
},
76+
"command": {
77+
"name": "finish",
78+
"args": {
79+
"response": "I have completed all my objectives."
80+
}
81+
}
82+
}
83+
*/
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { AutoGPT } from "langchain/experimental/autogpt";
2+
import { ReadFileTool, WriteFileTool, SerpAPI } from "langchain/tools";
3+
import { InMemoryFileStore } from "langchain/stores/file/in_memory";
4+
import { MemoryVectorStore } from "langchain/vectorstores/memory";
5+
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
6+
import { ChatOpenAI } from "langchain/chat_models/openai";
7+
8+
const store = new InMemoryFileStore();
9+
10+
const tools = [
11+
new ReadFileTool({ store }),
12+
new WriteFileTool({ store }),
13+
new SerpAPI(process.env.SERPAPI_API_KEY, {
14+
location: "San Francisco,California,United States",
15+
hl: "en",
16+
gl: "us",
17+
}),
18+
];
19+
20+
const vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());
21+
22+
const autogpt = AutoGPT.fromLLMAndTools(
23+
new ChatOpenAI({ temperature: 0 }),
24+
tools,
25+
{
26+
memory: vectorStore.asRetriever(),
27+
aiName: "Tom",
28+
aiRole: "Assistant",
29+
}
30+
);
31+
32+
await autogpt.run(["write a weather report for SF today"]);
33+
/*
34+
{
35+
"thoughts": {
36+
"text": "I need to write a weather report for SF today. I should use a search engine to find the current weather conditions.",
37+
"reasoning": "I don't have the current weather information for SF in my short term memory, so I need to use a search engine to find it.",
38+
"plan": "- Use the search command to find the current weather conditions for SF\n- Write a weather report based on the information found",
39+
"criticism": "I need to make sure that the information I find is accurate and up-to-date.",
40+
"speak": "I will use the search command to find the current weather conditions for SF."
41+
},
42+
"command": {
43+
"name": "search",
44+
"args": {
45+
"input": "current weather conditions San Francisco"
46+
}
47+
}
48+
}
49+
{
50+
"thoughts": {
51+
"text": "I have found the current weather conditions for SF. I need to write a weather report based on this information.",
52+
"reasoning": "I have the information I need to write a weather report, so I should use the write_file command to save it to a file.",
53+
"plan": "- Use the write_file command to save the weather report to a file",
54+
"criticism": "I need to make sure that the weather report is clear and concise.",
55+
"speak": "I will use the write_file command to save the weather report to a file."
56+
},
57+
"command": {
58+
"name": "write_file",
59+
"args": {
60+
"file_path": "weather_report.txt",
61+
"text": "San Francisco Weather Report:\n\nMorning: 53°, Chance of Rain 1%\nAfternoon: 59°, Chance of Rain 0%\nEvening: 52°, Chance of Rain 3%\nOvernight: 48°, Chance of Rain 2%"
62+
}
63+
}
64+
}
65+
{
66+
"thoughts": {
67+
"text": "I have completed all my objectives. I will use the finish command to signal that I am done.",
68+
"reasoning": "I have completed the task of writing a weather report for SF today, so I don't need to do anything else.",
69+
"plan": "- Use the finish command to signal that I am done",
70+
"criticism": "I need to make sure that I have completed all my objectives before using the finish command.",
71+
"speak": "I will use the finish command to signal that I am done."
72+
},
73+
"command": {
74+
"name": "finish",
75+
"args": {
76+
"response": "I have completed all my objectives."
77+
}
78+
}
79+
}
80+
*/

langchain/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,15 @@ cache.d.ts
226226
cache/redis.cjs
227227
cache/redis.js
228228
cache/redis.d.ts
229+
stores/file/in_memory.cjs
230+
stores/file/in_memory.js
231+
stores/file/in_memory.d.ts
232+
stores/file/node.cjs
233+
stores/file/node.js
234+
stores/file/node.d.ts
235+
experimental/autogpt.cjs
236+
experimental/autogpt.js
237+
experimental/autogpt.d.ts
229238
index.cjs
230239
index.js
231240
index.d.ts

langchain/package.json

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@
238238
"cache/redis.cjs",
239239
"cache/redis.js",
240240
"cache/redis.d.ts",
241+
"stores/file/in_memory.cjs",
242+
"stores/file/in_memory.js",
243+
"stores/file/in_memory.d.ts",
244+
"stores/file/node.cjs",
245+
"stores/file/node.js",
246+
"stores/file/node.d.ts",
247+
"experimental/autogpt.cjs",
248+
"experimental/autogpt.js",
249+
"experimental/autogpt.d.ts",
241250
"index.cjs",
242251
"index.js",
243252
"index.d.ts"
@@ -444,7 +453,8 @@
444453
"p-retry": "4",
445454
"uuid": "^9.0.0",
446455
"yaml": "^2.2.1",
447-
"zod": "^3.21.4"
456+
"zod": "^3.21.4",
457+
"zod-to-json-schema": "^3.20.4"
448458
},
449459
"publishConfig": {
450460
"access": "public"
@@ -861,6 +871,21 @@
861871
"import": "./cache/redis.js",
862872
"require": "./cache/redis.cjs"
863873
},
874+
"./stores/file/in_memory": {
875+
"types": "./stores/file/in_memory.d.ts",
876+
"import": "./stores/file/in_memory.js",
877+
"require": "./stores/file/in_memory.cjs"
878+
},
879+
"./stores/file/node": {
880+
"types": "./stores/file/node.d.ts",
881+
"import": "./stores/file/node.js",
882+
"require": "./stores/file/node.cjs"
883+
},
884+
"./experimental/autogpt": {
885+
"types": "./experimental/autogpt.d.ts",
886+
"import": "./experimental/autogpt.js",
887+
"require": "./experimental/autogpt.cjs"
888+
},
864889
"./package.json": "./package.json"
865890
}
866891
}

langchain/scripts/create-entrypoints.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ const entrypoints = {
104104
// cache
105105
cache: "cache/index",
106106
"cache/redis": "cache/redis",
107+
// stores
108+
"stores/file/in_memory": "stores/file/in_memory",
109+
"stores/file/node": "stores/file/node",
110+
// experimental
111+
"experimental/autogpt": "experimental/autogpt/index",
107112
};
108113

109114
// Entrypoints in this list will
@@ -164,6 +169,7 @@ const requiresOptionalDependency = [
164169
"retrievers/supabase",
165170
"retrievers/metal",
166171
"cache/redis",
172+
"stores/file/node",
167173
];
168174

169175
// List of test-exports-* packages which we use to test that the exports field

0 commit comments

Comments
 (0)