AI Functions on the Server

The callAI method is a server-side function available within the Game class. This method allows you to make requests to the Rocket Networking AI service, powered by OpenAI, to generate AI-based responses using the GPT-3.5 Turbo model. You can call this function in every Game instance to interact with the AI service.

Method Signature

async callAI(messagesArray: Array<{ role: string, content: string }>, temperature: number): Promise<any>

Parameters

  • messagesArray (Array of objects): An array of message objects that form the conversation context. Each message object should have a role (either 'system', 'user', or 'assistant') and content containing the text of the message.

  • temperature (number): A floating-point value controlling the randomness of the AI's response. Higher values (e.g., 0.8) make the response more random, while lower values (e.g., 0.2) make it more deterministic.

Returns

  • A Promise that resolves to the AI response data.

Usage

You can use the callAI method within a Game instance to make AI requests as follows:


const messages = [
  { role: "system", content: "You are a helpful assistant." },
  { role: "user", content: "Tell me a joke." },
  { role: "assistant", content: "Why did the chicken cross the road?" }
];
const temperature = 0.7;

game.callAI(messages, temperature)
  .then((response) => {
    console.log("AI response data is:");
    console.log(response);

    if(response == "error"){
      //error
    }else{
      // Handle the AI response here. response.message is the reply and response.creditsUsed is how many credits were used
    }
  })

Error Handling

  • If there's an error in making the AI request (e.g., network issues), the method will catch the error and return the string "error."

Last updated