0

Chat with the model

by
Published Apr 8, 2025

Generates a text response to a user message and streams it down, token by token. To learn how to use the Chat API with streaming follow our [Text Generation guides](https://docs.cohere.com/v2/docs/chat-api). Follow the [Migration Guide](https://docs.cohere.com/v2/docs/migrating-v1-to-v2) for instructions on moving from API v1 to API v2.

Script cohere Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Cohere = {
3
  apiKey: string;
4
};
5
/**
6
 * Chat with the model
7
 * Generates a text response to a user message and streams it down, token by token. To learn how to use the Chat API with streaming follow our [Text Generation guides](https://docs.cohere.com/v2/docs/chat-api).
8

9
Follow the [Migration Guide](https://docs.cohere.com/v2/docs/migrating-v1-to-v2) for instructions on moving from API v1 to API v2.
10

11
 */
12
export async function main(
13
  auth: Cohere,
14
  body: {
15
    stream?: false | true;
16
    model: string;
17
    messages:
18
      | { role: "user"; content: string | { type: "text"; text: string }[] }
19
      | {
20
          role: "assistant";
21
          tool_calls?: {
22
            id?: string;
23
            type?: "function";
24
            function?: { name?: string; arguments?: string };
25
          }[];
26
          tool_plan?: string;
27
          content?: string | { type: "text"; text: string }[];
28
          citations?: {
29
            start?: number;
30
            end?: number;
31
            text?: string;
32
            sources?:
33
              | { id?: string; tool_output?: {} }
34
              | { id?: string; document?: {} }[];
35
          }[];
36
        }
37
      | { role: "system"; content: string | { type: "text"; text: string }[] }
38
      | {
39
          role: "tool";
40
          tool_call_id: string;
41
          content:
42
            | string
43
            | { type: "text"; text: string }
44
            | { type: "document"; document: { data: {}; id?: string } }[];
45
        }[];
46
    tools?: {
47
      type?: "function";
48
      function?: { name?: string; description?: string; parameters?: {} };
49
    }[];
50
    strict_tools?: false | true;
51
    documents?: string | { data: {}; id?: string }[];
52
    citation_options?: { mode?: "FAST" | "ACCURATE" | "OFF" };
53
    response_format?:
54
      | { type: "text" | "json_object" }
55
      | { type: "text" | "json_object"; json_schema?: {} };
56
    safety_mode?: "OFF" | "CONTEXTUAL" | "STRICT";
57
    max_tokens?: number;
58
    stop_sequences?: string[];
59
    temperature?: number;
60
    seed?: number;
61
    frequency_penalty?: number;
62
    presence_penalty?: number;
63
    k?: number;
64
    p?: number;
65
    logprobs?: false | true;
66
  },
67
) {
68
  const url = new URL(`https://api.cohere.com/v2/chat`);
69

70
  const response = await fetch(url, {
71
    method: "POST",
72
    headers: {
73
      "Content-Type": "application/json",
74
      Authorization: "Bearer " + auth.apiKey,
75
    },
76
    body: JSON.stringify(body),
77
  });
78
  if (!response.ok) {
79
    const text = await response.text();
80
    throw new Error(`${response.status} ${text}`);
81
  }
82
  return await response.json();
83
}
84