1 | |
2 | type Togetherai = { |
3 | api_key: string; |
4 | }; |
5 | |
6 | * Create chat completion |
7 | * Query a chat model. |
8 | */ |
9 | export async function main( |
10 | auth: Togetherai, |
11 | body: { |
12 | messages: { |
13 | role: "system" | "user" | "assistant" | "tool"; |
14 | content: |
15 | | string |
16 | | { type: "text"; text: string } |
17 | | ({ type: "image_url"; image_url: { url: string } }[] & string); |
18 | }[]; |
19 | model: string; |
20 | max_tokens?: number; |
21 | stop?: string[]; |
22 | temperature?: number; |
23 | top_p?: number; |
24 | top_k?: number; |
25 | repetition_penalty?: number; |
26 | stream?: false | true; |
27 | logprobs?: number; |
28 | echo?: false | true; |
29 | n?: number; |
30 | min_p?: number; |
31 | presence_penalty?: number; |
32 | frequency_penalty?: number; |
33 | logit_bias?: {}; |
34 | seed?: number; |
35 | function_call?: "none" | "auto" | { name: string }; |
36 | response_format?: { type?: string; schema?: {} }; |
37 | tools?: { |
38 | type?: string; |
39 | function?: { description?: string; name?: string; parameters?: {} }; |
40 | }[]; |
41 | tool_choice?: |
42 | | string |
43 | | { |
44 | index: number; |
45 | id: string; |
46 | type: "function"; |
47 | function: { name: string; arguments: string }; |
48 | }; |
49 | safety_model?: string; |
50 | }, |
51 | ) { |
52 | const url = new URL(`https://api.together.xyz/v1/chat/completions`); |
53 |
|
54 | const response = await fetch(url, { |
55 | method: "POST", |
56 | headers: { |
57 | "Content-Type": "application/json", |
58 | Authorization: "Bearer " + auth.api_key, |
59 | }, |
60 | body: JSON.stringify(body), |
61 | }); |
62 | if (!response.ok) { |
63 | const text = await response.text(); |
64 | throw new Error(`${response.status} ${text}`); |
65 | } |
66 | return await response.json(); |
67 | } |
68 |
|