# 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

```javascript
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:

```javascript

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."


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rocket-networking.gitbook.io/docs/server-side-scripting/ai-functions-on-the-server.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
