Studio Chat Complete

This is the endpoint for the [Jamba Instruct model](https://docs.ai21.com/docs/jamba-models). This is a foundation model that supports both single-turn (question answering, text completion) and multi-turn (chat style) interactions. You can optionally stream results if you want to get the response as each token is generated, rather than waiting for the entire response.

Script ai21 Verified

by hugo697 ยท 4/8/2025

The script

Submitted by hugo697 Bun
Verified 427 days ago
1
//native
2
type Ai21 = {
3
  apiKey: string;
4
};
5
/**
6
 * Studio Chat Complete
7
 * This is the endpoint for the [Jamba Instruct model](https://docs.ai21.com/docs/jamba-models).
8
This is a foundation model that supports both single-turn (question answering,
9
text completion) and multi-turn (chat style) interactions.
10

11
You can optionally stream results if you want to get the response as each
12
token is generated, rather than waiting for the entire response.
13
 */
14
export async function main(
15
  auth: Ai21,
16
  body: {
17
    model:
18
      | "jamba-instruct"
19
      | "jamba-instruct-preview"
20
      | "jamba-1.5-mini"
21
      | "jamba-1.5-large";
22
    messages:
23
      | { role?: "user"; content: string }
24
      | {
25
          role?: "assistant";
26
          content?: string;
27
          tool_calls?: {
28
            id: string;
29
            type?: "function";
30
            function: { name: string; arguments: string };
31
          }[];
32
        }
33
      | { role?: "tool"; content: string; tool_call_id: string }
34
      | { role?: "system"; content: string }[];
35
    tools?: {
36
      type: "function";
37
      function: {
38
        name: string;
39
        description?: string;
40
        parameters?: { type?: "object"; properties: {}; required?: string[] };
41
      };
42
    }[];
43
    n?: number;
44
    max_tokens?: number;
45
    temperature?: number;
46
    top_p?: number;
47
    stop?: string | string[];
48
    stream?: false | true;
49
    mock_response?: {
50
      response_delay_seconds?: number;
51
      stream_response_delay_between_deltas_seconds?: number;
52
    };
53
    documents?: { id?: string; content: string; metadata?: {} }[];
54
    response_format?: { type: "text" | "json_object" };
55
  },
56
) {
57
  const url = new URL(`https://api.ai21.com/studio/v1/chat/completions`);
58

59
  const response = await fetch(url, {
60
    method: "POST",
61
    headers: {
62
      "Content-Type": "application/json",
63
      Authorization: "Bearer " + auth.apiKey,
64
    },
65
    body: JSON.stringify(body),
66
  });
67
  if (!response.ok) {
68
    const text = await response.text();
69
    throw new Error(`${response.status} ${text}`);
70
  }
71
  return await response.json();
72
}
73