@ai-sdk/togetherai 0.0.0-70e0935a-20260114150030 → 0.0.0-98261322-20260122142521

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,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';