@ai-sdk/togetherai 2.0.17 → 2.0.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # @ai-sdk/togetherai
2
2
 
3
+ ## 2.0.18
4
+
5
+ ### Patch Changes
6
+
7
+ - 8dc54db: chore: add src folders to package bundle
8
+ - Updated dependencies [8dc54db]
9
+ - @ai-sdk/openai-compatible@2.0.17
10
+
3
11
  ## 2.0.17
4
12
 
5
13
  ### Patch Changes
package/dist/index.js CHANGED
@@ -272,7 +272,7 @@ var togetheraiImageProviderOptionsSchema = (0, import_provider_utils4.lazySchema
272
272
  );
273
273
 
274
274
  // src/version.ts
275
- var VERSION = true ? "2.0.17" : "0.0.0-test";
275
+ var VERSION = true ? "2.0.18" : "0.0.0-test";
276
276
 
277
277
  // src/togetherai-provider.ts
278
278
  function createTogetherAI(options = {}) {
package/dist/index.mjs CHANGED
@@ -267,7 +267,7 @@ var togetheraiImageProviderOptionsSchema = lazySchema3(
267
267
  );
268
268
 
269
269
  // src/version.ts
270
- var VERSION = true ? "2.0.17" : "0.0.0-test";
270
+ var VERSION = true ? "2.0.18" : "0.0.0-test";
271
271
 
272
272
  // src/togetherai-provider.ts
273
273
  function createTogetherAI(options = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/togetherai",
3
- "version": "2.0.17",
3
+ "version": "2.0.18",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -8,6 +8,7 @@
8
8
  "types": "./dist/index.d.ts",
9
9
  "files": [
10
10
  "dist/**/*",
11
+ "src",
11
12
  "CHANGELOG.md",
12
13
  "README.md"
13
14
  ],
@@ -20,7 +21,7 @@
20
21
  }
21
22
  },
22
23
  "dependencies": {
23
- "@ai-sdk/openai-compatible": "2.0.16",
24
+ "@ai-sdk/openai-compatible": "2.0.17",
24
25
  "@ai-sdk/provider": "3.0.4",
25
26
  "@ai-sdk/provider-utils": "4.0.8"
26
27
  },
@@ -29,8 +30,8 @@
29
30
  "tsup": "^8",
30
31
  "typescript": "5.8.3",
31
32
  "zod": "3.25.76",
32
- "@vercel/ai-tsconfig": "0.0.0",
33
- "@ai-sdk/test-server": "1.0.1"
33
+ "@ai-sdk/test-server": "1.0.2",
34
+ "@vercel/ai-tsconfig": "0.0.0"
34
35
  },
35
36
  "peerDependencies": {
36
37
  "zod": "^3.25.76 || ^4.1.8"
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export type { OpenAICompatibleErrorData as TogetherAIErrorData } from '@ai-sdk/openai-compatible';
2
+ export type { TogetherAIRerankingOptions } from './reranking/togetherai-reranking-options';
3
+ export { createTogetherAI, togetherai } from './togetherai-provider';
4
+ export type {
5
+ TogetherAIProvider,
6
+ TogetherAIProviderSettings,
7
+ } from './togetherai-provider';
8
+ export type { TogetherAIImageProviderOptions } from './togetherai-image-model';
9
+ export { VERSION } from './version';
@@ -0,0 +1,22 @@
1
+ {
2
+ "id": "oGs6Zt9-62bZhn-99529372487b1b0a",
3
+ "object": "rerank",
4
+ "model": "Salesforce/Llama-Rank-v1",
5
+ "results": [
6
+ {
7
+ "index": 0,
8
+ "relevance_score": 0.6475887154399037,
9
+ "document": {}
10
+ },
11
+ {
12
+ "index": 5,
13
+ "relevance_score": 0.6323295373206566,
14
+ "document": {}
15
+ }
16
+ ],
17
+ "usage": {
18
+ "prompt_tokens": 2966,
19
+ "completion_tokens": 0,
20
+ "total_tokens": 2966
21
+ }
22
+ }
@@ -0,0 +1,43 @@
1
+ import { JSONObject } from '@ai-sdk/provider';
2
+ import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
3
+ import { z } from 'zod/v4';
4
+
5
+ // https://docs.together.ai/reference/rerank-1
6
+ export type TogetherAIRerankingInput = {
7
+ model: string;
8
+ query: string;
9
+ documents: JSONObject[] | string[];
10
+ top_n: number | undefined;
11
+ return_documents: boolean | undefined;
12
+ rank_fields: string[] | undefined;
13
+ };
14
+
15
+ export const togetheraiErrorSchema = lazySchema(() =>
16
+ zodSchema(
17
+ z.object({
18
+ error: z.object({
19
+ message: z.string(),
20
+ }),
21
+ }),
22
+ ),
23
+ );
24
+
25
+ export const togetheraiRerankingResponseSchema = lazySchema(() =>
26
+ zodSchema(
27
+ z.object({
28
+ id: z.string().nullish(),
29
+ model: z.string().nullish(),
30
+ results: z.array(
31
+ z.object({
32
+ index: z.number(),
33
+ relevance_score: z.number(),
34
+ }),
35
+ ),
36
+ usage: z.object({
37
+ prompt_tokens: z.number(),
38
+ completion_tokens: z.number(),
39
+ total_tokens: z.number(),
40
+ }),
41
+ }),
42
+ ),
43
+ );
@@ -0,0 +1,245 @@
1
+ import { createTestServer } from '@ai-sdk/test-server/with-vitest';
2
+ import fs from 'node:fs';
3
+ import { beforeEach, describe, expect, it } from 'vitest';
4
+ import { createTogetherAI } from '../togetherai-provider';
5
+ import { TogetherAIRerankingOptions } from './togetherai-reranking-options';
6
+
7
+ const provider = createTogetherAI({ apiKey: 'test-api-key' });
8
+ const model = provider.rerankingModel('Salesforce/Llama-Rank-v1');
9
+
10
+ describe('doRerank', () => {
11
+ const server = createTestServer({
12
+ 'https://api.together.xyz/v1/rerank': {},
13
+ });
14
+
15
+ function prepareJsonFixtureResponse(filename: string) {
16
+ server.urls['https://api.together.xyz/v1/rerank'].response = {
17
+ type: 'json-value',
18
+ body: JSON.parse(
19
+ fs.readFileSync(`src/reranking/__fixtures__/${filename}.json`, 'utf8'),
20
+ ),
21
+ };
22
+ return;
23
+ }
24
+
25
+ describe('json documents', () => {
26
+ let result: Awaited<ReturnType<typeof model.doRerank>>;
27
+
28
+ beforeEach(async () => {
29
+ prepareJsonFixtureResponse('togetherai-reranking.1');
30
+
31
+ result = await model.doRerank({
32
+ documents: {
33
+ type: 'object',
34
+ values: [
35
+ { example: 'sunny day at the beach' },
36
+ { example: 'rainy day in the city' },
37
+ ],
38
+ },
39
+ query: 'rainy day',
40
+ topN: 2,
41
+ providerOptions: {
42
+ togetherai: {
43
+ rankFields: ['example'],
44
+ } satisfies TogetherAIRerankingOptions,
45
+ },
46
+ });
47
+ });
48
+
49
+ it('should send request with stringified json documents', async () => {
50
+ expect(await server.calls[0].requestBodyJson).toMatchInlineSnapshot(`
51
+ {
52
+ "documents": [
53
+ {
54
+ "example": "sunny day at the beach",
55
+ },
56
+ {
57
+ "example": "rainy day in the city",
58
+ },
59
+ ],
60
+ "model": "Salesforce/Llama-Rank-v1",
61
+ "query": "rainy day",
62
+ "rank_fields": [
63
+ "example",
64
+ ],
65
+ "return_documents": false,
66
+ "top_n": 2,
67
+ }
68
+ `);
69
+ });
70
+
71
+ it('should send request with the correct headers', async () => {
72
+ expect(server.calls[0].requestHeaders).toMatchInlineSnapshot(`
73
+ {
74
+ "authorization": "Bearer test-api-key",
75
+ "content-type": "application/json",
76
+ }
77
+ `);
78
+ });
79
+
80
+ it('should return result with warnings', async () => {
81
+ expect(result.warnings).toMatchInlineSnapshot(`undefined`);
82
+ });
83
+
84
+ it('should return result with the correct ranking', async () => {
85
+ expect(result.ranking).toMatchInlineSnapshot(`
86
+ [
87
+ {
88
+ "index": 0,
89
+ "relevanceScore": 0.6475887154399037,
90
+ },
91
+ {
92
+ "index": 5,
93
+ "relevanceScore": 0.6323295373206566,
94
+ },
95
+ ]
96
+ `);
97
+ });
98
+
99
+ it('should not return provider metadata (use response body instead)', async () => {
100
+ expect(result.providerMetadata).toMatchInlineSnapshot(`undefined`);
101
+ });
102
+
103
+ it('should return result with the correct response', async () => {
104
+ expect(result.response).toMatchInlineSnapshot(`
105
+ {
106
+ "body": {
107
+ "id": "oGs6Zt9-62bZhn-99529372487b1b0a",
108
+ "model": "Salesforce/Llama-Rank-v1",
109
+ "object": "rerank",
110
+ "results": [
111
+ {
112
+ "document": {},
113
+ "index": 0,
114
+ "relevance_score": 0.6475887154399037,
115
+ },
116
+ {
117
+ "document": {},
118
+ "index": 5,
119
+ "relevance_score": 0.6323295373206566,
120
+ },
121
+ ],
122
+ "usage": {
123
+ "completion_tokens": 0,
124
+ "prompt_tokens": 2966,
125
+ "total_tokens": 2966,
126
+ },
127
+ },
128
+ "headers": {
129
+ "content-length": "304",
130
+ "content-type": "application/json",
131
+ },
132
+ "id": "oGs6Zt9-62bZhn-99529372487b1b0a",
133
+ "modelId": "Salesforce/Llama-Rank-v1",
134
+ }
135
+ `);
136
+ });
137
+ });
138
+
139
+ describe('text documents', () => {
140
+ let result: Awaited<ReturnType<typeof model.doRerank>>;
141
+
142
+ beforeEach(async () => {
143
+ prepareJsonFixtureResponse('togetherai-reranking.1');
144
+
145
+ result = await model.doRerank({
146
+ documents: {
147
+ type: 'text',
148
+ values: ['sunny day at the beach', 'rainy day in the city'],
149
+ },
150
+ query: 'rainy day',
151
+ topN: 2,
152
+ providerOptions: {
153
+ togetherai: {
154
+ rankFields: ['example'],
155
+ } satisfies TogetherAIRerankingOptions,
156
+ },
157
+ });
158
+ });
159
+
160
+ it('should send request with text documents', async () => {
161
+ expect(await server.calls[0].requestBodyJson).toMatchInlineSnapshot(`
162
+ {
163
+ "documents": [
164
+ "sunny day at the beach",
165
+ "rainy day in the city",
166
+ ],
167
+ "model": "Salesforce/Llama-Rank-v1",
168
+ "query": "rainy day",
169
+ "rank_fields": [
170
+ "example",
171
+ ],
172
+ "return_documents": false,
173
+ "top_n": 2,
174
+ }
175
+ `);
176
+ });
177
+
178
+ it('should send request with the correct headers', async () => {
179
+ expect(server.calls[0].requestHeaders).toMatchInlineSnapshot(`
180
+ {
181
+ "authorization": "Bearer test-api-key",
182
+ "content-type": "application/json",
183
+ }
184
+ `);
185
+ });
186
+
187
+ it('should return result without warnings', async () => {
188
+ expect(result.warnings).toMatchInlineSnapshot(`undefined`);
189
+ });
190
+
191
+ it('should return result with the correct ranking', async () => {
192
+ expect(result.ranking).toMatchInlineSnapshot(`
193
+ [
194
+ {
195
+ "index": 0,
196
+ "relevanceScore": 0.6475887154399037,
197
+ },
198
+ {
199
+ "index": 5,
200
+ "relevanceScore": 0.6323295373206566,
201
+ },
202
+ ]
203
+ `);
204
+ });
205
+
206
+ it('should not return provider metadata (use response body instead)', async () => {
207
+ expect(result.providerMetadata).toMatchInlineSnapshot(`undefined`);
208
+ });
209
+
210
+ it('should return result with the correct response', async () => {
211
+ expect(result.response).toMatchInlineSnapshot(`
212
+ {
213
+ "body": {
214
+ "id": "oGs6Zt9-62bZhn-99529372487b1b0a",
215
+ "model": "Salesforce/Llama-Rank-v1",
216
+ "object": "rerank",
217
+ "results": [
218
+ {
219
+ "document": {},
220
+ "index": 0,
221
+ "relevance_score": 0.6475887154399037,
222
+ },
223
+ {
224
+ "document": {},
225
+ "index": 5,
226
+ "relevance_score": 0.6323295373206566,
227
+ },
228
+ ],
229
+ "usage": {
230
+ "completion_tokens": 0,
231
+ "prompt_tokens": 2966,
232
+ "total_tokens": 2966,
233
+ },
234
+ },
235
+ "headers": {
236
+ "content-length": "304",
237
+ "content-type": "application/json",
238
+ },
239
+ "id": "oGs6Zt9-62bZhn-99529372487b1b0a",
240
+ "modelId": "Salesforce/Llama-Rank-v1",
241
+ }
242
+ `);
243
+ });
244
+ });
245
+ });
@@ -0,0 +1,101 @@
1
+ import { RerankingModelV3 } from '@ai-sdk/provider';
2
+ import {
3
+ combineHeaders,
4
+ createJsonErrorResponseHandler,
5
+ createJsonResponseHandler,
6
+ FetchFunction,
7
+ parseProviderOptions,
8
+ postJsonToApi,
9
+ } from '@ai-sdk/provider-utils';
10
+ import {
11
+ togetheraiErrorSchema,
12
+ TogetherAIRerankingInput,
13
+ togetheraiRerankingResponseSchema,
14
+ } from './togetherai-reranking-api';
15
+ import {
16
+ TogetherAIRerankingModelId,
17
+ togetheraiRerankingOptionsSchema,
18
+ } from './togetherai-reranking-options';
19
+
20
+ type TogetherAIRerankingConfig = {
21
+ provider: string;
22
+ baseURL: string;
23
+ headers: () => Record<string, string | undefined>;
24
+ fetch?: FetchFunction;
25
+ };
26
+
27
+ export class TogetherAIRerankingModel implements RerankingModelV3 {
28
+ readonly specificationVersion = 'v3';
29
+ readonly modelId: TogetherAIRerankingModelId;
30
+
31
+ private readonly config: TogetherAIRerankingConfig;
32
+
33
+ constructor(
34
+ modelId: TogetherAIRerankingModelId,
35
+ config: TogetherAIRerankingConfig,
36
+ ) {
37
+ this.modelId = modelId;
38
+ this.config = config;
39
+ }
40
+
41
+ get provider(): string {
42
+ return this.config.provider;
43
+ }
44
+
45
+ // see https://docs.together.ai/reference/rerank-1
46
+ async doRerank({
47
+ documents,
48
+ headers,
49
+ query,
50
+ topN,
51
+ abortSignal,
52
+ providerOptions,
53
+ }: Parameters<RerankingModelV3['doRerank']>[0]): Promise<
54
+ Awaited<ReturnType<RerankingModelV3['doRerank']>>
55
+ > {
56
+ const rerankingOptions = await parseProviderOptions({
57
+ provider: 'togetherai',
58
+ providerOptions,
59
+ schema: togetheraiRerankingOptionsSchema,
60
+ });
61
+
62
+ const {
63
+ responseHeaders,
64
+ value: response,
65
+ rawValue,
66
+ } = await postJsonToApi({
67
+ url: `${this.config.baseURL}/rerank`,
68
+ headers: combineHeaders(this.config.headers(), headers),
69
+ body: {
70
+ model: this.modelId,
71
+ documents: documents.values,
72
+ query,
73
+ top_n: topN,
74
+ rank_fields: rerankingOptions?.rankFields,
75
+ return_documents: false, // reduce response size
76
+ } satisfies TogetherAIRerankingInput,
77
+ failedResponseHandler: createJsonErrorResponseHandler({
78
+ errorSchema: togetheraiErrorSchema,
79
+ errorToMessage: data => data.error.message,
80
+ }),
81
+ successfulResponseHandler: createJsonResponseHandler(
82
+ togetheraiRerankingResponseSchema,
83
+ ),
84
+ abortSignal,
85
+ fetch: this.config.fetch,
86
+ });
87
+
88
+ return {
89
+ ranking: response.results.map(result => ({
90
+ index: result.index,
91
+ relevanceScore: result.relevance_score,
92
+ })),
93
+ response: {
94
+ id: response.id ?? undefined,
95
+ modelId: response.model ?? undefined,
96
+ headers: responseHeaders,
97
+ body: rawValue,
98
+ },
99
+ };
100
+ }
101
+ }
@@ -0,0 +1,27 @@
1
+ import { FlexibleSchema, lazySchema, zodSchema } from '@ai-sdk/provider-utils';
2
+ import { z } from 'zod/v4';
3
+
4
+ // see https://docs.together.ai/docs/serverless-models#rerank-models
5
+ export type TogetherAIRerankingModelId =
6
+ | 'Salesforce/Llama-Rank-v1'
7
+ | 'mixedbread-ai/Mxbai-Rerank-Large-V2'
8
+ | (string & {});
9
+
10
+ export type TogetherAIRerankingOptions = {
11
+ /**
12
+ * List of keys in the JSON Object document to rank by.
13
+ * Defaults to use all supplied keys for ranking.
14
+ *
15
+ * @example ["title", "text"]
16
+ */
17
+ rankFields?: string[];
18
+ };
19
+
20
+ export const togetheraiRerankingOptionsSchema: FlexibleSchema<TogetherAIRerankingOptions> =
21
+ lazySchema(() =>
22
+ zodSchema(
23
+ z.object({
24
+ rankFields: z.array(z.string()).optional(),
25
+ }),
26
+ ),
27
+ );
@@ -0,0 +1,36 @@
1
+ // https://docs.together.ai/docs/serverless-models#chat-models
2
+ export type TogetherAIChatModelId =
3
+ | 'meta-llama/Llama-3.3-70B-Instruct-Turbo'
4
+ | 'meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo'
5
+ | 'meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo'
6
+ | 'meta-llama/Meta-Llama-3.1-405B-Instruct-Turbo'
7
+ | 'meta-llama/Meta-Llama-3-8B-Instruct-Turbo'
8
+ | 'meta-llama/Meta-Llama-3-70B-Instruct-Turbo'
9
+ | 'meta-llama/Llama-3.2-3B-Instruct-Turbo'
10
+ | 'meta-llama/Meta-Llama-3-8B-Instruct-Lite'
11
+ | 'meta-llama/Meta-Llama-3-70B-Instruct-Lite'
12
+ | 'meta-llama/Llama-3-8b-chat-hf'
13
+ | 'meta-llama/Llama-3-70b-chat-hf'
14
+ | 'nvidia/Llama-3.1-Nemotron-70B-Instruct-HF'
15
+ | 'Qwen/Qwen2.5-Coder-32B-Instruct'
16
+ | 'Qwen/QwQ-32B-Preview'
17
+ | 'microsoft/WizardLM-2-8x22B'
18
+ | 'google/gemma-2-27b-it'
19
+ | 'google/gemma-2-9b-it'
20
+ | 'databricks/dbrx-instruct'
21
+ | 'deepseek-ai/deepseek-llm-67b-chat'
22
+ | 'deepseek-ai/DeepSeek-V3'
23
+ | 'google/gemma-2b-it'
24
+ | 'Gryphe/MythoMax-L2-13b'
25
+ | 'meta-llama/Llama-2-13b-chat-hf'
26
+ | 'mistralai/Mistral-7B-Instruct-v0.1'
27
+ | 'mistralai/Mistral-7B-Instruct-v0.2'
28
+ | 'mistralai/Mistral-7B-Instruct-v0.3'
29
+ | 'mistralai/Mixtral-8x7B-Instruct-v0.1'
30
+ | 'mistralai/Mixtral-8x22B-Instruct-v0.1'
31
+ | 'NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO'
32
+ | 'Qwen/Qwen2.5-7B-Instruct-Turbo'
33
+ | 'Qwen/Qwen2.5-72B-Instruct-Turbo'
34
+ | 'Qwen/Qwen2-72B-Instruct'
35
+ | 'upstage/SOLAR-10.7B-Instruct-v1.0'
36
+ | (string & {});
@@ -0,0 +1,9 @@
1
+ // https://docs.together.ai/docs/serverless-models#language-models
2
+ export type TogetherAICompletionModelId =
3
+ | 'meta-llama/Llama-2-70b-hf'
4
+ | 'mistralai/Mistral-7B-v0.1'
5
+ | 'mistralai/Mixtral-8x7B-v0.1'
6
+ | 'Meta-Llama/Llama-Guard-7b'
7
+ | 'codellama/CodeLlama-34b-Instruct-hf'
8
+ | 'Qwen/Qwen2.5-Coder-32B-Instruct'
9
+ | (string & {});
@@ -0,0 +1,11 @@
1
+ // https://docs.together.ai/docs/serverless-models#embedding-models
2
+ export type TogetherAIEmbeddingModelId =
3
+ | 'togethercomputer/m2-bert-80M-2k-retrieval'
4
+ | 'togethercomputer/m2-bert-80M-32k-retrieval'
5
+ | 'togethercomputer/m2-bert-80M-8k-retrieval'
6
+ | 'WhereIsAI/UAE-Large-V1'
7
+ | 'BAAI/bge-large-en-v1.5'
8
+ | 'BAAI/bge-base-en-v1.5'
9
+ | 'sentence-transformers/msmarco-bert-base-dot-v5'
10
+ | 'bert-base-uncased'
11
+ | (string & {});