@yourgpt/llm-sdk 2.1.6 → 2.1.8
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/dist/adapters/index.js +38 -0
- package/dist/adapters/index.mjs +38 -0
- package/dist/index.js +4 -1
- package/dist/index.mjs +4 -1
- package/dist/providers/azure/index.js +11 -0
- package/dist/providers/azure/index.mjs +11 -0
- package/dist/providers/fireworks/index.d.mts +49 -0
- package/dist/providers/fireworks/index.d.ts +49 -0
- package/dist/providers/fireworks/index.js +205 -0
- package/dist/providers/fireworks/index.mjs +202 -0
- package/dist/providers/google/index.js +11 -0
- package/dist/providers/google/index.mjs +11 -0
- package/dist/providers/ollama/index.js +5 -0
- package/dist/providers/ollama/index.mjs +5 -0
- package/dist/providers/openai/index.js +11 -0
- package/dist/providers/openai/index.mjs +11 -0
- package/dist/providers/openrouter/index.js +11 -0
- package/dist/providers/openrouter/index.mjs +11 -0
- package/dist/providers/togetherai/index.d.mts +53 -0
- package/dist/providers/togetherai/index.d.ts +53 -0
- package/dist/providers/togetherai/index.js +205 -0
- package/dist/providers/togetherai/index.mjs +202 -0
- package/dist/providers/xai/index.js +11 -0
- package/dist/providers/xai/index.mjs +11 -0
- package/package.json +14 -1
package/dist/adapters/index.js
CHANGED
|
@@ -667,6 +667,11 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
667
667
|
id: currentToolCall.id,
|
|
668
668
|
args: currentToolCall.arguments
|
|
669
669
|
};
|
|
670
|
+
yield {
|
|
671
|
+
type: "action:end",
|
|
672
|
+
id: currentToolCall.id,
|
|
673
|
+
name: currentToolCall.name
|
|
674
|
+
};
|
|
670
675
|
}
|
|
671
676
|
const tcExtraContent = toolCall.extra_content;
|
|
672
677
|
currentToolCall = {
|
|
@@ -700,6 +705,12 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
700
705
|
id: currentToolCall.id,
|
|
701
706
|
args: currentToolCall.arguments
|
|
702
707
|
};
|
|
708
|
+
yield {
|
|
709
|
+
type: "action:end",
|
|
710
|
+
id: currentToolCall.id,
|
|
711
|
+
name: currentToolCall.name
|
|
712
|
+
};
|
|
713
|
+
currentToolCall = null;
|
|
703
714
|
}
|
|
704
715
|
}
|
|
705
716
|
}
|
|
@@ -1537,6 +1548,11 @@ var OllamaAdapter = class {
|
|
|
1537
1548
|
id: toolCallId,
|
|
1538
1549
|
args: JSON.stringify(toolCall.function?.arguments || {})
|
|
1539
1550
|
};
|
|
1551
|
+
yield {
|
|
1552
|
+
type: "action:end",
|
|
1553
|
+
id: toolCallId,
|
|
1554
|
+
name: toolCall.function?.name || ""
|
|
1555
|
+
};
|
|
1540
1556
|
}
|
|
1541
1557
|
hasEmittedToolCalls = true;
|
|
1542
1558
|
}
|
|
@@ -1818,6 +1834,11 @@ var GoogleAdapter = class {
|
|
|
1818
1834
|
id: currentToolCall.id,
|
|
1819
1835
|
args: JSON.stringify(currentToolCall.args)
|
|
1820
1836
|
};
|
|
1837
|
+
yield {
|
|
1838
|
+
type: "action:end",
|
|
1839
|
+
id: currentToolCall.id,
|
|
1840
|
+
name: currentToolCall.name
|
|
1841
|
+
};
|
|
1821
1842
|
}
|
|
1822
1843
|
currentToolCall = {
|
|
1823
1844
|
id: toolId,
|
|
@@ -1838,6 +1859,12 @@ var GoogleAdapter = class {
|
|
|
1838
1859
|
id: currentToolCall.id,
|
|
1839
1860
|
args: JSON.stringify(currentToolCall.args)
|
|
1840
1861
|
};
|
|
1862
|
+
yield {
|
|
1863
|
+
type: "action:end",
|
|
1864
|
+
id: currentToolCall.id,
|
|
1865
|
+
name: currentToolCall.name
|
|
1866
|
+
};
|
|
1867
|
+
currentToolCall = null;
|
|
1841
1868
|
}
|
|
1842
1869
|
}
|
|
1843
1870
|
const groundingMetadata = candidate?.groundingMetadata;
|
|
@@ -2127,6 +2154,11 @@ var AzureAdapter = class {
|
|
|
2127
2154
|
id: currentToolCall.id,
|
|
2128
2155
|
args: currentToolCall.arguments
|
|
2129
2156
|
};
|
|
2157
|
+
yield {
|
|
2158
|
+
type: "action:end",
|
|
2159
|
+
id: currentToolCall.id,
|
|
2160
|
+
name: currentToolCall.name
|
|
2161
|
+
};
|
|
2130
2162
|
}
|
|
2131
2163
|
currentToolCall = {
|
|
2132
2164
|
id: toolCall.id,
|
|
@@ -2150,6 +2182,12 @@ var AzureAdapter = class {
|
|
|
2150
2182
|
id: currentToolCall.id,
|
|
2151
2183
|
args: currentToolCall.arguments
|
|
2152
2184
|
};
|
|
2185
|
+
yield {
|
|
2186
|
+
type: "action:end",
|
|
2187
|
+
id: currentToolCall.id,
|
|
2188
|
+
name: currentToolCall.name
|
|
2189
|
+
};
|
|
2190
|
+
currentToolCall = null;
|
|
2153
2191
|
}
|
|
2154
2192
|
}
|
|
2155
2193
|
}
|
package/dist/adapters/index.mjs
CHANGED
|
@@ -665,6 +665,11 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
665
665
|
id: currentToolCall.id,
|
|
666
666
|
args: currentToolCall.arguments
|
|
667
667
|
};
|
|
668
|
+
yield {
|
|
669
|
+
type: "action:end",
|
|
670
|
+
id: currentToolCall.id,
|
|
671
|
+
name: currentToolCall.name
|
|
672
|
+
};
|
|
668
673
|
}
|
|
669
674
|
const tcExtraContent = toolCall.extra_content;
|
|
670
675
|
currentToolCall = {
|
|
@@ -698,6 +703,12 @@ var OpenAIAdapter = class _OpenAIAdapter {
|
|
|
698
703
|
id: currentToolCall.id,
|
|
699
704
|
args: currentToolCall.arguments
|
|
700
705
|
};
|
|
706
|
+
yield {
|
|
707
|
+
type: "action:end",
|
|
708
|
+
id: currentToolCall.id,
|
|
709
|
+
name: currentToolCall.name
|
|
710
|
+
};
|
|
711
|
+
currentToolCall = null;
|
|
701
712
|
}
|
|
702
713
|
}
|
|
703
714
|
}
|
|
@@ -1535,6 +1546,11 @@ var OllamaAdapter = class {
|
|
|
1535
1546
|
id: toolCallId,
|
|
1536
1547
|
args: JSON.stringify(toolCall.function?.arguments || {})
|
|
1537
1548
|
};
|
|
1549
|
+
yield {
|
|
1550
|
+
type: "action:end",
|
|
1551
|
+
id: toolCallId,
|
|
1552
|
+
name: toolCall.function?.name || ""
|
|
1553
|
+
};
|
|
1538
1554
|
}
|
|
1539
1555
|
hasEmittedToolCalls = true;
|
|
1540
1556
|
}
|
|
@@ -1816,6 +1832,11 @@ var GoogleAdapter = class {
|
|
|
1816
1832
|
id: currentToolCall.id,
|
|
1817
1833
|
args: JSON.stringify(currentToolCall.args)
|
|
1818
1834
|
};
|
|
1835
|
+
yield {
|
|
1836
|
+
type: "action:end",
|
|
1837
|
+
id: currentToolCall.id,
|
|
1838
|
+
name: currentToolCall.name
|
|
1839
|
+
};
|
|
1819
1840
|
}
|
|
1820
1841
|
currentToolCall = {
|
|
1821
1842
|
id: toolId,
|
|
@@ -1836,6 +1857,12 @@ var GoogleAdapter = class {
|
|
|
1836
1857
|
id: currentToolCall.id,
|
|
1837
1858
|
args: JSON.stringify(currentToolCall.args)
|
|
1838
1859
|
};
|
|
1860
|
+
yield {
|
|
1861
|
+
type: "action:end",
|
|
1862
|
+
id: currentToolCall.id,
|
|
1863
|
+
name: currentToolCall.name
|
|
1864
|
+
};
|
|
1865
|
+
currentToolCall = null;
|
|
1839
1866
|
}
|
|
1840
1867
|
}
|
|
1841
1868
|
const groundingMetadata = candidate?.groundingMetadata;
|
|
@@ -2125,6 +2152,11 @@ var AzureAdapter = class {
|
|
|
2125
2152
|
id: currentToolCall.id,
|
|
2126
2153
|
args: currentToolCall.arguments
|
|
2127
2154
|
};
|
|
2155
|
+
yield {
|
|
2156
|
+
type: "action:end",
|
|
2157
|
+
id: currentToolCall.id,
|
|
2158
|
+
name: currentToolCall.name
|
|
2159
|
+
};
|
|
2128
2160
|
}
|
|
2129
2161
|
currentToolCall = {
|
|
2130
2162
|
id: toolCall.id,
|
|
@@ -2148,6 +2180,12 @@ var AzureAdapter = class {
|
|
|
2148
2180
|
id: currentToolCall.id,
|
|
2149
2181
|
args: currentToolCall.arguments
|
|
2150
2182
|
};
|
|
2183
|
+
yield {
|
|
2184
|
+
type: "action:end",
|
|
2185
|
+
id: currentToolCall.id,
|
|
2186
|
+
name: currentToolCall.name
|
|
2187
|
+
};
|
|
2188
|
+
currentToolCall = null;
|
|
2151
2189
|
}
|
|
2152
2190
|
}
|
|
2153
2191
|
}
|
package/dist/index.js
CHANGED
|
@@ -2838,7 +2838,10 @@ var Runtime = class {
|
|
|
2838
2838
|
}
|
|
2839
2839
|
}
|
|
2840
2840
|
if (resolvedThreadId) {
|
|
2841
|
-
yield {
|
|
2841
|
+
yield {
|
|
2842
|
+
type: "thread:created",
|
|
2843
|
+
threadId: resolvedThreadId
|
|
2844
|
+
};
|
|
2842
2845
|
}
|
|
2843
2846
|
if (resolvedThreadId && storageHealthy) {
|
|
2844
2847
|
try {
|
package/dist/index.mjs
CHANGED
|
@@ -2836,7 +2836,10 @@ var Runtime = class {
|
|
|
2836
2836
|
}
|
|
2837
2837
|
}
|
|
2838
2838
|
if (resolvedThreadId) {
|
|
2839
|
-
yield {
|
|
2839
|
+
yield {
|
|
2840
|
+
type: "thread:created",
|
|
2841
|
+
threadId: resolvedThreadId
|
|
2842
|
+
};
|
|
2840
2843
|
}
|
|
2841
2844
|
if (resolvedThreadId && storageHealthy) {
|
|
2842
2845
|
try {
|
|
@@ -279,6 +279,11 @@ var AzureAdapter = class {
|
|
|
279
279
|
id: currentToolCall.id,
|
|
280
280
|
args: currentToolCall.arguments
|
|
281
281
|
};
|
|
282
|
+
yield {
|
|
283
|
+
type: "action:end",
|
|
284
|
+
id: currentToolCall.id,
|
|
285
|
+
name: currentToolCall.name
|
|
286
|
+
};
|
|
282
287
|
}
|
|
283
288
|
currentToolCall = {
|
|
284
289
|
id: toolCall.id,
|
|
@@ -302,6 +307,12 @@ var AzureAdapter = class {
|
|
|
302
307
|
id: currentToolCall.id,
|
|
303
308
|
args: currentToolCall.arguments
|
|
304
309
|
};
|
|
310
|
+
yield {
|
|
311
|
+
type: "action:end",
|
|
312
|
+
id: currentToolCall.id,
|
|
313
|
+
name: currentToolCall.name
|
|
314
|
+
};
|
|
315
|
+
currentToolCall = null;
|
|
305
316
|
}
|
|
306
317
|
}
|
|
307
318
|
}
|
|
@@ -277,6 +277,11 @@ var AzureAdapter = class {
|
|
|
277
277
|
id: currentToolCall.id,
|
|
278
278
|
args: currentToolCall.arguments
|
|
279
279
|
};
|
|
280
|
+
yield {
|
|
281
|
+
type: "action:end",
|
|
282
|
+
id: currentToolCall.id,
|
|
283
|
+
name: currentToolCall.name
|
|
284
|
+
};
|
|
280
285
|
}
|
|
281
286
|
currentToolCall = {
|
|
282
287
|
id: toolCall.id,
|
|
@@ -300,6 +305,12 @@ var AzureAdapter = class {
|
|
|
300
305
|
id: currentToolCall.id,
|
|
301
306
|
args: currentToolCall.arguments
|
|
302
307
|
};
|
|
308
|
+
yield {
|
|
309
|
+
type: "action:end",
|
|
310
|
+
id: currentToolCall.id,
|
|
311
|
+
name: currentToolCall.name
|
|
312
|
+
};
|
|
313
|
+
currentToolCall = null;
|
|
303
314
|
}
|
|
304
315
|
}
|
|
305
316
|
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { L as LanguageModel } from '../../types-CR8mi9I0.mjs';
|
|
2
|
+
import 'zod';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Fireworks Provider
|
|
6
|
+
*
|
|
7
|
+
* Fireworks.ai is a high-performance inference platform for open-source models.
|
|
8
|
+
* It uses an OpenAI-compatible REST API.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { fireworks } from '@yourgpt/llm-sdk/fireworks';
|
|
13
|
+
* import { generateText } from '@yourgpt/llm-sdk';
|
|
14
|
+
*
|
|
15
|
+
* const result = await generateText({
|
|
16
|
+
* model: fireworks('accounts/fireworks/models/llama-v3p1-70b-instruct'),
|
|
17
|
+
* prompt: 'Hello!',
|
|
18
|
+
* });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
interface FireworksProviderOptions {
|
|
23
|
+
/** API key (defaults to FIREWORKS_API_KEY env var) */
|
|
24
|
+
apiKey?: string;
|
|
25
|
+
/** Base URL for API (defaults to https://api.fireworks.ai/inference/v1) */
|
|
26
|
+
baseURL?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create a Fireworks language model.
|
|
30
|
+
*
|
|
31
|
+
* Model IDs follow the format `accounts/fireworks/models/{model-name}`.
|
|
32
|
+
*
|
|
33
|
+
* @param modelId - Full model ID or shorthand (e.g. 'accounts/fireworks/models/llama-v3p1-70b-instruct')
|
|
34
|
+
* @param options - Provider options
|
|
35
|
+
* @returns LanguageModel instance
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const model = fireworks('accounts/fireworks/models/llama-v3p1-70b-instruct');
|
|
40
|
+
*
|
|
41
|
+
* // With explicit API key
|
|
42
|
+
* const model = fireworks('accounts/fireworks/models/deepseek-v3', {
|
|
43
|
+
* apiKey: 'fw_...',
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
declare function fireworks(modelId: string, options?: FireworksProviderOptions): LanguageModel;
|
|
48
|
+
|
|
49
|
+
export { type FireworksProviderOptions, fireworks as createFireworks, fireworks };
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { L as LanguageModel } from '../../types-CR8mi9I0.js';
|
|
2
|
+
import 'zod';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Fireworks Provider
|
|
6
|
+
*
|
|
7
|
+
* Fireworks.ai is a high-performance inference platform for open-source models.
|
|
8
|
+
* It uses an OpenAI-compatible REST API.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { fireworks } from '@yourgpt/llm-sdk/fireworks';
|
|
13
|
+
* import { generateText } from '@yourgpt/llm-sdk';
|
|
14
|
+
*
|
|
15
|
+
* const result = await generateText({
|
|
16
|
+
* model: fireworks('accounts/fireworks/models/llama-v3p1-70b-instruct'),
|
|
17
|
+
* prompt: 'Hello!',
|
|
18
|
+
* });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
interface FireworksProviderOptions {
|
|
23
|
+
/** API key (defaults to FIREWORKS_API_KEY env var) */
|
|
24
|
+
apiKey?: string;
|
|
25
|
+
/** Base URL for API (defaults to https://api.fireworks.ai/inference/v1) */
|
|
26
|
+
baseURL?: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create a Fireworks language model.
|
|
30
|
+
*
|
|
31
|
+
* Model IDs follow the format `accounts/fireworks/models/{model-name}`.
|
|
32
|
+
*
|
|
33
|
+
* @param modelId - Full model ID or shorthand (e.g. 'accounts/fireworks/models/llama-v3p1-70b-instruct')
|
|
34
|
+
* @param options - Provider options
|
|
35
|
+
* @returns LanguageModel instance
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const model = fireworks('accounts/fireworks/models/llama-v3p1-70b-instruct');
|
|
40
|
+
*
|
|
41
|
+
* // With explicit API key
|
|
42
|
+
* const model = fireworks('accounts/fireworks/models/deepseek-v3', {
|
|
43
|
+
* apiKey: 'fw_...',
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
declare function fireworks(modelId: string, options?: FireworksProviderOptions): LanguageModel;
|
|
48
|
+
|
|
49
|
+
export { type FireworksProviderOptions, fireworks as createFireworks, fireworks };
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/providers/fireworks/provider.ts
|
|
4
|
+
function fireworks(modelId, options = {}) {
|
|
5
|
+
const apiKey = options.apiKey ?? process.env.FIREWORKS_API_KEY;
|
|
6
|
+
const baseURL = options.baseURL ?? "https://api.fireworks.ai/inference/v1";
|
|
7
|
+
let client = null;
|
|
8
|
+
async function getClient() {
|
|
9
|
+
if (!client) {
|
|
10
|
+
const { default: OpenAI } = await import('openai');
|
|
11
|
+
client = new OpenAI({ apiKey, baseURL });
|
|
12
|
+
}
|
|
13
|
+
return client;
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
provider: "fireworks",
|
|
17
|
+
modelId,
|
|
18
|
+
capabilities: {
|
|
19
|
+
supportsVision: false,
|
|
20
|
+
supportsTools: true,
|
|
21
|
+
supportsStreaming: true,
|
|
22
|
+
supportsJsonMode: true,
|
|
23
|
+
supportsThinking: false,
|
|
24
|
+
supportsPDF: false,
|
|
25
|
+
maxTokens: 131072,
|
|
26
|
+
supportedImageTypes: []
|
|
27
|
+
},
|
|
28
|
+
async doGenerate(params) {
|
|
29
|
+
const client2 = await getClient();
|
|
30
|
+
const messages = formatMessages(params.messages);
|
|
31
|
+
const requestBody = {
|
|
32
|
+
model: modelId,
|
|
33
|
+
messages,
|
|
34
|
+
temperature: params.temperature,
|
|
35
|
+
max_tokens: params.maxTokens
|
|
36
|
+
};
|
|
37
|
+
if (params.tools) {
|
|
38
|
+
requestBody.tools = params.tools;
|
|
39
|
+
}
|
|
40
|
+
const response = await client2.chat.completions.create(requestBody);
|
|
41
|
+
const choice = response.choices[0];
|
|
42
|
+
const message = choice.message;
|
|
43
|
+
const toolCalls = (message.tool_calls ?? []).map(
|
|
44
|
+
(tc) => ({
|
|
45
|
+
id: tc.id,
|
|
46
|
+
name: tc.function.name,
|
|
47
|
+
args: JSON.parse(tc.function.arguments || "{}")
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
return {
|
|
51
|
+
text: message.content ?? "",
|
|
52
|
+
toolCalls,
|
|
53
|
+
finishReason: mapFinishReason(choice.finish_reason),
|
|
54
|
+
usage: {
|
|
55
|
+
promptTokens: response.usage?.prompt_tokens ?? 0,
|
|
56
|
+
completionTokens: response.usage?.completion_tokens ?? 0,
|
|
57
|
+
totalTokens: response.usage?.total_tokens ?? 0
|
|
58
|
+
},
|
|
59
|
+
rawResponse: response
|
|
60
|
+
};
|
|
61
|
+
},
|
|
62
|
+
async *doStream(params) {
|
|
63
|
+
const client2 = await getClient();
|
|
64
|
+
const messages = formatMessages(params.messages);
|
|
65
|
+
const requestBody = {
|
|
66
|
+
model: modelId,
|
|
67
|
+
messages,
|
|
68
|
+
temperature: params.temperature,
|
|
69
|
+
max_tokens: params.maxTokens,
|
|
70
|
+
stream: true
|
|
71
|
+
};
|
|
72
|
+
if (params.tools) {
|
|
73
|
+
requestBody.tools = params.tools;
|
|
74
|
+
}
|
|
75
|
+
const stream = await client2.chat.completions.create(requestBody);
|
|
76
|
+
const toolCallMap = /* @__PURE__ */ new Map();
|
|
77
|
+
let totalPromptTokens = 0;
|
|
78
|
+
let totalCompletionTokens = 0;
|
|
79
|
+
for await (const chunk of stream) {
|
|
80
|
+
if (params.signal?.aborted) {
|
|
81
|
+
yield { type: "error", error: new Error("Aborted") };
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const choice = chunk.choices[0];
|
|
85
|
+
const delta = choice?.delta;
|
|
86
|
+
if (delta?.content) {
|
|
87
|
+
yield { type: "text-delta", text: delta.content };
|
|
88
|
+
}
|
|
89
|
+
if (delta?.tool_calls) {
|
|
90
|
+
for (const tc of delta.tool_calls) {
|
|
91
|
+
const idx = tc.index ?? 0;
|
|
92
|
+
if (!toolCallMap.has(idx)) {
|
|
93
|
+
toolCallMap.set(idx, {
|
|
94
|
+
id: tc.id ?? "",
|
|
95
|
+
name: tc.function?.name ?? "",
|
|
96
|
+
arguments: tc.function?.arguments ?? ""
|
|
97
|
+
});
|
|
98
|
+
} else {
|
|
99
|
+
const existing = toolCallMap.get(idx);
|
|
100
|
+
if (tc.id && !existing.id) existing.id = tc.id;
|
|
101
|
+
if (tc.function?.name && !existing.name)
|
|
102
|
+
existing.name = tc.function.name;
|
|
103
|
+
if (tc.function?.arguments)
|
|
104
|
+
existing.arguments += tc.function.arguments;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (choice?.finish_reason) {
|
|
109
|
+
for (const [, tc] of toolCallMap) {
|
|
110
|
+
yield {
|
|
111
|
+
type: "tool-call",
|
|
112
|
+
toolCall: {
|
|
113
|
+
id: tc.id,
|
|
114
|
+
name: tc.name,
|
|
115
|
+
args: JSON.parse(tc.arguments || "{}")
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
toolCallMap.clear();
|
|
120
|
+
if (chunk.usage) {
|
|
121
|
+
totalPromptTokens = chunk.usage.prompt_tokens;
|
|
122
|
+
totalCompletionTokens = chunk.usage.completion_tokens;
|
|
123
|
+
}
|
|
124
|
+
yield {
|
|
125
|
+
type: "finish",
|
|
126
|
+
finishReason: mapFinishReason(choice.finish_reason),
|
|
127
|
+
usage: {
|
|
128
|
+
promptTokens: totalPromptTokens,
|
|
129
|
+
completionTokens: totalCompletionTokens,
|
|
130
|
+
totalTokens: totalPromptTokens + totalCompletionTokens
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function mapFinishReason(reason) {
|
|
139
|
+
switch (reason) {
|
|
140
|
+
case "stop":
|
|
141
|
+
return "stop";
|
|
142
|
+
case "length":
|
|
143
|
+
return "length";
|
|
144
|
+
case "tool_calls":
|
|
145
|
+
case "function_call":
|
|
146
|
+
return "tool-calls";
|
|
147
|
+
case "content_filter":
|
|
148
|
+
return "content-filter";
|
|
149
|
+
default:
|
|
150
|
+
return "unknown";
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function formatMessages(messages) {
|
|
154
|
+
return messages.map((msg) => {
|
|
155
|
+
switch (msg.role) {
|
|
156
|
+
case "system":
|
|
157
|
+
return { role: "system", content: msg.content };
|
|
158
|
+
case "user":
|
|
159
|
+
if (typeof msg.content === "string") {
|
|
160
|
+
return { role: "user", content: msg.content };
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
role: "user",
|
|
164
|
+
content: msg.content.map((part) => {
|
|
165
|
+
if (part.type === "text") {
|
|
166
|
+
return { type: "text", text: part.text };
|
|
167
|
+
}
|
|
168
|
+
if (part.type === "image") {
|
|
169
|
+
const imageData = typeof part.image === "string" ? part.image : Buffer.from(part.image).toString("base64");
|
|
170
|
+
const url = imageData.startsWith("data:") ? imageData : `data:${part.mimeType ?? "image/png"};base64,${imageData}`;
|
|
171
|
+
return { type: "image_url", image_url: { url, detail: "auto" } };
|
|
172
|
+
}
|
|
173
|
+
return { type: "text", text: "" };
|
|
174
|
+
})
|
|
175
|
+
};
|
|
176
|
+
case "assistant": {
|
|
177
|
+
const assistantMsg = { role: "assistant", content: msg.content };
|
|
178
|
+
if (msg.toolCalls && msg.toolCalls.length > 0) {
|
|
179
|
+
assistantMsg.tool_calls = msg.toolCalls.map((tc) => ({
|
|
180
|
+
id: tc.id,
|
|
181
|
+
type: "function",
|
|
182
|
+
function: {
|
|
183
|
+
name: tc.name,
|
|
184
|
+
arguments: JSON.stringify(tc.args)
|
|
185
|
+
}
|
|
186
|
+
}));
|
|
187
|
+
}
|
|
188
|
+
return assistantMsg;
|
|
189
|
+
}
|
|
190
|
+
case "tool":
|
|
191
|
+
return {
|
|
192
|
+
role: "tool",
|
|
193
|
+
tool_call_id: msg.toolCallId,
|
|
194
|
+
content: msg.content
|
|
195
|
+
};
|
|
196
|
+
default:
|
|
197
|
+
return msg;
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
exports.createFireworks = fireworks;
|
|
203
|
+
exports.fireworks = fireworks;
|
|
204
|
+
//# sourceMappingURL=index.js.map
|
|
205
|
+
//# sourceMappingURL=index.js.map
|