@crewai-ts/core 0.1.6 → 0.1.11
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/index.cjs +362 -16
- package/dist/index.js +362 -16
- package/dist/openai-completion.d.ts +3 -0
- package/dist/provider-completions.d.ts +3 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -30289,6 +30289,28 @@ function createLLM(llmValue = null, env = process.env) {
|
|
|
30289
30289
|
if (apiBase !== void 0) {
|
|
30290
30290
|
options.api_base = apiBase;
|
|
30291
30291
|
}
|
|
30292
|
+
const providerOptions = options;
|
|
30293
|
+
const llmOptions = llmValue;
|
|
30294
|
+
for (const key of [
|
|
30295
|
+
"api",
|
|
30296
|
+
"instructions",
|
|
30297
|
+
"store",
|
|
30298
|
+
"previousResponseId",
|
|
30299
|
+
"previous_response_id",
|
|
30300
|
+
"include",
|
|
30301
|
+
"builtinTools",
|
|
30302
|
+
"builtin_tools",
|
|
30303
|
+
"parseToolOutputs",
|
|
30304
|
+
"parse_tool_outputs",
|
|
30305
|
+
"autoChain",
|
|
30306
|
+
"auto_chain",
|
|
30307
|
+
"autoChainReasoning",
|
|
30308
|
+
"auto_chain_reasoning"
|
|
30309
|
+
]) {
|
|
30310
|
+
if (llmOptions[key] !== void 0) {
|
|
30311
|
+
providerOptions[key] = llmOptions[key];
|
|
30312
|
+
}
|
|
30313
|
+
}
|
|
30292
30314
|
return createConfiguredLLM(options);
|
|
30293
30315
|
}
|
|
30294
30316
|
var create_llm = createLLM;
|
|
@@ -45517,8 +45539,13 @@ var OpenAICompletion = class _OpenAICompletion extends ConfiguredLLM {
|
|
|
45517
45539
|
this.responseChainId = this.previousResponseId;
|
|
45518
45540
|
this.reasoningChainItems = null;
|
|
45519
45541
|
}
|
|
45520
|
-
call(messages, options) {
|
|
45521
|
-
|
|
45542
|
+
async call(messages, options) {
|
|
45543
|
+
if (this.stream) {
|
|
45544
|
+
throw new Error("OpenAI streaming responses are not supported by the built-in fetch transport yet.");
|
|
45545
|
+
}
|
|
45546
|
+
const tools = options?.tools ?? null;
|
|
45547
|
+
const responseModel = options?.responseModel ?? null;
|
|
45548
|
+
return this.api === "responses" ? await this.callResponses(messages, tools, responseModel) : await this.callChatCompletions(messages, tools);
|
|
45522
45549
|
}
|
|
45523
45550
|
async acall(messages, options) {
|
|
45524
45551
|
return await super.acall(messages, options);
|
|
@@ -45655,6 +45682,79 @@ ${message.content}` : message.content;
|
|
|
45655
45682
|
_prepare_responses_params(messages, tools = null, responseModel = null) {
|
|
45656
45683
|
return this.prepareResponsesParams(messages, tools, responseModel);
|
|
45657
45684
|
}
|
|
45685
|
+
async callChatCompletions(messages, tools) {
|
|
45686
|
+
const params = this.prepareCompletionParams(this.formatMessages(messages), tools);
|
|
45687
|
+
const response = await this.fetchOpenAI("chat/completions", params);
|
|
45688
|
+
const usage = this.extractOpenAITokenUsage(response);
|
|
45689
|
+
if (usage.total_tokens !== 0) {
|
|
45690
|
+
this.trackTokenUsageInternal(usage);
|
|
45691
|
+
}
|
|
45692
|
+
const choices = readObject(response).choices;
|
|
45693
|
+
const firstChoice = Array.isArray(choices) ? readObject(choices[0]) : {};
|
|
45694
|
+
const message = readObject(firstChoice.message);
|
|
45695
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
45696
|
+
if (toolCalls.length > 0) {
|
|
45697
|
+
return toolCalls;
|
|
45698
|
+
}
|
|
45699
|
+
return typeof message.content === "string" ? message.content : "";
|
|
45700
|
+
}
|
|
45701
|
+
async callResponses(messages, tools, responseModel) {
|
|
45702
|
+
const params = this.prepareResponsesParams(this.formatMessages(messages), tools, responseModel);
|
|
45703
|
+
const response = await this.fetchOpenAI("responses", params);
|
|
45704
|
+
const usage = this.extractResponsesTokenUsage(response);
|
|
45705
|
+
if (usage.total_tokens !== 0) {
|
|
45706
|
+
this.trackTokenUsageInternal(usage);
|
|
45707
|
+
}
|
|
45708
|
+
const responseId = stringOrNull2(readObject(response).id);
|
|
45709
|
+
if (this.autoChain && responseId) {
|
|
45710
|
+
this.responseChainId = responseId;
|
|
45711
|
+
}
|
|
45712
|
+
const reasoningItems = this.extractReasoningItems(response);
|
|
45713
|
+
if (this.autoChainReasoning && reasoningItems.length > 0) {
|
|
45714
|
+
this.reasoningChainItems = reasoningItems;
|
|
45715
|
+
}
|
|
45716
|
+
const result = this.extractBuiltinToolOutputs(response);
|
|
45717
|
+
if (result.function_calls.length > 0 || result.hasToolOutputs() || result.hasReasoning()) {
|
|
45718
|
+
return result;
|
|
45719
|
+
}
|
|
45720
|
+
return result.text;
|
|
45721
|
+
}
|
|
45722
|
+
async fetchOpenAI(path, body) {
|
|
45723
|
+
const clientParams = this.getClientParams();
|
|
45724
|
+
const apiKey = stringOrNull2(clientParams.api_key);
|
|
45725
|
+
if (!apiKey) {
|
|
45726
|
+
throw new Error("OpenAI API key is required. Pass api_key when constructing OpenAICompletion.");
|
|
45727
|
+
}
|
|
45728
|
+
const baseUrl = (stringOrNull2(clientParams.base_url) ?? "https://api.openai.com/v1").replace(/\/$/, "");
|
|
45729
|
+
const defaultQuery = readObject(clientParams.default_query);
|
|
45730
|
+
const query = new URLSearchParams();
|
|
45731
|
+
for (const [key, value] of Object.entries(defaultQuery)) {
|
|
45732
|
+
if (value !== void 0 && value !== null) {
|
|
45733
|
+
query.set(key, typeof value === "string" || typeof value === "number" || typeof value === "boolean" ? String(value) : JSON.stringify(value));
|
|
45734
|
+
}
|
|
45735
|
+
}
|
|
45736
|
+
const url = `${baseUrl}/${path}${query.size > 0 ? `?${query.toString()}` : ""}`;
|
|
45737
|
+
const defaultHeaders = Object.fromEntries(
|
|
45738
|
+
Object.entries(readObject(clientParams.default_headers)).filter(([, value]) => typeof value === "string")
|
|
45739
|
+
);
|
|
45740
|
+
const response = await fetch(url, {
|
|
45741
|
+
method: "POST",
|
|
45742
|
+
headers: {
|
|
45743
|
+
...defaultHeaders,
|
|
45744
|
+
Authorization: `Bearer ${apiKey}`,
|
|
45745
|
+
"Content-Type": "application/json",
|
|
45746
|
+
...typeof clientParams.organization === "string" ? { "OpenAI-Organization": clientParams.organization } : {},
|
|
45747
|
+
...typeof clientParams.project === "string" ? { "OpenAI-Project": clientParams.project } : {}
|
|
45748
|
+
},
|
|
45749
|
+
body: JSON.stringify(body)
|
|
45750
|
+
});
|
|
45751
|
+
const payload = await response.json().catch(() => ({}));
|
|
45752
|
+
if (!response.ok) {
|
|
45753
|
+
const error = readObject(readObject(payload).error);
|
|
45754
|
+
throw new Error(stringOrNull2(error.message) ?? `OpenAI request failed with HTTP ${response.status.toString()}.`);
|
|
45755
|
+
}
|
|
45756
|
+
return payload;
|
|
45757
|
+
}
|
|
45658
45758
|
convertToolsForInterference(tools) {
|
|
45659
45759
|
return convertToolsToOpenAISchema(tools)[0];
|
|
45660
45760
|
}
|
|
@@ -45854,6 +45954,18 @@ ${message.content}` : message.content;
|
|
|
45854
45954
|
name: item.name,
|
|
45855
45955
|
arguments: item.arguments
|
|
45856
45956
|
});
|
|
45957
|
+
} else if (type2 === "message") {
|
|
45958
|
+
const content = Array.isArray(item.content) ? item.content : [];
|
|
45959
|
+
const textParts = [];
|
|
45960
|
+
for (const rawContent of content) {
|
|
45961
|
+
const contentItem = readObject(rawContent);
|
|
45962
|
+
if (contentItem.type === "output_text" && typeof contentItem.text === "string") {
|
|
45963
|
+
textParts.push(contentItem.text);
|
|
45964
|
+
}
|
|
45965
|
+
}
|
|
45966
|
+
if (!result.text && textParts.length > 0) {
|
|
45967
|
+
result.text = textParts.join("");
|
|
45968
|
+
}
|
|
45857
45969
|
}
|
|
45858
45970
|
}
|
|
45859
45971
|
return result;
|
|
@@ -46135,6 +46247,7 @@ var OpenAICompatibleCompletion = class _OpenAICompatibleCompletion extends OpenA
|
|
|
46135
46247
|
return _OpenAICompatibleCompletion.resolveHeaders(headers, config);
|
|
46136
46248
|
}
|
|
46137
46249
|
};
|
|
46250
|
+
registerLLMProviderFactory("openai", (options) => new OpenAICompletion(options));
|
|
46138
46251
|
for (const provider of Object.keys(OPENAI_COMPATIBLE_PROVIDERS)) {
|
|
46139
46252
|
registerLLMProviderFactory(provider, (options) => {
|
|
46140
46253
|
const { logprobs: _logprobs, ...compatibleOptions } = options;
|
|
@@ -46300,6 +46413,8 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
46300
46413
|
stop: options.stop,
|
|
46301
46414
|
stopSequences: options.stopSequences,
|
|
46302
46415
|
stop_sequences: options.stop_sequences,
|
|
46416
|
+
responseFormat: options.responseFormat,
|
|
46417
|
+
response_format: options.response_format,
|
|
46303
46418
|
maxTokens: options.maxTokens ?? options.max_tokens ?? 4096,
|
|
46304
46419
|
timeout: options.timeout ?? null
|
|
46305
46420
|
}));
|
|
@@ -46324,8 +46439,71 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
46324
46439
|
this.previousThinkingBlocks = [];
|
|
46325
46440
|
this._previous_thinking_blocks = this.previousThinkingBlocks;
|
|
46326
46441
|
}
|
|
46327
|
-
call(messages, options) {
|
|
46328
|
-
|
|
46442
|
+
async call(messages, options) {
|
|
46443
|
+
const apiKey = this.apiKey;
|
|
46444
|
+
if (!apiKey) {
|
|
46445
|
+
throw new Error("Anthropic API key required. Pass api_key when constructing AnthropicCompletion.");
|
|
46446
|
+
}
|
|
46447
|
+
if (this.stream) {
|
|
46448
|
+
throw new Error("Anthropic streaming responses are not supported by the built-in HTTP transport yet.");
|
|
46449
|
+
}
|
|
46450
|
+
const [formattedMessages, systemMessage] = this.formatMessagesForAnthropic(messages);
|
|
46451
|
+
const availableFunctions = options?.availableFunctions ?? options?.available_functions ?? null;
|
|
46452
|
+
const params = this.prepareCompletionParams(
|
|
46453
|
+
formattedMessages,
|
|
46454
|
+
systemMessage,
|
|
46455
|
+
options?.tools ?? null,
|
|
46456
|
+
availableFunctions
|
|
46457
|
+
);
|
|
46458
|
+
const schema = anthropicResponseSchema(options?.responseModel ?? this.responseFormat);
|
|
46459
|
+
if (schema) {
|
|
46460
|
+
const tools = Array.isArray(params.tools) ? params.tools.filter((tool2) => typeof tool2 === "object" && tool2 !== null) : [];
|
|
46461
|
+
tools.push({
|
|
46462
|
+
name: STRUCTURED_OUTPUT_TOOL_NAME,
|
|
46463
|
+
description: "Use this tool to provide your final structured response in the required JSON shape.",
|
|
46464
|
+
input_schema: sanitizeToolParamsForAnthropicStrict(structuredClone(schema)),
|
|
46465
|
+
strict: true
|
|
46466
|
+
});
|
|
46467
|
+
params.tools = tools;
|
|
46468
|
+
params.tool_choice = { type: "tool", name: STRUCTURED_OUTPUT_TOOL_NAME };
|
|
46469
|
+
}
|
|
46470
|
+
const baseUrl = this.baseUrl ?? "https://api.anthropic.com";
|
|
46471
|
+
const response = await fetch(`${baseUrl.replace(/\/$/, "")}/v1/messages`, {
|
|
46472
|
+
method: "POST",
|
|
46473
|
+
headers: {
|
|
46474
|
+
"content-type": "application/json",
|
|
46475
|
+
"x-api-key": apiKey,
|
|
46476
|
+
"anthropic-version": "2023-06-01"
|
|
46477
|
+
},
|
|
46478
|
+
body: JSON.stringify(params),
|
|
46479
|
+
...options?.signal ? { signal: options.signal } : {}
|
|
46480
|
+
});
|
|
46481
|
+
const body = await response.json().catch(() => ({}));
|
|
46482
|
+
if (!response.ok) {
|
|
46483
|
+
const error = readObject2(readObject2(body).error);
|
|
46484
|
+
throw new Error(scalarToString(error.message) ?? `Anthropic request failed with HTTP ${response.status.toString()}.`);
|
|
46485
|
+
}
|
|
46486
|
+
const usage = this.extractAnthropicTokenUsage(body);
|
|
46487
|
+
if (usage.total_tokens !== 0) {
|
|
46488
|
+
this.trackTokenUsageInternal(usage);
|
|
46489
|
+
}
|
|
46490
|
+
const thinkingBlocks = _AnthropicCompletion.extractThinkingBlocksFromResponse(body);
|
|
46491
|
+
if (thinkingBlocks.length > 0) {
|
|
46492
|
+
this.previousThinkingBlocks = thinkingBlocks;
|
|
46493
|
+
this._previous_thinking_blocks = this.previousThinkingBlocks;
|
|
46494
|
+
}
|
|
46495
|
+
const structuredOutput = _AnthropicCompletion.extractStructuredOutputFromResponse(body);
|
|
46496
|
+
if (structuredOutput) {
|
|
46497
|
+
return structuredOutput;
|
|
46498
|
+
}
|
|
46499
|
+
const toolUses = _AnthropicCompletion.extractToolUsesFromResponse(body);
|
|
46500
|
+
if (toolUses.length > 0) {
|
|
46501
|
+
if (availableFunctions && Object.keys(availableFunctions).length > 0) {
|
|
46502
|
+
return await this.executeFirstTool(toolUses, availableFunctions);
|
|
46503
|
+
}
|
|
46504
|
+
return toolUses;
|
|
46505
|
+
}
|
|
46506
|
+
return this.applyStopWords(anthropicResponseText(body));
|
|
46329
46507
|
}
|
|
46330
46508
|
async acall(messages, options) {
|
|
46331
46509
|
return await super.acall(messages, options);
|
|
@@ -46818,7 +46996,7 @@ var BedrockCompletion = class _BedrockCompletion extends ConfiguredLLM {
|
|
|
46818
46996
|
maxTokens: options.maxTokens ?? options.max_tokens ?? null,
|
|
46819
46997
|
timeout: options.timeout ?? null
|
|
46820
46998
|
}));
|
|
46821
|
-
this.regionName = options.regionName ?? options.region_name ??
|
|
46999
|
+
this.regionName = options.regionName ?? options.region_name ?? process.env.AWS_DEFAULT_REGION ?? process.env.AWS_REGION_NAME ?? process.env.AWS_REGION ?? "us-east-1";
|
|
46822
47000
|
this.region_name = this.regionName;
|
|
46823
47001
|
this.session = options.session ?? null;
|
|
46824
47002
|
this.timeout = options.timeout ?? null;
|
|
@@ -46839,8 +47017,32 @@ var BedrockCompletion = class _BedrockCompletion extends ConfiguredLLM {
|
|
|
46839
47017
|
this.additional_model_response_field_paths = this.additionalModelResponseFieldPaths;
|
|
46840
47018
|
this.interceptor = null;
|
|
46841
47019
|
}
|
|
46842
|
-
call(messages, options) {
|
|
46843
|
-
|
|
47020
|
+
async call(messages, options) {
|
|
47021
|
+
if (this.stream) {
|
|
47022
|
+
throw new Error("Bedrock streaming responses are not supported by the built-in client transport yet.");
|
|
47023
|
+
}
|
|
47024
|
+
const client = this.getConverseClient();
|
|
47025
|
+
const { messages: formattedMessages, body } = this.prepareConverseRequestBody(
|
|
47026
|
+
messages,
|
|
47027
|
+
options?.tools ?? null
|
|
47028
|
+
);
|
|
47029
|
+
const request = {
|
|
47030
|
+
modelId: this.model,
|
|
47031
|
+
messages: formattedMessages,
|
|
47032
|
+
...body
|
|
47033
|
+
};
|
|
47034
|
+
const response = await client.converse(request);
|
|
47035
|
+
this.trackTokenUsageInternal(readObject2(readObject2(response).usage));
|
|
47036
|
+
const toolUses = _BedrockCompletion.extractToolUsesFromResponse(response);
|
|
47037
|
+
const availableFunctions = options?.availableFunctions ?? options?.available_functions ?? null;
|
|
47038
|
+
if (toolUses.length > 0) {
|
|
47039
|
+
if (availableFunctions && Object.keys(availableFunctions).length > 0) {
|
|
47040
|
+
const executed = await this.executeToolUseAndPrepareMessages(formattedMessages, toolUses[0], availableFunctions);
|
|
47041
|
+
return executed.result;
|
|
47042
|
+
}
|
|
47043
|
+
return toolUses;
|
|
47044
|
+
}
|
|
47045
|
+
return bedrockResponseText(response);
|
|
46844
47046
|
}
|
|
46845
47047
|
async acall(messages, options) {
|
|
46846
47048
|
return await super.acall(messages, options);
|
|
@@ -46915,7 +47117,14 @@ var BedrockCompletion = class _BedrockCompletion extends ConfiguredLLM {
|
|
|
46915
47117
|
return this.getFileUploader();
|
|
46916
47118
|
}
|
|
46917
47119
|
toConfigDict() {
|
|
46918
|
-
return
|
|
47120
|
+
return {
|
|
47121
|
+
...super.toConfigDict(),
|
|
47122
|
+
...this.regionName && this.regionName !== "us-east-1" ? { region_name: this.regionName } : {},
|
|
47123
|
+
...this.maxTokens === null ? {} : { max_tokens: this.maxTokens },
|
|
47124
|
+
...this.topP === null ? {} : { top_p: this.topP },
|
|
47125
|
+
...this.topK === null ? {} : { top_k: this.topK },
|
|
47126
|
+
...this.guardrailConfig ? { guardrail_config: this.guardrailConfig } : {}
|
|
47127
|
+
};
|
|
46919
47128
|
}
|
|
46920
47129
|
to_config_dict() {
|
|
46921
47130
|
return this.toConfigDict();
|
|
@@ -47263,6 +47472,24 @@ ${content}` : content;
|
|
|
47263
47472
|
const model = this.model.toLowerCase();
|
|
47264
47473
|
return model.includes("anthropic") || model.includes("claude");
|
|
47265
47474
|
}
|
|
47475
|
+
getConverseClient() {
|
|
47476
|
+
const direct = readObject2(this.session).converse;
|
|
47477
|
+
if (typeof direct === "function") {
|
|
47478
|
+
return { converse: (request) => Reflect.apply(direct, this.session, [request]) };
|
|
47479
|
+
}
|
|
47480
|
+
const clientFactory = readObject2(this.session).client;
|
|
47481
|
+
if (typeof clientFactory === "function") {
|
|
47482
|
+
const client = Reflect.apply(clientFactory, this.session, [
|
|
47483
|
+
"bedrock-runtime",
|
|
47484
|
+
this.regionName ? { region: this.regionName } : void 0
|
|
47485
|
+
]);
|
|
47486
|
+
const converse = readObject2(client).converse;
|
|
47487
|
+
if (typeof converse === "function") {
|
|
47488
|
+
return { converse: (request) => Reflect.apply(converse, client, [request]) };
|
|
47489
|
+
}
|
|
47490
|
+
}
|
|
47491
|
+
throw new Error("Bedrock live calls require a session/client with a converse(request) method.");
|
|
47492
|
+
}
|
|
47266
47493
|
};
|
|
47267
47494
|
var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
47268
47495
|
project;
|
|
@@ -47311,8 +47538,8 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
47311
47538
|
timeout: options.timeout ?? null
|
|
47312
47539
|
}));
|
|
47313
47540
|
this.project = options.project ?? process.env.GOOGLE_CLOUD_PROJECT ?? null;
|
|
47314
|
-
this.location = options.location ?? "us-central1";
|
|
47315
|
-
this.useVertexai = options.useVertexai ?? options.use_vertexai ??
|
|
47541
|
+
this.location = options.location ?? process.env.GOOGLE_CLOUD_LOCATION ?? "us-central1";
|
|
47542
|
+
this.useVertexai = options.useVertexai ?? options.use_vertexai ?? process.env.GOOGLE_GENAI_USE_VERTEXAI?.toLowerCase() === "true";
|
|
47316
47543
|
this.use_vertexai = this.useVertexai;
|
|
47317
47544
|
this.timeout = options.timeout ?? null;
|
|
47318
47545
|
this.maxRetries = options.maxRetries ?? options.max_retries ?? 2;
|
|
@@ -47340,7 +47567,7 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
47340
47567
|
}
|
|
47341
47568
|
call(messages, options) {
|
|
47342
47569
|
if (this.useVertexai) {
|
|
47343
|
-
return
|
|
47570
|
+
return this.callVertexAI(messages, options);
|
|
47344
47571
|
}
|
|
47345
47572
|
const apiKey = this.apiKey ?? process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY ?? null;
|
|
47346
47573
|
if (!apiKey) {
|
|
@@ -47394,6 +47621,58 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
47394
47621
|
);
|
|
47395
47622
|
});
|
|
47396
47623
|
}
|
|
47624
|
+
async callVertexAI(messages, options) {
|
|
47625
|
+
if (!this.project) {
|
|
47626
|
+
throw new Error("Vertex AI Gemini calls require a project.");
|
|
47627
|
+
}
|
|
47628
|
+
const clientParams = readObject2(this.clientParams);
|
|
47629
|
+
const accessToken = scalarToString(clientParams.access_token ?? clientParams.accessToken ?? this.apiKey);
|
|
47630
|
+
if (!accessToken) {
|
|
47631
|
+
throw new Error("Vertex AI Gemini calls require client_params.access_token or api_key.");
|
|
47632
|
+
}
|
|
47633
|
+
const [contents, systemInstruction] = this.formatMessagesForGemini(messages);
|
|
47634
|
+
const tools = options?.tools ?? null;
|
|
47635
|
+
const generationConfig = this.prepareGenerationConfig(
|
|
47636
|
+
systemInstruction,
|
|
47637
|
+
tools,
|
|
47638
|
+
options?.responseModel ?? null
|
|
47639
|
+
);
|
|
47640
|
+
const requestBody = readObject2(generationConfig);
|
|
47641
|
+
const generationConfigBody = { ...generationConfig };
|
|
47642
|
+
delete generationConfigBody.system_instruction;
|
|
47643
|
+
delete generationConfigBody.tools;
|
|
47644
|
+
delete generationConfigBody.safety_settings;
|
|
47645
|
+
const model = this.model.replace(/^(?:gemini|google)\//, "");
|
|
47646
|
+
const baseUrl = this.baseUrl ?? `https://${this.location}-aiplatform.googleapis.com/v1`;
|
|
47647
|
+
const url = `${baseUrl.replace(/\/$/, "")}/projects/${encodeURIComponent(this.project)}/locations/${encodeURIComponent(this.location)}/publishers/google/models/${encodeURIComponent(model)}:generateContent`;
|
|
47648
|
+
const response = await fetch(url, {
|
|
47649
|
+
method: "POST",
|
|
47650
|
+
headers: {
|
|
47651
|
+
Authorization: `Bearer ${accessToken}`,
|
|
47652
|
+
"Content-Type": "application/json"
|
|
47653
|
+
},
|
|
47654
|
+
body: JSON.stringify({
|
|
47655
|
+
contents,
|
|
47656
|
+
...Object.keys(generationConfigBody).length > 0 ? { generationConfig: generationConfigBody } : {},
|
|
47657
|
+
..."system_instruction" in requestBody ? { systemInstruction: requestBody.system_instruction } : {},
|
|
47658
|
+
..."tools" in requestBody ? { tools: requestBody.tools } : {},
|
|
47659
|
+
..."safety_settings" in requestBody ? { safetySettings: requestBody.safety_settings } : {}
|
|
47660
|
+
}),
|
|
47661
|
+
...options?.signal ? { signal: options.signal } : {}
|
|
47662
|
+
});
|
|
47663
|
+
const body = await response.json().catch(() => ({}));
|
|
47664
|
+
if (!response.ok) {
|
|
47665
|
+
const error = readObject2(readObject2(body).error);
|
|
47666
|
+
throw new Error(scalarToString(error.message) ?? `Vertex AI Gemini request failed with HTTP ${response.status.toString()}.`);
|
|
47667
|
+
}
|
|
47668
|
+
return await this.processResponseWithTools(
|
|
47669
|
+
body,
|
|
47670
|
+
contents,
|
|
47671
|
+
options?.availableFunctions ?? options?.available_functions ?? null,
|
|
47672
|
+
null,
|
|
47673
|
+
geminiMaxToolRounds(options)
|
|
47674
|
+
);
|
|
47675
|
+
}
|
|
47397
47676
|
async acall(messages, options) {
|
|
47398
47677
|
return await this.call(this.formatMessages(messages), options);
|
|
47399
47678
|
}
|
|
@@ -48401,7 +48680,7 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
48401
48680
|
if (this._responses_delegate) {
|
|
48402
48681
|
return this._responses_delegate.call(messages, options);
|
|
48403
48682
|
}
|
|
48404
|
-
return
|
|
48683
|
+
return this.callChatCompletions(messages, options);
|
|
48405
48684
|
}
|
|
48406
48685
|
async acall(messages, options) {
|
|
48407
48686
|
if (this._responses_delegate) {
|
|
@@ -48534,7 +48813,11 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
48534
48813
|
}
|
|
48535
48814
|
return {
|
|
48536
48815
|
endpoint,
|
|
48537
|
-
api_key: apiKey
|
|
48816
|
+
...apiKey ? { api_key: apiKey } : {
|
|
48817
|
+
credential: {
|
|
48818
|
+
provider: "DefaultAzureCredential"
|
|
48819
|
+
}
|
|
48820
|
+
},
|
|
48538
48821
|
api_version: this.apiVersion,
|
|
48539
48822
|
...credentialScopes ? { credential_scopes: credentialScopes } : {}
|
|
48540
48823
|
};
|
|
@@ -48550,9 +48833,6 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
48550
48833
|
if (!kwargs.endpoint) {
|
|
48551
48834
|
throw new Error("Azure endpoint is required");
|
|
48552
48835
|
}
|
|
48553
|
-
if (!kwargs.api_key) {
|
|
48554
|
-
throw new Error("Azure API key is required");
|
|
48555
|
-
}
|
|
48556
48836
|
this._client = {
|
|
48557
48837
|
provider: "azure",
|
|
48558
48838
|
model: this.model,
|
|
@@ -48615,6 +48895,50 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
48615
48895
|
_prepare_completion_params(messages, tools = null) {
|
|
48616
48896
|
return this.prepareCompletionParams(messages, tools);
|
|
48617
48897
|
}
|
|
48898
|
+
async callChatCompletions(messages, options) {
|
|
48899
|
+
if (this.stream) {
|
|
48900
|
+
throw new Error("Azure streaming responses are not supported by the built-in fetch transport yet.");
|
|
48901
|
+
}
|
|
48902
|
+
const client = this.getSyncClient();
|
|
48903
|
+
const endpoint = scalarToString(client.endpoint);
|
|
48904
|
+
const apiKey = scalarToString(client.api_key);
|
|
48905
|
+
if (!endpoint) {
|
|
48906
|
+
throw new Error("Azure endpoint is required");
|
|
48907
|
+
}
|
|
48908
|
+
if (!apiKey) {
|
|
48909
|
+
throw new Error("Azure API key is required");
|
|
48910
|
+
}
|
|
48911
|
+
const params = this.prepareCompletionParams(
|
|
48912
|
+
this.formatMessages(messages),
|
|
48913
|
+
options?.tools ?? null
|
|
48914
|
+
);
|
|
48915
|
+
const url = azureChatCompletionsUrl(endpoint, this.model, this.apiVersion);
|
|
48916
|
+
const response = await fetch(url, {
|
|
48917
|
+
method: "POST",
|
|
48918
|
+
headers: {
|
|
48919
|
+
"Content-Type": "application/json",
|
|
48920
|
+
"api-key": apiKey
|
|
48921
|
+
},
|
|
48922
|
+
body: JSON.stringify(params)
|
|
48923
|
+
});
|
|
48924
|
+
const payload = await response.json().catch(() => ({}));
|
|
48925
|
+
if (!response.ok) {
|
|
48926
|
+
const error = readObject2(readObject2(payload).error);
|
|
48927
|
+
throw new Error(scalarToString(error.message) ?? `Azure completion request failed with HTTP ${response.status.toString()}.`);
|
|
48928
|
+
}
|
|
48929
|
+
const usage = this.extractAzureTokenUsage(payload);
|
|
48930
|
+
if (usage.total_tokens !== 0) {
|
|
48931
|
+
this.trackTokenUsageInternal(usage);
|
|
48932
|
+
}
|
|
48933
|
+
const choices = readObject2(payload).choices;
|
|
48934
|
+
const firstChoice = Array.isArray(choices) ? readObject2(choices[0]) : {};
|
|
48935
|
+
const message = readObject2(firstChoice.message);
|
|
48936
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
48937
|
+
if (toolCalls.length > 0) {
|
|
48938
|
+
return toolCalls;
|
|
48939
|
+
}
|
|
48940
|
+
return typeof message.content === "string" ? message.content : "";
|
|
48941
|
+
}
|
|
48618
48942
|
prepareResponsesParams(messages, tools = null, responseModel = null) {
|
|
48619
48943
|
if (!this._responses_delegate) {
|
|
48620
48944
|
throw new Error("Azure Responses API is only available when api is set to 'responses'.");
|
|
@@ -48912,15 +49236,29 @@ function geminiResponseSchema(value) {
|
|
|
48912
49236
|
const schema = schemaProvider.model_json_schema?.() ?? schemaProvider.modelJsonSchema?.() ?? schemaProvider.schema;
|
|
48913
49237
|
return schema && typeof schema === "object" && !Array.isArray(schema) ? schema : null;
|
|
48914
49238
|
}
|
|
49239
|
+
function anthropicResponseSchema(value) {
|
|
49240
|
+
if (!value || typeof value !== "object") {
|
|
49241
|
+
return null;
|
|
49242
|
+
}
|
|
49243
|
+
const schemaProvider = value;
|
|
49244
|
+
const schema = schemaProvider.model_json_schema?.() ?? schemaProvider.modelJsonSchema?.() ?? schemaProvider.schema;
|
|
49245
|
+
return schema && typeof schema === "object" && !Array.isArray(schema) ? schema : null;
|
|
49246
|
+
}
|
|
48915
49247
|
function anthropicResponseContent(response) {
|
|
48916
49248
|
const content = readObject2(response).content;
|
|
48917
49249
|
return Array.isArray(content) ? content : [];
|
|
48918
49250
|
}
|
|
49251
|
+
function anthropicResponseText(response) {
|
|
49252
|
+
return anthropicResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
49253
|
+
}
|
|
48919
49254
|
function bedrockResponseContent(response) {
|
|
48920
49255
|
const output4 = readObject2(readObject2(response).output);
|
|
48921
49256
|
const message = readObject2(output4.message);
|
|
48922
49257
|
return Array.isArray(message.content) ? message.content : [];
|
|
48923
49258
|
}
|
|
49259
|
+
function bedrockResponseText(response) {
|
|
49260
|
+
return bedrockResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
49261
|
+
}
|
|
48924
49262
|
function isAzureOpenAIEndpoint(endpoint) {
|
|
48925
49263
|
if (!endpoint) {
|
|
48926
49264
|
return false;
|
|
@@ -48951,6 +49289,14 @@ function azureResponsesBaseUrl(endpoint) {
|
|
|
48951
49289
|
return `${trimmed.replace(/\/openai(?:\/v1)?$/i, "")}/openai/v1/`;
|
|
48952
49290
|
}
|
|
48953
49291
|
}
|
|
49292
|
+
function azureChatCompletionsUrl(endpoint, model, apiVersion) {
|
|
49293
|
+
const version2 = encodeURIComponent(apiVersion ?? "2024-06-01");
|
|
49294
|
+
const trimmed = endpoint.replace(/\/+$/, "");
|
|
49295
|
+
const deploymentPath = "/openai/deployments/";
|
|
49296
|
+
const lower = trimmed.toLowerCase();
|
|
49297
|
+
const base = lower.includes(deploymentPath) ? trimmed : `${trimmed.replace(/\/openai(?:\/v1)?$/i, "")}${deploymentPath}${encodeURIComponent(azureResponsesModelName(model))}`;
|
|
49298
|
+
return `${base}/chat/completions?api-version=${version2}`;
|
|
49299
|
+
}
|
|
48954
49300
|
function geminiContextWindowSize(model) {
|
|
48955
49301
|
const windows = {
|
|
48956
49302
|
"gemini-3-pro-preview": 1048576,
|
package/dist/index.js
CHANGED
|
@@ -14006,6 +14006,28 @@ function createLLM(llmValue = null, env = process.env) {
|
|
|
14006
14006
|
if (apiBase !== void 0) {
|
|
14007
14007
|
options.api_base = apiBase;
|
|
14008
14008
|
}
|
|
14009
|
+
const providerOptions = options;
|
|
14010
|
+
const llmOptions = llmValue;
|
|
14011
|
+
for (const key of [
|
|
14012
|
+
"api",
|
|
14013
|
+
"instructions",
|
|
14014
|
+
"store",
|
|
14015
|
+
"previousResponseId",
|
|
14016
|
+
"previous_response_id",
|
|
14017
|
+
"include",
|
|
14018
|
+
"builtinTools",
|
|
14019
|
+
"builtin_tools",
|
|
14020
|
+
"parseToolOutputs",
|
|
14021
|
+
"parse_tool_outputs",
|
|
14022
|
+
"autoChain",
|
|
14023
|
+
"auto_chain",
|
|
14024
|
+
"autoChainReasoning",
|
|
14025
|
+
"auto_chain_reasoning"
|
|
14026
|
+
]) {
|
|
14027
|
+
if (llmOptions[key] !== void 0) {
|
|
14028
|
+
providerOptions[key] = llmOptions[key];
|
|
14029
|
+
}
|
|
14030
|
+
}
|
|
14009
14031
|
return createConfiguredLLM(options);
|
|
14010
14032
|
}
|
|
14011
14033
|
var create_llm = createLLM;
|
|
@@ -29180,8 +29202,13 @@ var OpenAICompletion = class _OpenAICompletion extends ConfiguredLLM {
|
|
|
29180
29202
|
this.responseChainId = this.previousResponseId;
|
|
29181
29203
|
this.reasoningChainItems = null;
|
|
29182
29204
|
}
|
|
29183
|
-
call(messages, options) {
|
|
29184
|
-
|
|
29205
|
+
async call(messages, options) {
|
|
29206
|
+
if (this.stream) {
|
|
29207
|
+
throw new Error("OpenAI streaming responses are not supported by the built-in fetch transport yet.");
|
|
29208
|
+
}
|
|
29209
|
+
const tools = options?.tools ?? null;
|
|
29210
|
+
const responseModel = options?.responseModel ?? null;
|
|
29211
|
+
return this.api === "responses" ? await this.callResponses(messages, tools, responseModel) : await this.callChatCompletions(messages, tools);
|
|
29185
29212
|
}
|
|
29186
29213
|
async acall(messages, options) {
|
|
29187
29214
|
return await super.acall(messages, options);
|
|
@@ -29318,6 +29345,79 @@ ${message.content}` : message.content;
|
|
|
29318
29345
|
_prepare_responses_params(messages, tools = null, responseModel = null) {
|
|
29319
29346
|
return this.prepareResponsesParams(messages, tools, responseModel);
|
|
29320
29347
|
}
|
|
29348
|
+
async callChatCompletions(messages, tools) {
|
|
29349
|
+
const params = this.prepareCompletionParams(this.formatMessages(messages), tools);
|
|
29350
|
+
const response = await this.fetchOpenAI("chat/completions", params);
|
|
29351
|
+
const usage = this.extractOpenAITokenUsage(response);
|
|
29352
|
+
if (usage.total_tokens !== 0) {
|
|
29353
|
+
this.trackTokenUsageInternal(usage);
|
|
29354
|
+
}
|
|
29355
|
+
const choices = readObject(response).choices;
|
|
29356
|
+
const firstChoice = Array.isArray(choices) ? readObject(choices[0]) : {};
|
|
29357
|
+
const message = readObject(firstChoice.message);
|
|
29358
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
29359
|
+
if (toolCalls.length > 0) {
|
|
29360
|
+
return toolCalls;
|
|
29361
|
+
}
|
|
29362
|
+
return typeof message.content === "string" ? message.content : "";
|
|
29363
|
+
}
|
|
29364
|
+
async callResponses(messages, tools, responseModel) {
|
|
29365
|
+
const params = this.prepareResponsesParams(this.formatMessages(messages), tools, responseModel);
|
|
29366
|
+
const response = await this.fetchOpenAI("responses", params);
|
|
29367
|
+
const usage = this.extractResponsesTokenUsage(response);
|
|
29368
|
+
if (usage.total_tokens !== 0) {
|
|
29369
|
+
this.trackTokenUsageInternal(usage);
|
|
29370
|
+
}
|
|
29371
|
+
const responseId = stringOrNull2(readObject(response).id);
|
|
29372
|
+
if (this.autoChain && responseId) {
|
|
29373
|
+
this.responseChainId = responseId;
|
|
29374
|
+
}
|
|
29375
|
+
const reasoningItems = this.extractReasoningItems(response);
|
|
29376
|
+
if (this.autoChainReasoning && reasoningItems.length > 0) {
|
|
29377
|
+
this.reasoningChainItems = reasoningItems;
|
|
29378
|
+
}
|
|
29379
|
+
const result = this.extractBuiltinToolOutputs(response);
|
|
29380
|
+
if (result.function_calls.length > 0 || result.hasToolOutputs() || result.hasReasoning()) {
|
|
29381
|
+
return result;
|
|
29382
|
+
}
|
|
29383
|
+
return result.text;
|
|
29384
|
+
}
|
|
29385
|
+
async fetchOpenAI(path, body) {
|
|
29386
|
+
const clientParams = this.getClientParams();
|
|
29387
|
+
const apiKey = stringOrNull2(clientParams.api_key);
|
|
29388
|
+
if (!apiKey) {
|
|
29389
|
+
throw new Error("OpenAI API key is required. Pass api_key when constructing OpenAICompletion.");
|
|
29390
|
+
}
|
|
29391
|
+
const baseUrl = (stringOrNull2(clientParams.base_url) ?? "https://api.openai.com/v1").replace(/\/$/, "");
|
|
29392
|
+
const defaultQuery = readObject(clientParams.default_query);
|
|
29393
|
+
const query = new URLSearchParams();
|
|
29394
|
+
for (const [key, value] of Object.entries(defaultQuery)) {
|
|
29395
|
+
if (value !== void 0 && value !== null) {
|
|
29396
|
+
query.set(key, typeof value === "string" || typeof value === "number" || typeof value === "boolean" ? String(value) : JSON.stringify(value));
|
|
29397
|
+
}
|
|
29398
|
+
}
|
|
29399
|
+
const url = `${baseUrl}/${path}${query.size > 0 ? `?${query.toString()}` : ""}`;
|
|
29400
|
+
const defaultHeaders = Object.fromEntries(
|
|
29401
|
+
Object.entries(readObject(clientParams.default_headers)).filter(([, value]) => typeof value === "string")
|
|
29402
|
+
);
|
|
29403
|
+
const response = await fetch(url, {
|
|
29404
|
+
method: "POST",
|
|
29405
|
+
headers: {
|
|
29406
|
+
...defaultHeaders,
|
|
29407
|
+
Authorization: `Bearer ${apiKey}`,
|
|
29408
|
+
"Content-Type": "application/json",
|
|
29409
|
+
...typeof clientParams.organization === "string" ? { "OpenAI-Organization": clientParams.organization } : {},
|
|
29410
|
+
...typeof clientParams.project === "string" ? { "OpenAI-Project": clientParams.project } : {}
|
|
29411
|
+
},
|
|
29412
|
+
body: JSON.stringify(body)
|
|
29413
|
+
});
|
|
29414
|
+
const payload = await response.json().catch(() => ({}));
|
|
29415
|
+
if (!response.ok) {
|
|
29416
|
+
const error = readObject(readObject(payload).error);
|
|
29417
|
+
throw new Error(stringOrNull2(error.message) ?? `OpenAI request failed with HTTP ${response.status.toString()}.`);
|
|
29418
|
+
}
|
|
29419
|
+
return payload;
|
|
29420
|
+
}
|
|
29321
29421
|
convertToolsForInterference(tools) {
|
|
29322
29422
|
return convertToolsToOpenAISchema(tools)[0];
|
|
29323
29423
|
}
|
|
@@ -29517,6 +29617,18 @@ ${message.content}` : message.content;
|
|
|
29517
29617
|
name: item.name,
|
|
29518
29618
|
arguments: item.arguments
|
|
29519
29619
|
});
|
|
29620
|
+
} else if (type === "message") {
|
|
29621
|
+
const content = Array.isArray(item.content) ? item.content : [];
|
|
29622
|
+
const textParts = [];
|
|
29623
|
+
for (const rawContent of content) {
|
|
29624
|
+
const contentItem = readObject(rawContent);
|
|
29625
|
+
if (contentItem.type === "output_text" && typeof contentItem.text === "string") {
|
|
29626
|
+
textParts.push(contentItem.text);
|
|
29627
|
+
}
|
|
29628
|
+
}
|
|
29629
|
+
if (!result.text && textParts.length > 0) {
|
|
29630
|
+
result.text = textParts.join("");
|
|
29631
|
+
}
|
|
29520
29632
|
}
|
|
29521
29633
|
}
|
|
29522
29634
|
return result;
|
|
@@ -29798,6 +29910,7 @@ var OpenAICompatibleCompletion = class _OpenAICompatibleCompletion extends OpenA
|
|
|
29798
29910
|
return _OpenAICompatibleCompletion.resolveHeaders(headers, config);
|
|
29799
29911
|
}
|
|
29800
29912
|
};
|
|
29913
|
+
registerLLMProviderFactory("openai", (options) => new OpenAICompletion(options));
|
|
29801
29914
|
for (const provider of Object.keys(OPENAI_COMPATIBLE_PROVIDERS)) {
|
|
29802
29915
|
registerLLMProviderFactory(provider, (options) => {
|
|
29803
29916
|
const { logprobs: _logprobs, ...compatibleOptions } = options;
|
|
@@ -29961,6 +30074,8 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
29961
30074
|
stop: options.stop,
|
|
29962
30075
|
stopSequences: options.stopSequences,
|
|
29963
30076
|
stop_sequences: options.stop_sequences,
|
|
30077
|
+
responseFormat: options.responseFormat,
|
|
30078
|
+
response_format: options.response_format,
|
|
29964
30079
|
maxTokens: options.maxTokens ?? options.max_tokens ?? 4096,
|
|
29965
30080
|
timeout: options.timeout ?? null
|
|
29966
30081
|
}));
|
|
@@ -29985,8 +30100,71 @@ var AnthropicCompletion = class _AnthropicCompletion extends ConfiguredLLM {
|
|
|
29985
30100
|
this.previousThinkingBlocks = [];
|
|
29986
30101
|
this._previous_thinking_blocks = this.previousThinkingBlocks;
|
|
29987
30102
|
}
|
|
29988
|
-
call(messages, options) {
|
|
29989
|
-
|
|
30103
|
+
async call(messages, options) {
|
|
30104
|
+
const apiKey = this.apiKey;
|
|
30105
|
+
if (!apiKey) {
|
|
30106
|
+
throw new Error("Anthropic API key required. Pass api_key when constructing AnthropicCompletion.");
|
|
30107
|
+
}
|
|
30108
|
+
if (this.stream) {
|
|
30109
|
+
throw new Error("Anthropic streaming responses are not supported by the built-in HTTP transport yet.");
|
|
30110
|
+
}
|
|
30111
|
+
const [formattedMessages, systemMessage] = this.formatMessagesForAnthropic(messages);
|
|
30112
|
+
const availableFunctions = options?.availableFunctions ?? options?.available_functions ?? null;
|
|
30113
|
+
const params = this.prepareCompletionParams(
|
|
30114
|
+
formattedMessages,
|
|
30115
|
+
systemMessage,
|
|
30116
|
+
options?.tools ?? null,
|
|
30117
|
+
availableFunctions
|
|
30118
|
+
);
|
|
30119
|
+
const schema = anthropicResponseSchema(options?.responseModel ?? this.responseFormat);
|
|
30120
|
+
if (schema) {
|
|
30121
|
+
const tools = Array.isArray(params.tools) ? params.tools.filter((tool2) => typeof tool2 === "object" && tool2 !== null) : [];
|
|
30122
|
+
tools.push({
|
|
30123
|
+
name: STRUCTURED_OUTPUT_TOOL_NAME,
|
|
30124
|
+
description: "Use this tool to provide your final structured response in the required JSON shape.",
|
|
30125
|
+
input_schema: sanitizeToolParamsForAnthropicStrict(structuredClone(schema)),
|
|
30126
|
+
strict: true
|
|
30127
|
+
});
|
|
30128
|
+
params.tools = tools;
|
|
30129
|
+
params.tool_choice = { type: "tool", name: STRUCTURED_OUTPUT_TOOL_NAME };
|
|
30130
|
+
}
|
|
30131
|
+
const baseUrl = this.baseUrl ?? "https://api.anthropic.com";
|
|
30132
|
+
const response = await fetch(`${baseUrl.replace(/\/$/, "")}/v1/messages`, {
|
|
30133
|
+
method: "POST",
|
|
30134
|
+
headers: {
|
|
30135
|
+
"content-type": "application/json",
|
|
30136
|
+
"x-api-key": apiKey,
|
|
30137
|
+
"anthropic-version": "2023-06-01"
|
|
30138
|
+
},
|
|
30139
|
+
body: JSON.stringify(params),
|
|
30140
|
+
...options?.signal ? { signal: options.signal } : {}
|
|
30141
|
+
});
|
|
30142
|
+
const body = await response.json().catch(() => ({}));
|
|
30143
|
+
if (!response.ok) {
|
|
30144
|
+
const error = readObject2(readObject2(body).error);
|
|
30145
|
+
throw new Error(scalarToString(error.message) ?? `Anthropic request failed with HTTP ${response.status.toString()}.`);
|
|
30146
|
+
}
|
|
30147
|
+
const usage = this.extractAnthropicTokenUsage(body);
|
|
30148
|
+
if (usage.total_tokens !== 0) {
|
|
30149
|
+
this.trackTokenUsageInternal(usage);
|
|
30150
|
+
}
|
|
30151
|
+
const thinkingBlocks = _AnthropicCompletion.extractThinkingBlocksFromResponse(body);
|
|
30152
|
+
if (thinkingBlocks.length > 0) {
|
|
30153
|
+
this.previousThinkingBlocks = thinkingBlocks;
|
|
30154
|
+
this._previous_thinking_blocks = this.previousThinkingBlocks;
|
|
30155
|
+
}
|
|
30156
|
+
const structuredOutput = _AnthropicCompletion.extractStructuredOutputFromResponse(body);
|
|
30157
|
+
if (structuredOutput) {
|
|
30158
|
+
return structuredOutput;
|
|
30159
|
+
}
|
|
30160
|
+
const toolUses = _AnthropicCompletion.extractToolUsesFromResponse(body);
|
|
30161
|
+
if (toolUses.length > 0) {
|
|
30162
|
+
if (availableFunctions && Object.keys(availableFunctions).length > 0) {
|
|
30163
|
+
return await this.executeFirstTool(toolUses, availableFunctions);
|
|
30164
|
+
}
|
|
30165
|
+
return toolUses;
|
|
30166
|
+
}
|
|
30167
|
+
return this.applyStopWords(anthropicResponseText(body));
|
|
29990
30168
|
}
|
|
29991
30169
|
async acall(messages, options) {
|
|
29992
30170
|
return await super.acall(messages, options);
|
|
@@ -30479,7 +30657,7 @@ var BedrockCompletion = class _BedrockCompletion extends ConfiguredLLM {
|
|
|
30479
30657
|
maxTokens: options.maxTokens ?? options.max_tokens ?? null,
|
|
30480
30658
|
timeout: options.timeout ?? null
|
|
30481
30659
|
}));
|
|
30482
|
-
this.regionName = options.regionName ?? options.region_name ??
|
|
30660
|
+
this.regionName = options.regionName ?? options.region_name ?? process.env.AWS_DEFAULT_REGION ?? process.env.AWS_REGION_NAME ?? process.env.AWS_REGION ?? "us-east-1";
|
|
30483
30661
|
this.region_name = this.regionName;
|
|
30484
30662
|
this.session = options.session ?? null;
|
|
30485
30663
|
this.timeout = options.timeout ?? null;
|
|
@@ -30500,8 +30678,32 @@ var BedrockCompletion = class _BedrockCompletion extends ConfiguredLLM {
|
|
|
30500
30678
|
this.additional_model_response_field_paths = this.additionalModelResponseFieldPaths;
|
|
30501
30679
|
this.interceptor = null;
|
|
30502
30680
|
}
|
|
30503
|
-
call(messages, options) {
|
|
30504
|
-
|
|
30681
|
+
async call(messages, options) {
|
|
30682
|
+
if (this.stream) {
|
|
30683
|
+
throw new Error("Bedrock streaming responses are not supported by the built-in client transport yet.");
|
|
30684
|
+
}
|
|
30685
|
+
const client = this.getConverseClient();
|
|
30686
|
+
const { messages: formattedMessages, body } = this.prepareConverseRequestBody(
|
|
30687
|
+
messages,
|
|
30688
|
+
options?.tools ?? null
|
|
30689
|
+
);
|
|
30690
|
+
const request = {
|
|
30691
|
+
modelId: this.model,
|
|
30692
|
+
messages: formattedMessages,
|
|
30693
|
+
...body
|
|
30694
|
+
};
|
|
30695
|
+
const response = await client.converse(request);
|
|
30696
|
+
this.trackTokenUsageInternal(readObject2(readObject2(response).usage));
|
|
30697
|
+
const toolUses = _BedrockCompletion.extractToolUsesFromResponse(response);
|
|
30698
|
+
const availableFunctions = options?.availableFunctions ?? options?.available_functions ?? null;
|
|
30699
|
+
if (toolUses.length > 0) {
|
|
30700
|
+
if (availableFunctions && Object.keys(availableFunctions).length > 0) {
|
|
30701
|
+
const executed = await this.executeToolUseAndPrepareMessages(formattedMessages, toolUses[0], availableFunctions);
|
|
30702
|
+
return executed.result;
|
|
30703
|
+
}
|
|
30704
|
+
return toolUses;
|
|
30705
|
+
}
|
|
30706
|
+
return bedrockResponseText(response);
|
|
30505
30707
|
}
|
|
30506
30708
|
async acall(messages, options) {
|
|
30507
30709
|
return await super.acall(messages, options);
|
|
@@ -30576,7 +30778,14 @@ var BedrockCompletion = class _BedrockCompletion extends ConfiguredLLM {
|
|
|
30576
30778
|
return this.getFileUploader();
|
|
30577
30779
|
}
|
|
30578
30780
|
toConfigDict() {
|
|
30579
|
-
return
|
|
30781
|
+
return {
|
|
30782
|
+
...super.toConfigDict(),
|
|
30783
|
+
...this.regionName && this.regionName !== "us-east-1" ? { region_name: this.regionName } : {},
|
|
30784
|
+
...this.maxTokens === null ? {} : { max_tokens: this.maxTokens },
|
|
30785
|
+
...this.topP === null ? {} : { top_p: this.topP },
|
|
30786
|
+
...this.topK === null ? {} : { top_k: this.topK },
|
|
30787
|
+
...this.guardrailConfig ? { guardrail_config: this.guardrailConfig } : {}
|
|
30788
|
+
};
|
|
30580
30789
|
}
|
|
30581
30790
|
to_config_dict() {
|
|
30582
30791
|
return this.toConfigDict();
|
|
@@ -30924,6 +31133,24 @@ ${content}` : content;
|
|
|
30924
31133
|
const model = this.model.toLowerCase();
|
|
30925
31134
|
return model.includes("anthropic") || model.includes("claude");
|
|
30926
31135
|
}
|
|
31136
|
+
getConverseClient() {
|
|
31137
|
+
const direct = readObject2(this.session).converse;
|
|
31138
|
+
if (typeof direct === "function") {
|
|
31139
|
+
return { converse: (request) => Reflect.apply(direct, this.session, [request]) };
|
|
31140
|
+
}
|
|
31141
|
+
const clientFactory = readObject2(this.session).client;
|
|
31142
|
+
if (typeof clientFactory === "function") {
|
|
31143
|
+
const client = Reflect.apply(clientFactory, this.session, [
|
|
31144
|
+
"bedrock-runtime",
|
|
31145
|
+
this.regionName ? { region: this.regionName } : void 0
|
|
31146
|
+
]);
|
|
31147
|
+
const converse = readObject2(client).converse;
|
|
31148
|
+
if (typeof converse === "function") {
|
|
31149
|
+
return { converse: (request) => Reflect.apply(converse, client, [request]) };
|
|
31150
|
+
}
|
|
31151
|
+
}
|
|
31152
|
+
throw new Error("Bedrock live calls require a session/client with a converse(request) method.");
|
|
31153
|
+
}
|
|
30927
31154
|
};
|
|
30928
31155
|
var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
30929
31156
|
project;
|
|
@@ -30972,8 +31199,8 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
30972
31199
|
timeout: options.timeout ?? null
|
|
30973
31200
|
}));
|
|
30974
31201
|
this.project = options.project ?? process.env.GOOGLE_CLOUD_PROJECT ?? null;
|
|
30975
|
-
this.location = options.location ?? "us-central1";
|
|
30976
|
-
this.useVertexai = options.useVertexai ?? options.use_vertexai ??
|
|
31202
|
+
this.location = options.location ?? process.env.GOOGLE_CLOUD_LOCATION ?? "us-central1";
|
|
31203
|
+
this.useVertexai = options.useVertexai ?? options.use_vertexai ?? process.env.GOOGLE_GENAI_USE_VERTEXAI?.toLowerCase() === "true";
|
|
30977
31204
|
this.use_vertexai = this.useVertexai;
|
|
30978
31205
|
this.timeout = options.timeout ?? null;
|
|
30979
31206
|
this.maxRetries = options.maxRetries ?? options.max_retries ?? 2;
|
|
@@ -31001,7 +31228,7 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
31001
31228
|
}
|
|
31002
31229
|
call(messages, options) {
|
|
31003
31230
|
if (this.useVertexai) {
|
|
31004
|
-
return
|
|
31231
|
+
return this.callVertexAI(messages, options);
|
|
31005
31232
|
}
|
|
31006
31233
|
const apiKey = this.apiKey ?? process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY ?? null;
|
|
31007
31234
|
if (!apiKey) {
|
|
@@ -31055,6 +31282,58 @@ var GeminiCompletion = class _GeminiCompletion extends ConfiguredLLM {
|
|
|
31055
31282
|
);
|
|
31056
31283
|
});
|
|
31057
31284
|
}
|
|
31285
|
+
async callVertexAI(messages, options) {
|
|
31286
|
+
if (!this.project) {
|
|
31287
|
+
throw new Error("Vertex AI Gemini calls require a project.");
|
|
31288
|
+
}
|
|
31289
|
+
const clientParams = readObject2(this.clientParams);
|
|
31290
|
+
const accessToken = scalarToString(clientParams.access_token ?? clientParams.accessToken ?? this.apiKey);
|
|
31291
|
+
if (!accessToken) {
|
|
31292
|
+
throw new Error("Vertex AI Gemini calls require client_params.access_token or api_key.");
|
|
31293
|
+
}
|
|
31294
|
+
const [contents, systemInstruction] = this.formatMessagesForGemini(messages);
|
|
31295
|
+
const tools = options?.tools ?? null;
|
|
31296
|
+
const generationConfig = this.prepareGenerationConfig(
|
|
31297
|
+
systemInstruction,
|
|
31298
|
+
tools,
|
|
31299
|
+
options?.responseModel ?? null
|
|
31300
|
+
);
|
|
31301
|
+
const requestBody = readObject2(generationConfig);
|
|
31302
|
+
const generationConfigBody = { ...generationConfig };
|
|
31303
|
+
delete generationConfigBody.system_instruction;
|
|
31304
|
+
delete generationConfigBody.tools;
|
|
31305
|
+
delete generationConfigBody.safety_settings;
|
|
31306
|
+
const model = this.model.replace(/^(?:gemini|google)\//, "");
|
|
31307
|
+
const baseUrl = this.baseUrl ?? `https://${this.location}-aiplatform.googleapis.com/v1`;
|
|
31308
|
+
const url = `${baseUrl.replace(/\/$/, "")}/projects/${encodeURIComponent(this.project)}/locations/${encodeURIComponent(this.location)}/publishers/google/models/${encodeURIComponent(model)}:generateContent`;
|
|
31309
|
+
const response = await fetch(url, {
|
|
31310
|
+
method: "POST",
|
|
31311
|
+
headers: {
|
|
31312
|
+
Authorization: `Bearer ${accessToken}`,
|
|
31313
|
+
"Content-Type": "application/json"
|
|
31314
|
+
},
|
|
31315
|
+
body: JSON.stringify({
|
|
31316
|
+
contents,
|
|
31317
|
+
...Object.keys(generationConfigBody).length > 0 ? { generationConfig: generationConfigBody } : {},
|
|
31318
|
+
..."system_instruction" in requestBody ? { systemInstruction: requestBody.system_instruction } : {},
|
|
31319
|
+
..."tools" in requestBody ? { tools: requestBody.tools } : {},
|
|
31320
|
+
..."safety_settings" in requestBody ? { safetySettings: requestBody.safety_settings } : {}
|
|
31321
|
+
}),
|
|
31322
|
+
...options?.signal ? { signal: options.signal } : {}
|
|
31323
|
+
});
|
|
31324
|
+
const body = await response.json().catch(() => ({}));
|
|
31325
|
+
if (!response.ok) {
|
|
31326
|
+
const error = readObject2(readObject2(body).error);
|
|
31327
|
+
throw new Error(scalarToString(error.message) ?? `Vertex AI Gemini request failed with HTTP ${response.status.toString()}.`);
|
|
31328
|
+
}
|
|
31329
|
+
return await this.processResponseWithTools(
|
|
31330
|
+
body,
|
|
31331
|
+
contents,
|
|
31332
|
+
options?.availableFunctions ?? options?.available_functions ?? null,
|
|
31333
|
+
null,
|
|
31334
|
+
geminiMaxToolRounds(options)
|
|
31335
|
+
);
|
|
31336
|
+
}
|
|
31058
31337
|
async acall(messages, options) {
|
|
31059
31338
|
return await this.call(this.formatMessages(messages), options);
|
|
31060
31339
|
}
|
|
@@ -32062,7 +32341,7 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
32062
32341
|
if (this._responses_delegate) {
|
|
32063
32342
|
return this._responses_delegate.call(messages, options);
|
|
32064
32343
|
}
|
|
32065
|
-
return
|
|
32344
|
+
return this.callChatCompletions(messages, options);
|
|
32066
32345
|
}
|
|
32067
32346
|
async acall(messages, options) {
|
|
32068
32347
|
if (this._responses_delegate) {
|
|
@@ -32195,7 +32474,11 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
32195
32474
|
}
|
|
32196
32475
|
return {
|
|
32197
32476
|
endpoint,
|
|
32198
|
-
api_key: apiKey
|
|
32477
|
+
...apiKey ? { api_key: apiKey } : {
|
|
32478
|
+
credential: {
|
|
32479
|
+
provider: "DefaultAzureCredential"
|
|
32480
|
+
}
|
|
32481
|
+
},
|
|
32199
32482
|
api_version: this.apiVersion,
|
|
32200
32483
|
...credentialScopes ? { credential_scopes: credentialScopes } : {}
|
|
32201
32484
|
};
|
|
@@ -32211,9 +32494,6 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
32211
32494
|
if (!kwargs.endpoint) {
|
|
32212
32495
|
throw new Error("Azure endpoint is required");
|
|
32213
32496
|
}
|
|
32214
|
-
if (!kwargs.api_key) {
|
|
32215
|
-
throw new Error("Azure API key is required");
|
|
32216
|
-
}
|
|
32217
32497
|
this._client = {
|
|
32218
32498
|
provider: "azure",
|
|
32219
32499
|
model: this.model,
|
|
@@ -32276,6 +32556,50 @@ var AzureCompletion = class _AzureCompletion extends ConfiguredLLM {
|
|
|
32276
32556
|
_prepare_completion_params(messages, tools = null) {
|
|
32277
32557
|
return this.prepareCompletionParams(messages, tools);
|
|
32278
32558
|
}
|
|
32559
|
+
async callChatCompletions(messages, options) {
|
|
32560
|
+
if (this.stream) {
|
|
32561
|
+
throw new Error("Azure streaming responses are not supported by the built-in fetch transport yet.");
|
|
32562
|
+
}
|
|
32563
|
+
const client = this.getSyncClient();
|
|
32564
|
+
const endpoint = scalarToString(client.endpoint);
|
|
32565
|
+
const apiKey = scalarToString(client.api_key);
|
|
32566
|
+
if (!endpoint) {
|
|
32567
|
+
throw new Error("Azure endpoint is required");
|
|
32568
|
+
}
|
|
32569
|
+
if (!apiKey) {
|
|
32570
|
+
throw new Error("Azure API key is required");
|
|
32571
|
+
}
|
|
32572
|
+
const params = this.prepareCompletionParams(
|
|
32573
|
+
this.formatMessages(messages),
|
|
32574
|
+
options?.tools ?? null
|
|
32575
|
+
);
|
|
32576
|
+
const url = azureChatCompletionsUrl(endpoint, this.model, this.apiVersion);
|
|
32577
|
+
const response = await fetch(url, {
|
|
32578
|
+
method: "POST",
|
|
32579
|
+
headers: {
|
|
32580
|
+
"Content-Type": "application/json",
|
|
32581
|
+
"api-key": apiKey
|
|
32582
|
+
},
|
|
32583
|
+
body: JSON.stringify(params)
|
|
32584
|
+
});
|
|
32585
|
+
const payload = await response.json().catch(() => ({}));
|
|
32586
|
+
if (!response.ok) {
|
|
32587
|
+
const error = readObject2(readObject2(payload).error);
|
|
32588
|
+
throw new Error(scalarToString(error.message) ?? `Azure completion request failed with HTTP ${response.status.toString()}.`);
|
|
32589
|
+
}
|
|
32590
|
+
const usage = this.extractAzureTokenUsage(payload);
|
|
32591
|
+
if (usage.total_tokens !== 0) {
|
|
32592
|
+
this.trackTokenUsageInternal(usage);
|
|
32593
|
+
}
|
|
32594
|
+
const choices = readObject2(payload).choices;
|
|
32595
|
+
const firstChoice = Array.isArray(choices) ? readObject2(choices[0]) : {};
|
|
32596
|
+
const message = readObject2(firstChoice.message);
|
|
32597
|
+
const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
|
|
32598
|
+
if (toolCalls.length > 0) {
|
|
32599
|
+
return toolCalls;
|
|
32600
|
+
}
|
|
32601
|
+
return typeof message.content === "string" ? message.content : "";
|
|
32602
|
+
}
|
|
32279
32603
|
prepareResponsesParams(messages, tools = null, responseModel = null) {
|
|
32280
32604
|
if (!this._responses_delegate) {
|
|
32281
32605
|
throw new Error("Azure Responses API is only available when api is set to 'responses'.");
|
|
@@ -32573,15 +32897,29 @@ function geminiResponseSchema(value) {
|
|
|
32573
32897
|
const schema = schemaProvider.model_json_schema?.() ?? schemaProvider.modelJsonSchema?.() ?? schemaProvider.schema;
|
|
32574
32898
|
return schema && typeof schema === "object" && !Array.isArray(schema) ? schema : null;
|
|
32575
32899
|
}
|
|
32900
|
+
function anthropicResponseSchema(value) {
|
|
32901
|
+
if (!value || typeof value !== "object") {
|
|
32902
|
+
return null;
|
|
32903
|
+
}
|
|
32904
|
+
const schemaProvider = value;
|
|
32905
|
+
const schema = schemaProvider.model_json_schema?.() ?? schemaProvider.modelJsonSchema?.() ?? schemaProvider.schema;
|
|
32906
|
+
return schema && typeof schema === "object" && !Array.isArray(schema) ? schema : null;
|
|
32907
|
+
}
|
|
32576
32908
|
function anthropicResponseContent(response) {
|
|
32577
32909
|
const content = readObject2(response).content;
|
|
32578
32910
|
return Array.isArray(content) ? content : [];
|
|
32579
32911
|
}
|
|
32912
|
+
function anthropicResponseText(response) {
|
|
32913
|
+
return anthropicResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
32914
|
+
}
|
|
32580
32915
|
function bedrockResponseContent(response) {
|
|
32581
32916
|
const output4 = readObject2(readObject2(response).output);
|
|
32582
32917
|
const message = readObject2(output4.message);
|
|
32583
32918
|
return Array.isArray(message.content) ? message.content : [];
|
|
32584
32919
|
}
|
|
32920
|
+
function bedrockResponseText(response) {
|
|
32921
|
+
return bedrockResponseContent(response).map((block) => scalarToString(readObject2(block).text)).filter((text) => text !== null).join("");
|
|
32922
|
+
}
|
|
32585
32923
|
function isAzureOpenAIEndpoint(endpoint) {
|
|
32586
32924
|
if (!endpoint) {
|
|
32587
32925
|
return false;
|
|
@@ -32612,6 +32950,14 @@ function azureResponsesBaseUrl(endpoint) {
|
|
|
32612
32950
|
return `${trimmed.replace(/\/openai(?:\/v1)?$/i, "")}/openai/v1/`;
|
|
32613
32951
|
}
|
|
32614
32952
|
}
|
|
32953
|
+
function azureChatCompletionsUrl(endpoint, model, apiVersion) {
|
|
32954
|
+
const version2 = encodeURIComponent(apiVersion ?? "2024-06-01");
|
|
32955
|
+
const trimmed = endpoint.replace(/\/+$/, "");
|
|
32956
|
+
const deploymentPath = "/openai/deployments/";
|
|
32957
|
+
const lower = trimmed.toLowerCase();
|
|
32958
|
+
const base = lower.includes(deploymentPath) ? trimmed : `${trimmed.replace(/\/openai(?:\/v1)?$/i, "")}${deploymentPath}${encodeURIComponent(azureResponsesModelName(model))}`;
|
|
32959
|
+
return `${base}/chat/completions?api-version=${version2}`;
|
|
32960
|
+
}
|
|
32615
32961
|
function geminiContextWindowSize(model) {
|
|
32616
32962
|
const windows = {
|
|
32617
32963
|
"gemini-3-pro-preview": 1048576,
|
|
@@ -219,6 +219,9 @@ export declare class OpenAICompletion extends ConfiguredLLM {
|
|
|
219
219
|
_prepare_completion_params(messages: readonly LLMMessage[], tools?: readonly Tool[] | null): Record<string, unknown>;
|
|
220
220
|
prepareResponsesParams(messages: readonly LLMMessage[], tools?: readonly Tool[] | null, responseModel?: unknown): Record<string, unknown>;
|
|
221
221
|
_prepare_responses_params(messages: readonly LLMMessage[], tools?: readonly Tool[] | null, responseModel?: unknown): Record<string, unknown>;
|
|
222
|
+
private callChatCompletions;
|
|
223
|
+
private callResponses;
|
|
224
|
+
private fetchOpenAI;
|
|
222
225
|
convertToolsForInterference(tools: readonly Tool[]): Record<string, unknown>[];
|
|
223
226
|
_convert_tools_for_interference(tools: readonly Tool[]): Record<string, unknown>[];
|
|
224
227
|
convertToolsForResponses(tools: readonly Tool[]): Record<string, unknown>[];
|
|
@@ -268,6 +268,7 @@ export declare class BedrockCompletion extends ConfiguredLLM {
|
|
|
268
268
|
trackTokenUsageInternal(usageData: Record<string, unknown>): void;
|
|
269
269
|
_track_token_usage_internal(usageData: Record<string, unknown>): void;
|
|
270
270
|
private isClaudeModel;
|
|
271
|
+
private getConverseClient;
|
|
271
272
|
}
|
|
272
273
|
export type GeminiCompletionOptions = BaseLLMOptions & {
|
|
273
274
|
project?: string | null;
|
|
@@ -322,6 +323,7 @@ export declare class GeminiCompletion extends ConfiguredLLM {
|
|
|
322
323
|
tools: readonly Tool[] | null;
|
|
323
324
|
constructor(options?: GeminiCompletionOptions);
|
|
324
325
|
call(messages: readonly LLMMessage[], options?: LLMCallOptions): Promise<LLMResponse>;
|
|
326
|
+
private callVertexAI;
|
|
325
327
|
acall(messages: LLMMessageInput, options?: LLMCallOptions): Promise<LLMResponse>;
|
|
326
328
|
supportsFunctionCalling(): boolean;
|
|
327
329
|
supports_function_calling(): boolean;
|
|
@@ -579,6 +581,7 @@ export declare class AzureCompletion extends ConfiguredLLM {
|
|
|
579
581
|
_get_sync_client(env?: NodeJS.ProcessEnv): Record<string, unknown>;
|
|
580
582
|
prepareCompletionParams(messages: readonly LLMMessage[], tools?: readonly Tool[] | null): AzureCompletionParams;
|
|
581
583
|
_prepare_completion_params(messages: readonly LLMMessage[], tools?: readonly Tool[] | null): AzureCompletionParams;
|
|
584
|
+
private callChatCompletions;
|
|
582
585
|
prepareResponsesParams(messages: readonly LLMMessage[], tools?: readonly Tool[] | null, responseModel?: unknown): Record<string, unknown>;
|
|
583
586
|
_prepare_responses_params(messages: readonly LLMMessage[], tools?: readonly Tool[] | null, responseModel?: unknown): Record<string, unknown>;
|
|
584
587
|
convertToolsForInterference(tools: readonly Tool[]): Record<string, unknown>[];
|