Merge pull request #1 from kolbytn/update-openai

updated open ai package and code
This commit is contained in:
Kolby Nottingham 2023-09-28 20:59:44 -07:00 committed by GitHub
commit 9682255f71
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 18 deletions

View file

@ -6,12 +6,7 @@ Crafting minds for Minecraft with AI!
Install Node.js >= 14 from [nodejs.org](https://nodejs.org/) Install Node.js >= 14 from [nodejs.org](https://nodejs.org/)
Then, install mineflayer Install node modules with `npm install`
```
npm install mineflayer
npm install mineflayer-pathfinder
npm install mineflayer-collectblock
```
## Usage ## Usage
@ -19,4 +14,4 @@ Start minecraft server on localhost port `55916`
Add `OPENAI_API_KEY` (and optionally `OPENAI_ORG_ID`) to your environment variables. Add `OPENAI_API_KEY` (and optionally `OPENAI_ORG_ID`) to your environment variables.
run `npm main.js` run `node main.js`

View file

@ -1,8 +1,9 @@
{ {
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"mineflayer": "^4.11.0", "mineflayer": "^4.14.0",
"mineflayer-collectblock": "^1.4.1", "mineflayer-collectblock": "^1.4.1",
"mineflayer-pathfinder": "^2.4.4" "mineflayer-pathfinder": "^2.4.4",
"openai": "^4.4.0"
} }
} }

View file

@ -1,16 +1,16 @@
import { Configuration, OpenAIApi } from 'openai'; import OpenAIApi from 'openai';
var openAiConfig = null; let openAiConfig = null;
if (process.env.OPENAI_ORG_ID) { if (process.env.OPENAI_ORG_ID) {
openAiConfig = new Configuration({ openAiConfig = {
organization: process.env.OPENAI_ORG_ID, organization: process.env.OPENAI_ORG_ID,
apiKey: process.env.OPENAI_API_KEY, apiKey: process.env.OPENAI_API_KEY,
}); };
} else { } else {
openAiConfig = new Configuration({ openAiConfig = {
apiKey: process.env.OPENAI_API_KEY, apiKey: process.env.OPENAI_API_KEY,
}); };
} }
const openai = new OpenAIApi(openAiConfig); const openai = new OpenAIApi(openAiConfig);
@ -28,16 +28,16 @@ export async function sendRequest(turns, systemMessage, stop_seq) {
let res = null; let res = null;
try { try {
let completion = await openai.createChatCompletion({ let completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo', model: 'gpt-3.5-turbo',
messages: messages, messages: messages,
stop: stop_seq, stop: stop_seq,
}); });
res = completion.data.choices[0].message.content; res = completion.choices[0].message.content;
} }
catch (err) { catch (err) {
console.log(err); console.log(err);
res = 'I amm sorry, I do not know how to respond to that.'; res = 'I am sorry, I do not know how to respond to that.';
} }
return res; return res;
} }