Skip to content

Commit

Permalink
Implement autogpt
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos committed Apr 19, 2023
1 parent 60518f4 commit 4a0c691
Show file tree
Hide file tree
Showing 35 changed files with 971 additions and 16 deletions.
6 changes: 6 additions & 0 deletions examples/langchain-g25uXC/weather_report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
San Francisco Weather Report:

Morning: 53°, Chance of Rain 1%
Afternoon: 59°, Chance of Rain 0%
Evening: 52°, Chance of Rain 3%
Overnight: 48°, Chance of Rain 2%
83 changes: 83 additions & 0 deletions examples/src/experimental/autogpt/weather.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { AutoGPT } from "langchain/experimental/autogpt";
import { ReadFileTool, WriteFileTool, SerpAPI } from "langchain/tools";
import { NodeFileStore } from "langchain/stores/file/node";
import { HNSWLib } from "langchain/vectorstores/hnswlib";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { ChatOpenAI } from "langchain/chat_models/openai";

const store = new NodeFileStore();

const tools = [
new ReadFileTool({ store }),
new WriteFileTool({ store }),
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "San Francisco,California,United States",
hl: "en",
gl: "us",
}),
];

const vectorStore = new HNSWLib(new OpenAIEmbeddings(), {
space: "cosine",
numDimensions: 1536,
});

const autogpt = AutoGPT.fromLLMAndTools(
new ChatOpenAI({ temperature: 0 }),
tools,
{
memory: vectorStore.asRetriever(),
aiName: "Tom",
aiRole: "Assistant",
}
);

await autogpt.run(["write a weather report for SF today"]);
/*
{
"thoughts": {
"text": "I need to write a weather report for SF today. I should use a search engine to find the current weather conditions.",
"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.",
"plan": "- Use the search command to find the current weather conditions for SF\n- Write a weather report based on the information found",
"criticism": "I need to make sure that the information I find is accurate and up-to-date.",
"speak": "I will use the search command to find the current weather conditions for SF."
},
"command": {
"name": "search",
"args": {
"input": "current weather conditions San Francisco"
}
}
}
{
"thoughts": {
"text": "I have found the current weather conditions for SF. I need to write a weather report based on this information.",
"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.",
"plan": "- Use the write_file command to save the weather report to a file",
"criticism": "I need to make sure that the weather report is clear and concise.",
"speak": "I will use the write_file command to save the weather report to a file."
},
"command": {
"name": "write_file",
"args": {
"file_path": "weather_report.txt",
"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%"
}
}
}
{
"thoughts": {
"text": "I have completed all my objectives. I will use the finish command to signal that I am done.",
"reasoning": "I have completed the task of writing a weather report for SF today, so I don't need to do anything else.",
"plan": "- Use the finish command to signal that I am done",
"criticism": "I need to make sure that I have completed all my objectives before using the finish command.",
"speak": "I will use the finish command to signal that I am done."
},
"command": {
"name": "finish",
"args": {
"response": "I have completed all my objectives."
}
}
}
*/
80 changes: 80 additions & 0 deletions examples/src/experimental/autogpt/weather_browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { AutoGPT } from "langchain/experimental/autogpt";
import { ReadFileTool, WriteFileTool, SerpAPI } from "langchain/tools";
import { InMemoryFileStore } from "langchain/stores/file/in_memory";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { ChatOpenAI } from "langchain/chat_models/openai";

const store = new InMemoryFileStore();

const tools = [
new ReadFileTool({ store }),
new WriteFileTool({ store }),
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "San Francisco,California,United States",
hl: "en",
gl: "us",
}),
];

const vectorStore = new MemoryVectorStore(new OpenAIEmbeddings());

const autogpt = AutoGPT.fromLLMAndTools(
new ChatOpenAI({ temperature: 0 }),
tools,
{
memory: vectorStore.asRetriever(),
aiName: "Tom",
aiRole: "Assistant",
}
);

await autogpt.run(["write a weather report for SF today"]);
/*
{
"thoughts": {
"text": "I need to write a weather report for SF today. I should use a search engine to find the current weather conditions.",
"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.",
"plan": "- Use the search command to find the current weather conditions for SF\n- Write a weather report based on the information found",
"criticism": "I need to make sure that the information I find is accurate and up-to-date.",
"speak": "I will use the search command to find the current weather conditions for SF."
},
"command": {
"name": "search",
"args": {
"input": "current weather conditions San Francisco"
}
}
}
{
"thoughts": {
"text": "I have found the current weather conditions for SF. I need to write a weather report based on this information.",
"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.",
"plan": "- Use the write_file command to save the weather report to a file",
"criticism": "I need to make sure that the weather report is clear and concise.",
"speak": "I will use the write_file command to save the weather report to a file."
},
"command": {
"name": "write_file",
"args": {
"file_path": "weather_report.txt",
"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%"
}
}
}
{
"thoughts": {
"text": "I have completed all my objectives. I will use the finish command to signal that I am done.",
"reasoning": "I have completed the task of writing a weather report for SF today, so I don't need to do anything else.",
"plan": "- Use the finish command to signal that I am done",
"criticism": "I need to make sure that I have completed all my objectives before using the finish command.",
"speak": "I will use the finish command to signal that I am done."
},
"command": {
"name": "finish",
"args": {
"response": "I have completed all my objectives."
}
}
}
*/
9 changes: 9 additions & 0 deletions langchain/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ cache.d.ts
cache/redis.cjs
cache/redis.js
cache/redis.d.ts
stores/file/in_memory.cjs
stores/file/in_memory.js
stores/file/in_memory.d.ts
stores/file/node.cjs
stores/file/node.js
stores/file/node.d.ts
experimental/autogpt.cjs
experimental/autogpt.js
experimental/autogpt.d.ts
index.cjs
index.js
index.d.ts
27 changes: 26 additions & 1 deletion langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,15 @@
"cache/redis.cjs",
"cache/redis.js",
"cache/redis.d.ts",
"stores/file/in_memory.cjs",
"stores/file/in_memory.js",
"stores/file/in_memory.d.ts",
"stores/file/node.cjs",
"stores/file/node.js",
"stores/file/node.d.ts",
"experimental/autogpt.cjs",
"experimental/autogpt.js",
"experimental/autogpt.d.ts",
"index.cjs",
"index.js",
"index.d.ts"
Expand Down Expand Up @@ -444,7 +453,8 @@
"p-retry": "4",
"uuid": "^9.0.0",
"yaml": "^2.2.1",
"zod": "^3.21.4"
"zod": "^3.21.4",
"zod-to-json-schema": "^3.20.4"
},
"publishConfig": {
"access": "public"
Expand Down Expand Up @@ -861,6 +871,21 @@
"import": "./cache/redis.js",
"require": "./cache/redis.cjs"
},
"./stores/file/in_memory": {
"types": "./stores/file/in_memory.d.ts",
"import": "./stores/file/in_memory.js",
"require": "./stores/file/in_memory.cjs"
},
"./stores/file/node": {
"types": "./stores/file/node.d.ts",
"import": "./stores/file/node.js",
"require": "./stores/file/node.cjs"
},
"./experimental/autogpt": {
"types": "./experimental/autogpt.d.ts",
"import": "./experimental/autogpt.js",
"require": "./experimental/autogpt.cjs"
},
"./package.json": "./package.json"
}
}
6 changes: 6 additions & 0 deletions langchain/scripts/create-entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ const entrypoints = {
// cache
cache: "cache/index",
"cache/redis": "cache/redis",
// stores
"stores/file/in_memory": "stores/file/in_memory",
"stores/file/node": "stores/file/node",
// experimental
"experimental/autogpt": "experimental/autogpt/index",
};

// Entrypoints in this list will
Expand Down Expand Up @@ -164,6 +169,7 @@ const requiresOptionalDependency = [
"retrievers/supabase",
"retrievers/metal",
"cache/redis",
"stores/file/node",
];

// List of test-exports-* packages which we use to test that the exports field
Expand Down
Loading

0 comments on commit 4a0c691

Please sign in to comment.