manekinekko / minecraft-openai

Controlling a Minecraft NPC using OpenAI 🤖
https://slides.com/wassimchegham/playing-minecraft-artificial-intelligence-open-ai-gpt-3-javascript
MIT License
108 stars 12 forks source link

minecraft-openai.api:error api response failed with statis Not Found +27s minecraft-openai.bot:log OpenAI response was empty. Ignore. +189ms #12

Open philibertschlutzki opened 1 year ago

philibertschlutzki commented 1 year ago

It looks like the API isn't working correctly


 +0ms
  minecraft-openai.api:log payload { prompt: '\n' + '// Go forward\n' + "bot.setControlState('forward', true);\n" + '\n' + '// Go back\n' + "bot.setControlState('back', true);\n" + '\n' + '// jump\n' + "bot.setControlState('jump', true);\n" + '\n' + '// Hello !\n' + 'bot.chat("Hello friend!");\n' + '  \n' + '// Hi how are you ?\n' + `bot.chat("I'm fine, thanks!");\n` + '\n' + "// What's your name ?\n" + 'bot.chat("My name is " + bot.username);\n' + '\n' + "// What's your favorite color ?\n" + 'bot.chat("I like red");\n' + '\n' + "// What's your favorite conference?\n" + 'bot.chat("Devoxx France, of course!");\n' + '  \n' + '// hello\n', max_tokens: 300, temperature: 0, stop: '//', n: 1 } +27s
  minecraft-openai.api:log context:
  minecraft-openai.api:log 
// Go forward
bot.setControlState('forward', true);

// Go back
bot.setControlState('back', true);

// jump
bot.setControlState('jump', true);

// Hello !
bot.chat("Hello friend!");

// Hi how are you ?
bot.chat("I'm fine, thanks!");

// What's your name ?
bot.chat("My name is " + bot.username);

// What's your favorite color ?
bot.chat("I like red");

// What's your favorite conference?
bot.chat("Devoxx France, of course!");

// hello
 +1ms
  minecraft-openai.api:error api response failed with statis Not Found +27s
  minecraft-openai.bot:log OpenAI response was empty. Ignore. +189ms
ranguisj commented 1 year ago

Solution : modifier le fichier api.js pour qu'il corresponde à la nouvelle version de l'API OpenAI. En effet, il faut maintenant spécifier le modèle dans le corps de la requête et non plus dans l'URL.

import fetch from "isomorphic-fetch";
import dotenv from "dotenv";
dotenv.config();

import debug from "debug";
const error = debug("minecraft-openai.api:error");
const log = debug("minecraft-openai.api:log");

const STOP_WORD = "//";
const EOL = "\n";

/**
 * Call the OpenAI API with the previous context and the new user input.
 *
 * @param {string} input The user's input from the Minecraft chat.
 * @param {string} context The previous context to be sent to the OpenAI API
 * @returns {Pormise<{ id: string, object: string, created: number, mode: string, choices: Array<{ text: string, index: number, logprobs: any, finish_reason: text }> }>}
 */
export async function callOpenAI(input, context) {
  const openAIkey = process.env.CODEX_API_KEY;
  if (!openAIkey) {
    error("ERROR: CODEX_API_KEY is required.");
    process.exit(1);
  }

  const body = {
    prompt: `${context}${EOL}${STOP_WORD} ${input}${EOL}`,
    model: "text-davinci-003",
    temperature: 1,
    max_tokens: 256,
    top_p: 1,
    frequency_penalty: 0,
    presence_penalty: 0,
  };

  log("payload %o", body);
  log("context:%s\n", body.prompt);

  const response = await fetch("https://api.openai.com/v1/completions", {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${openAIkey}`,
    },
    body: JSON.stringify(body),
  });

  log(response);

  if (!response.ok) {
    error("api response failed with statis %s", response.statusText);
    return;
  }

  return await response.json();
}

Enjoy ;)