@ai-sdk/togetherai 2.0.16 → 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.
@@ -0,0 +1,188 @@
1
+ import { ImageModelV3, SharedV3Warning } from '@ai-sdk/provider';
2
+ import {
3
+ combineHeaders,
4
+ convertImageModelFileToDataUri,
5
+ createJsonResponseHandler,
6
+ createJsonErrorResponseHandler,
7
+ FetchFunction,
8
+ InferSchema,
9
+ lazySchema,
10
+ parseProviderOptions,
11
+ postJsonToApi,
12
+ zodSchema,
13
+ } from '@ai-sdk/provider-utils';
14
+ import { TogetherAIImageModelId } from './togetherai-image-settings';
15
+ import { z } from 'zod/v4';
16
+
17
+ interface TogetherAIImageModelConfig {
18
+ provider: string;
19
+ baseURL: string;
20
+ headers: () => Record<string, string>;
21
+ fetch?: FetchFunction;
22
+ _internal?: {
23
+ currentDate?: () => Date;
24
+ };
25
+ }
26
+
27
+ export class TogetherAIImageModel implements ImageModelV3 {
28
+ readonly specificationVersion = 'v3';
29
+ readonly maxImagesPerCall = 1;
30
+
31
+ get provider(): string {
32
+ return this.config.provider;
33
+ }
34
+
35
+ constructor(
36
+ readonly modelId: TogetherAIImageModelId,
37
+ private config: TogetherAIImageModelConfig,
38
+ ) {}
39
+
40
+ async doGenerate({
41
+ prompt,
42
+ n,
43
+ size,
44
+ seed,
45
+ providerOptions,
46
+ headers,
47
+ abortSignal,
48
+ files,
49
+ mask,
50
+ }: Parameters<ImageModelV3['doGenerate']>[0]): Promise<
51
+ Awaited<ReturnType<ImageModelV3['doGenerate']>>
52
+ > {
53
+ const warnings: Array<SharedV3Warning> = [];
54
+
55
+ if (mask != null) {
56
+ throw new Error(
57
+ 'Together AI does not support mask-based image editing. ' +
58
+ 'Use FLUX Kontext models (e.g., black-forest-labs/FLUX.1-kontext-pro) ' +
59
+ 'with a reference image and descriptive prompt instead.',
60
+ );
61
+ }
62
+
63
+ if (size != null) {
64
+ warnings.push({
65
+ type: 'unsupported',
66
+ feature: 'aspectRatio',
67
+ details:
68
+ 'This model does not support the `aspectRatio` option. Use `size` instead.',
69
+ });
70
+ }
71
+
72
+ const currentDate = this.config._internal?.currentDate?.() ?? new Date();
73
+
74
+ const togetheraiOptions = await parseProviderOptions({
75
+ provider: 'togetherai',
76
+ providerOptions,
77
+ schema: togetheraiImageProviderOptionsSchema,
78
+ });
79
+
80
+ // Handle image input from files
81
+ let imageUrl: string | undefined;
82
+ if (files != null && files.length > 0) {
83
+ imageUrl = convertImageModelFileToDataUri(files[0]);
84
+
85
+ if (files.length > 1) {
86
+ warnings.push({
87
+ type: 'other',
88
+ message:
89
+ 'Together AI only supports a single input image. Additional images are ignored.',
90
+ });
91
+ }
92
+ }
93
+
94
+ const splitSize = size?.split('x');
95
+ // https://docs.together.ai/reference/post_images-generations
96
+ const { value: response, responseHeaders } = await postJsonToApi({
97
+ url: `${this.config.baseURL}/images/generations`,
98
+ headers: combineHeaders(this.config.headers(), headers),
99
+ body: {
100
+ model: this.modelId,
101
+ prompt,
102
+ seed,
103
+ ...(n > 1 ? { n } : {}),
104
+ ...(splitSize && {
105
+ width: parseInt(splitSize[0]),
106
+ height: parseInt(splitSize[1]),
107
+ }),
108
+ ...(imageUrl != null ? { image_url: imageUrl } : {}),
109
+ response_format: 'base64',
110
+ ...(togetheraiOptions ?? {}),
111
+ },
112
+ failedResponseHandler: createJsonErrorResponseHandler({
113
+ errorSchema: togetheraiErrorSchema,
114
+ errorToMessage: data => data.error.message,
115
+ }),
116
+ successfulResponseHandler: createJsonResponseHandler(
117
+ togetheraiImageResponseSchema,
118
+ ),
119
+ abortSignal,
120
+ fetch: this.config.fetch,
121
+ });
122
+
123
+ return {
124
+ images: response.data.map(item => item.b64_json),
125
+ warnings,
126
+ response: {
127
+ timestamp: currentDate,
128
+ modelId: this.modelId,
129
+ headers: responseHeaders,
130
+ },
131
+ };
132
+ }
133
+ }
134
+
135
+ // limited version of the schema, focussed on what is needed for the implementation
136
+ // this approach limits breakages when the API changes and increases efficiency
137
+ const togetheraiImageResponseSchema = z.object({
138
+ data: z.array(
139
+ z.object({
140
+ b64_json: z.string(),
141
+ }),
142
+ ),
143
+ });
144
+
145
+ // limited version of the schema, focussed on what is needed for the implementation
146
+ // this approach limits breakages when the API changes and increases efficiency
147
+ const togetheraiErrorSchema = z.object({
148
+ error: z.object({
149
+ message: z.string(),
150
+ }),
151
+ });
152
+
153
+ /**
154
+ * Provider options schema for Together AI image generation.
155
+ */
156
+ export const togetheraiImageProviderOptionsSchema = lazySchema(() =>
157
+ zodSchema(
158
+ z
159
+ .object({
160
+ /**
161
+ * Number of generation steps. Higher values can improve quality.
162
+ */
163
+ steps: z.number().nullish(),
164
+
165
+ /**
166
+ * Guidance scale for image generation.
167
+ */
168
+ guidance: z.number().nullish(),
169
+
170
+ /**
171
+ * Negative prompt to guide what to avoid.
172
+ */
173
+ negative_prompt: z.string().nullish(),
174
+
175
+ /**
176
+ * Disable the safety checker for image generation.
177
+ * When true, the API will not reject images flagged as potentially NSFW.
178
+ * Not available for Flux Schnell Free and Flux Pro models.
179
+ */
180
+ disable_safety_checker: z.boolean().nullish(),
181
+ })
182
+ .passthrough(),
183
+ ),
184
+ );
185
+
186
+ export type TogetherAIImageProviderOptions = InferSchema<
187
+ typeof togetheraiImageProviderOptionsSchema
188
+ >;
@@ -0,0 +1,18 @@
1
+ // https://api.together.ai/models
2
+ export type TogetherAIImageModelId =
3
+ // Text-to-image models
4
+ | 'stabilityai/stable-diffusion-xl-base-1.0'
5
+ | 'black-forest-labs/FLUX.1-dev'
6
+ | 'black-forest-labs/FLUX.1-dev-lora'
7
+ | 'black-forest-labs/FLUX.1-schnell'
8
+ | 'black-forest-labs/FLUX.1-canny'
9
+ | 'black-forest-labs/FLUX.1-depth'
10
+ | 'black-forest-labs/FLUX.1-redux'
11
+ | 'black-forest-labs/FLUX.1.1-pro'
12
+ | 'black-forest-labs/FLUX.1-pro'
13
+ | 'black-forest-labs/FLUX.1-schnell-Free'
14
+ // FLUX Kontext models for image editing
15
+ | 'black-forest-labs/FLUX.1-kontext-pro'
16
+ | 'black-forest-labs/FLUX.1-kontext-max'
17
+ | 'black-forest-labs/FLUX.1-kontext-dev'
18
+ | (string & {});
@@ -0,0 +1,196 @@
1
+ import {
2
+ OpenAICompatibleChatLanguageModel,
3
+ OpenAICompatibleCompletionLanguageModel,
4
+ OpenAICompatibleEmbeddingModel,
5
+ } from '@ai-sdk/openai-compatible';
6
+ import {
7
+ EmbeddingModelV3,
8
+ LanguageModelV3,
9
+ RerankingModelV3,
10
+ } from '@ai-sdk/provider';
11
+ import { loadApiKey } from '@ai-sdk/provider-utils';
12
+ import { beforeEach, describe, expect, it, Mock, vi } from 'vitest';
13
+ import { TogetherAIRerankingModel } from './reranking/togetherai-reranking-model';
14
+ import { TogetherAIImageModel } from './togetherai-image-model';
15
+ import { createTogetherAI } from './togetherai-provider';
16
+
17
+ // Add type assertion for the mocked class
18
+ const OpenAICompatibleChatLanguageModelMock =
19
+ OpenAICompatibleChatLanguageModel as unknown as Mock;
20
+
21
+ vi.mock('@ai-sdk/openai-compatible', () => ({
22
+ OpenAICompatibleChatLanguageModel: vi.fn(),
23
+ OpenAICompatibleCompletionLanguageModel: vi.fn(),
24
+ OpenAICompatibleEmbeddingModel: vi.fn(),
25
+ }));
26
+
27
+ vi.mock('@ai-sdk/provider-utils', async () => {
28
+ const actual = await vi.importActual('@ai-sdk/provider-utils');
29
+ return {
30
+ ...actual,
31
+ loadApiKey: vi.fn().mockReturnValue('mock-api-key'),
32
+ withoutTrailingSlash: vi.fn(url => url),
33
+ };
34
+ });
35
+
36
+ vi.mock('./togetherai-image-model', () => ({
37
+ TogetherAIImageModel: vi.fn(),
38
+ }));
39
+
40
+ vi.mock('./reranking/togetherai-reranking-model', () => ({
41
+ TogetherAIRerankingModel: vi.fn(),
42
+ }));
43
+
44
+ describe('TogetherAIProvider', () => {
45
+ let mockLanguageModel: LanguageModelV3;
46
+ let mockEmbeddingModel: EmbeddingModelV3;
47
+ let mockRerankingModel: RerankingModelV3;
48
+
49
+ beforeEach(() => {
50
+ // Mock implementations of models
51
+ mockLanguageModel = {
52
+ // Add any required methods for LanguageModelV3
53
+ } as LanguageModelV3;
54
+ mockEmbeddingModel = {
55
+ // Add any required methods for EmbeddingModelV3
56
+ } as EmbeddingModelV3;
57
+ mockRerankingModel = {
58
+ // Add any required methods for RerankingModelV3
59
+ } as RerankingModelV3;
60
+
61
+ // Reset mocks
62
+ vi.clearAllMocks();
63
+ });
64
+
65
+ describe('createTogetherAI', () => {
66
+ it('should create a TogetherAIProvider instance with default options', () => {
67
+ const provider = createTogetherAI();
68
+ const model = provider('model-id');
69
+
70
+ // Use the mocked version
71
+ const constructorCall =
72
+ OpenAICompatibleChatLanguageModelMock.mock.calls[0];
73
+ const config = constructorCall[1];
74
+ config.headers();
75
+
76
+ expect(loadApiKey).toHaveBeenCalledWith({
77
+ apiKey: undefined,
78
+ environmentVariableName: 'TOGETHER_AI_API_KEY',
79
+ description: 'TogetherAI',
80
+ });
81
+ });
82
+
83
+ it('should create a TogetherAIProvider instance with custom options', () => {
84
+ const options = {
85
+ apiKey: 'custom-key',
86
+ baseURL: 'https://custom.url',
87
+ headers: { 'Custom-Header': 'value' },
88
+ };
89
+ const provider = createTogetherAI(options);
90
+ const model = provider('model-id');
91
+
92
+ const constructorCall =
93
+ OpenAICompatibleChatLanguageModelMock.mock.calls[0];
94
+ const config = constructorCall[1];
95
+ config.headers();
96
+
97
+ expect(loadApiKey).toHaveBeenCalledWith({
98
+ apiKey: 'custom-key',
99
+ environmentVariableName: 'TOGETHER_AI_API_KEY',
100
+ description: 'TogetherAI',
101
+ });
102
+ });
103
+
104
+ it('should return a chat model when called as a function', () => {
105
+ const provider = createTogetherAI();
106
+ const modelId = 'foo-model-id';
107
+
108
+ const model = provider(modelId);
109
+ expect(model).toBeInstanceOf(OpenAICompatibleChatLanguageModel);
110
+ });
111
+ });
112
+
113
+ describe('chatModel', () => {
114
+ it('should construct a chat model with correct configuration', () => {
115
+ const provider = createTogetherAI();
116
+ const modelId = 'together-chat-model';
117
+
118
+ const model = provider.chatModel(modelId);
119
+
120
+ expect(model).toBeInstanceOf(OpenAICompatibleChatLanguageModel);
121
+ });
122
+ });
123
+
124
+ describe('completionModel', () => {
125
+ it('should construct a completion model with correct configuration', () => {
126
+ const provider = createTogetherAI();
127
+ const modelId = 'together-completion-model';
128
+
129
+ const model = provider.completionModel(modelId);
130
+
131
+ expect(model).toBeInstanceOf(OpenAICompatibleCompletionLanguageModel);
132
+ });
133
+ });
134
+
135
+ describe('embeddingModel', () => {
136
+ it('should construct a text embedding model with correct configuration', () => {
137
+ const provider = createTogetherAI();
138
+ const modelId = 'together-embedding-model';
139
+
140
+ const model = provider.embeddingModel(modelId);
141
+
142
+ expect(model).toBeInstanceOf(OpenAICompatibleEmbeddingModel);
143
+ });
144
+ });
145
+
146
+ describe('image', () => {
147
+ it('should construct an image model with correct configuration', () => {
148
+ const provider = createTogetherAI();
149
+ const modelId = 'stabilityai/stable-diffusion-xl';
150
+
151
+ const model = provider.image(modelId);
152
+
153
+ expect(TogetherAIImageModel).toHaveBeenCalledWith(
154
+ modelId,
155
+ expect.objectContaining({
156
+ provider: 'togetherai.image',
157
+ baseURL: 'https://api.together.xyz/v1/',
158
+ }),
159
+ );
160
+ expect(model).toBeInstanceOf(TogetherAIImageModel);
161
+ });
162
+
163
+ it('should pass custom baseURL to image model', () => {
164
+ const provider = createTogetherAI({
165
+ baseURL: 'https://custom.url/',
166
+ });
167
+ const modelId = 'stabilityai/stable-diffusion-xl';
168
+
169
+ provider.image(modelId);
170
+
171
+ expect(TogetherAIImageModel).toHaveBeenCalledWith(
172
+ modelId,
173
+ expect.objectContaining({
174
+ baseURL: 'https://custom.url/',
175
+ }),
176
+ );
177
+ });
178
+ });
179
+
180
+ describe('rerankingModel', () => {
181
+ it('should construct a reranking model with correct configuration', () => {
182
+ const provider = createTogetherAI();
183
+ const modelId = 'Salesforce/Llama-Rank-v1';
184
+ 0;
185
+ const model = provider.rerankingModel(modelId);
186
+
187
+ expect(TogetherAIRerankingModel).toHaveBeenCalledWith(
188
+ modelId,
189
+ expect.objectContaining({
190
+ baseURL: 'https://api.together.xyz/v1/',
191
+ }),
192
+ );
193
+ expect(model).toBeInstanceOf(TogetherAIRerankingModel);
194
+ });
195
+ });
196
+ });
@@ -0,0 +1,180 @@
1
+ import {
2
+ OpenAICompatibleChatLanguageModel,
3
+ OpenAICompatibleCompletionLanguageModel,
4
+ OpenAICompatibleEmbeddingModel,
5
+ } from '@ai-sdk/openai-compatible';
6
+ import {
7
+ EmbeddingModelV3,
8
+ ImageModelV3,
9
+ LanguageModelV3,
10
+ ProviderV3,
11
+ RerankingModelV3,
12
+ } from '@ai-sdk/provider';
13
+ import {
14
+ FetchFunction,
15
+ loadApiKey,
16
+ withoutTrailingSlash,
17
+ withUserAgentSuffix,
18
+ } from '@ai-sdk/provider-utils';
19
+ import { TogetherAIRerankingModel } from './reranking/togetherai-reranking-model';
20
+ import { TogetherAIRerankingModelId } from './reranking/togetherai-reranking-options';
21
+ import { TogetherAIChatModelId } from './togetherai-chat-options';
22
+ import { TogetherAICompletionModelId } from './togetherai-completion-options';
23
+ import { TogetherAIEmbeddingModelId } from './togetherai-embedding-options';
24
+ import { TogetherAIImageModel } from './togetherai-image-model';
25
+ import { TogetherAIImageModelId } from './togetherai-image-settings';
26
+ import { VERSION } from './version';
27
+
28
+ export interface TogetherAIProviderSettings {
29
+ /**
30
+ TogetherAI API key.
31
+ */
32
+ apiKey?: string;
33
+ /**
34
+ Base URL for the API calls.
35
+ */
36
+ baseURL?: string;
37
+ /**
38
+ Custom headers to include in the requests.
39
+ */
40
+ headers?: Record<string, string>;
41
+ /**
42
+ Custom fetch implementation. You can use it as a middleware to intercept requests,
43
+ or to provide a custom fetch implementation for e.g. testing.
44
+ */
45
+ fetch?: FetchFunction;
46
+ }
47
+
48
+ export interface TogetherAIProvider extends ProviderV3 {
49
+ /**
50
+ Creates a model for text generation.
51
+ */
52
+ (modelId: TogetherAIChatModelId): LanguageModelV3;
53
+
54
+ /**
55
+ Creates a chat model for text generation.
56
+ */
57
+ chatModel(modelId: TogetherAIChatModelId): LanguageModelV3;
58
+
59
+ /**
60
+ Creates a chat model for text generation.
61
+ */
62
+ languageModel(modelId: TogetherAIChatModelId): LanguageModelV3;
63
+
64
+ /**
65
+ Creates a completion model for text generation.
66
+ */
67
+ completionModel(modelId: TogetherAICompletionModelId): LanguageModelV3;
68
+
69
+ /**
70
+ Creates a text embedding model for text generation.
71
+ */
72
+ embeddingModel(modelId: TogetherAIEmbeddingModelId): EmbeddingModelV3;
73
+
74
+ /**
75
+ * @deprecated Use `embeddingModel` instead.
76
+ */
77
+ textEmbeddingModel(modelId: TogetherAIEmbeddingModelId): EmbeddingModelV3;
78
+
79
+ /**
80
+ Creates a model for image generation.
81
+ */
82
+ image(modelId: TogetherAIImageModelId): ImageModelV3;
83
+
84
+ /**
85
+ Creates a model for image generation.
86
+ */
87
+ imageModel(modelId: TogetherAIImageModelId): ImageModelV3;
88
+
89
+ /**
90
+ * Creates a model for reranking.
91
+ */
92
+ reranking(modelId: TogetherAIRerankingModelId): RerankingModelV3;
93
+
94
+ /**
95
+ * Creates a model for reranking.
96
+ */
97
+ rerankingModel(modelId: TogetherAIRerankingModelId): RerankingModelV3;
98
+ }
99
+
100
+ export function createTogetherAI(
101
+ options: TogetherAIProviderSettings = {},
102
+ ): TogetherAIProvider {
103
+ const baseURL = withoutTrailingSlash(
104
+ options.baseURL ?? 'https://api.together.xyz/v1/',
105
+ );
106
+ const getHeaders = () =>
107
+ withUserAgentSuffix(
108
+ {
109
+ Authorization: `Bearer ${loadApiKey({
110
+ apiKey: options.apiKey,
111
+ environmentVariableName: 'TOGETHER_AI_API_KEY',
112
+ description: 'TogetherAI',
113
+ })}`,
114
+ ...options.headers,
115
+ },
116
+ `ai-sdk/togetherai/${VERSION}`,
117
+ );
118
+
119
+ interface CommonModelConfig {
120
+ provider: string;
121
+ url: ({ path }: { path: string }) => string;
122
+ headers: () => Record<string, string>;
123
+ fetch?: FetchFunction;
124
+ }
125
+
126
+ const getCommonModelConfig = (modelType: string): CommonModelConfig => ({
127
+ provider: `togetherai.${modelType}`,
128
+ url: ({ path }) => `${baseURL}${path}`,
129
+ headers: getHeaders,
130
+ fetch: options.fetch,
131
+ });
132
+
133
+ const createChatModel = (modelId: TogetherAIChatModelId) => {
134
+ return new OpenAICompatibleChatLanguageModel(
135
+ modelId,
136
+ getCommonModelConfig('chat'),
137
+ );
138
+ };
139
+
140
+ const createCompletionModel = (modelId: TogetherAICompletionModelId) =>
141
+ new OpenAICompatibleCompletionLanguageModel(
142
+ modelId,
143
+ getCommonModelConfig('completion'),
144
+ );
145
+
146
+ const createEmbeddingModel = (modelId: TogetherAIEmbeddingModelId) =>
147
+ new OpenAICompatibleEmbeddingModel(
148
+ modelId,
149
+ getCommonModelConfig('embedding'),
150
+ );
151
+
152
+ const createImageModel = (modelId: TogetherAIImageModelId) =>
153
+ new TogetherAIImageModel(modelId, {
154
+ ...getCommonModelConfig('image'),
155
+ baseURL: baseURL ?? 'https://api.together.xyz/v1/',
156
+ });
157
+
158
+ const createRerankingModel = (modelId: TogetherAIRerankingModelId) =>
159
+ new TogetherAIRerankingModel(modelId, {
160
+ ...getCommonModelConfig('reranking'),
161
+ baseURL: baseURL ?? 'https://api.together.xyz/v1/',
162
+ });
163
+
164
+ const provider = (modelId: TogetherAIChatModelId) => createChatModel(modelId);
165
+
166
+ provider.specificationVersion = 'v3' as const;
167
+ provider.completionModel = createCompletionModel;
168
+ provider.languageModel = createChatModel;
169
+ provider.chatModel = createChatModel;
170
+ provider.embeddingModel = createEmbeddingModel;
171
+ provider.textEmbeddingModel = createEmbeddingModel;
172
+ provider.image = createImageModel;
173
+ provider.imageModel = createImageModel;
174
+ provider.reranking = createRerankingModel;
175
+ provider.rerankingModel = createRerankingModel;
176
+
177
+ return provider;
178
+ }
179
+
180
+ export const togetherai = createTogetherAI();
package/src/version.ts ADDED
@@ -0,0 +1,6 @@
1
+ // Version string of this package injected at build time.
2
+ declare const __PACKAGE_VERSION__: string | undefined;
3
+ export const VERSION: string =
4
+ typeof __PACKAGE_VERSION__ !== 'undefined'
5
+ ? __PACKAGE_VERSION__
6
+ : '0.0.0-test';