@sweetoburrito/backstage-plugin-ai-assistant-backend-module-model-provider-azure-ai 0.3.0 → 0.4.0
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/config.d.ts +5 -2
- package/dist/module.cjs.js +4 -5
- package/dist/module.cjs.js.map +1 -1
- package/dist/{azure-ai-inference-chat-model.cjs.js → services/chat-model/azure-ai-inference-chat-model.cjs.js} +5 -1
- package/dist/services/chat-model/azure-ai-inference-chat-model.cjs.js.map +1 -0
- package/dist/services/chat-model/index.cjs.js +18 -0
- package/dist/services/chat-model/index.cjs.js.map +1 -0
- package/dist/services/chat-model/open-ai.cjs.js +16 -0
- package/dist/services/chat-model/open-ai.cjs.js.map +1 -0
- package/package.json +3 -2
- package/dist/azure-ai-inference-chat-model.cjs.js.map +0 -1
package/config.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { SdkType } from './src/types/chat-model';
|
|
2
|
+
|
|
1
3
|
export interface Config {
|
|
2
4
|
aiAssistant: {
|
|
3
5
|
models: {
|
|
4
|
-
|
|
6
|
+
azureAi: {
|
|
5
7
|
/**
|
|
6
8
|
* @visibility secret
|
|
7
9
|
*/
|
|
@@ -9,7 +11,8 @@ export interface Config {
|
|
|
9
11
|
models: {
|
|
10
12
|
modelName: string;
|
|
11
13
|
endpoint: string;
|
|
12
|
-
|
|
14
|
+
sdk?: SdkType;
|
|
15
|
+
}[];
|
|
13
16
|
};
|
|
14
17
|
};
|
|
15
18
|
};
|
package/dist/module.cjs.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var backendPluginApi = require('@backstage/backend-plugin-api');
|
|
4
4
|
var backstagePluginAiAssistantNode = require('@sweetoburrito/backstage-plugin-ai-assistant-node');
|
|
5
|
-
var
|
|
5
|
+
var index = require('./services/chat-model/index.cjs.js');
|
|
6
6
|
|
|
7
7
|
const aiAssistantModuleModelProviderAzureAi = backendPluginApi.createBackendModule({
|
|
8
8
|
pluginId: "ai-assistant",
|
|
@@ -14,15 +14,14 @@ const aiAssistantModuleModelProviderAzureAi = backendPluginApi.createBackendModu
|
|
|
14
14
|
modelProvider: backstagePluginAiAssistantNode.modelProviderExtensionPoint
|
|
15
15
|
},
|
|
16
16
|
async init({ config, modelProvider }) {
|
|
17
|
-
const azureConfig = config.getConfig(
|
|
18
|
-
"aiAssistant.models.azureAiInference"
|
|
19
|
-
);
|
|
17
|
+
const azureConfig = config.getConfig("aiAssistant.models.azureAi");
|
|
20
18
|
const apiKey = azureConfig.getString("apiKey");
|
|
21
19
|
const modelConfigs = azureConfig.getOptionalConfigArray("models");
|
|
22
20
|
const models = modelConfigs?.map((modelConfig) => {
|
|
23
21
|
const endpoint = modelConfig.getString("endpoint");
|
|
24
22
|
const modelName = modelConfig.getString("modelName");
|
|
25
|
-
const
|
|
23
|
+
const sdk = modelConfig.getOptionalString("sdk") ?? "openai";
|
|
24
|
+
const chatModel = index.createChatModeForSdk(sdk, {
|
|
26
25
|
apiKey,
|
|
27
26
|
endpoint,
|
|
28
27
|
modelName
|
package/dist/module.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"module.cjs.js","sources":["../src/module.ts"],"sourcesContent":["import {\n coreServices,\n createBackendModule,\n} from '@backstage/backend-plugin-api';\n\nimport {\n Model,\n modelProviderExtensionPoint,\n} from '@sweetoburrito/backstage-plugin-ai-assistant-node';\nimport {
|
|
1
|
+
{"version":3,"file":"module.cjs.js","sources":["../src/module.ts"],"sourcesContent":["import {\n coreServices,\n createBackendModule,\n} from '@backstage/backend-plugin-api';\n\nimport {\n Model,\n modelProviderExtensionPoint,\n} from '@sweetoburrito/backstage-plugin-ai-assistant-node';\nimport { createChatModeForSdk } from './services/chat-model';\n\nexport const aiAssistantModuleModelProviderAzureAi = createBackendModule({\n pluginId: 'ai-assistant',\n moduleId: 'model-provider-azure-ai',\n register(reg) {\n reg.registerInit({\n deps: {\n config: coreServices.rootConfig,\n modelProvider: modelProviderExtensionPoint,\n },\n async init({ config, modelProvider }) {\n const azureConfig = config.getConfig('aiAssistant.models.azureAi');\n\n const apiKey = azureConfig.getString('apiKey');\n const modelConfigs = azureConfig.getOptionalConfigArray('models');\n\n const models: Model[] =\n modelConfigs?.map<Model>(modelConfig => {\n const endpoint = modelConfig.getString('endpoint');\n const modelName = modelConfig.getString('modelName');\n const sdk = modelConfig.getOptionalString('sdk') ?? 'openai';\n\n const chatModel = createChatModeForSdk(sdk, {\n apiKey,\n endpoint,\n modelName,\n });\n\n return {\n id: modelName,\n chatModel,\n };\n }) ?? [];\n\n models.forEach(model => modelProvider.register(model));\n },\n });\n },\n});\n"],"names":["createBackendModule","coreServices","modelProviderExtensionPoint","createChatModeForSdk"],"mappings":";;;;;;AAWO,MAAM,wCAAwCA,oCAAA,CAAoB;AAAA,EACvE,QAAA,EAAU,cAAA;AAAA,EACV,QAAA,EAAU,yBAAA;AAAA,EACV,SAAS,GAAA,EAAK;AACZ,IAAA,GAAA,CAAI,YAAA,CAAa;AAAA,MACf,IAAA,EAAM;AAAA,QACJ,QAAQC,6BAAA,CAAa,UAAA;AAAA,QACrB,aAAA,EAAeC;AAAA,OACjB;AAAA,MACA,MAAM,IAAA,CAAK,EAAE,MAAA,EAAQ,eAAc,EAAG;AACpC,QAAA,MAAM,WAAA,GAAc,MAAA,CAAO,SAAA,CAAU,4BAA4B,CAAA;AAEjE,QAAA,MAAM,MAAA,GAAS,WAAA,CAAY,SAAA,CAAU,QAAQ,CAAA;AAC7C,QAAA,MAAM,YAAA,GAAe,WAAA,CAAY,sBAAA,CAAuB,QAAQ,CAAA;AAEhE,QAAA,MAAM,MAAA,GACJ,YAAA,EAAc,GAAA,CAAW,CAAA,WAAA,KAAe;AACtC,UAAA,MAAM,QAAA,GAAW,WAAA,CAAY,SAAA,CAAU,UAAU,CAAA;AACjD,UAAA,MAAM,SAAA,GAAY,WAAA,CAAY,SAAA,CAAU,WAAW,CAAA;AACnD,UAAA,MAAM,GAAA,GAAM,WAAA,CAAY,iBAAA,CAAkB,KAAK,CAAA,IAAK,QAAA;AAEpD,UAAA,MAAM,SAAA,GAAYC,2BAAqB,GAAA,EAAK;AAAA,YAC1C,MAAA;AAAA,YACA,QAAA;AAAA,YACA;AAAA,WACD,CAAA;AAED,UAAA,OAAO;AAAA,YACL,EAAA,EAAI,SAAA;AAAA,YACJ;AAAA,WACF;AAAA,QACF,CAAC,KAAK,EAAC;AAET,QAAA,MAAA,CAAO,OAAA,CAAQ,CAAA,KAAA,KAAS,aAAA,CAAc,QAAA,CAAS,KAAK,CAAC,CAAA;AAAA,MACvD;AAAA,KACD,CAAA;AAAA,EACH;AACF,CAAC;;;;"}
|
|
@@ -6,7 +6,7 @@ var coreAuth = require('@azure/core-auth');
|
|
|
6
6
|
var messages = require('@langchain/core/messages');
|
|
7
7
|
var stream = require('@langchain/core/utils/stream');
|
|
8
8
|
var outputs = require('@langchain/core/outputs');
|
|
9
|
-
var utils = require('
|
|
9
|
+
var utils = require('../../utils.cjs.js');
|
|
10
10
|
var coreSse = require('@azure/core-sse');
|
|
11
11
|
|
|
12
12
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
@@ -100,6 +100,10 @@ class AzureAiInferenceChatModel extends chat_models.BaseChatModel {
|
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
+
const createAzureAiInferenceChatModel = (options) => {
|
|
104
|
+
return new AzureAiInferenceChatModel(options);
|
|
105
|
+
};
|
|
103
106
|
|
|
104
107
|
exports.AzureAiInferenceChatModel = AzureAiInferenceChatModel;
|
|
108
|
+
exports.createAzureAiInferenceChatModel = createAzureAiInferenceChatModel;
|
|
105
109
|
//# sourceMappingURL=azure-ai-inference-chat-model.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"azure-ai-inference-chat-model.cjs.js","sources":["../../../src/services/chat-model/azure-ai-inference-chat-model.ts"],"sourcesContent":["import {\n BaseChatModel,\n BaseChatModelParams,\n} from '@langchain/core/language_models/chat_models';\nimport createClient, { ModelClient } from '@azure-rest/ai-inference';\nimport { AzureKeyCredential } from '@azure/core-auth';\nimport {\n AIMessage,\n AIMessageChunk,\n BaseMessage,\n} from '@langchain/core/messages';\nimport { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport { concat } from '@langchain/core/utils/stream';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport { convertToAzureAiInferenceMessages } from '../../utils';\nimport { createSseStream } from '@azure/core-sse';\nimport { CreateChatModelFunction } from '../../types/chat-model';\n\nexport interface ChatAzureAiInferenceInputs extends BaseChatModelParams {\n modelName: string;\n endpoint: string;\n apiKey: string;\n}\n\nexport class AzureAiInferenceChatModel\n extends BaseChatModel\n implements ChatAzureAiInferenceInputs\n{\n modelName: string;\n endpoint: string;\n apiKey: string;\n private client: ModelClient;\n\n constructor({\n modelName,\n endpoint,\n apiKey,\n ...rest\n }: ChatAzureAiInferenceInputs) {\n super(rest);\n this.modelName = modelName;\n this.endpoint = endpoint;\n this.apiKey = apiKey;\n this.client = createClient(endpoint, new AzureKeyCredential(apiKey));\n }\n\n _llmType(): string {\n return 'azure-ai-inference';\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n _options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun,\n ): AsyncGenerator<ChatGenerationChunk> {\n const aiInferenceMessages = convertToAzureAiInferenceMessages(messages);\n\n const response = await this.client\n .path('/chat/completions')\n .post({\n body: {\n stream: true,\n messages: aiInferenceMessages,\n model: this.modelName,\n },\n })\n .asNodeStream();\n\n const stream = response.body;\n\n if (!stream) {\n throw new Error('Azure AI Inference response stream is undefined');\n }\n\n if (response.status !== '200') {\n stream.destroy();\n throw new Error(\n `Failed to get chat completions. Operation failed with ${response.status} code.`,\n );\n }\n\n const sseStream = createSseStream(stream);\n\n for await (const event of sseStream) {\n if (event.data === '[DONE]') {\n return;\n }\n\n for (const choice of JSON.parse(event.data).choices) {\n const token = choice.delta?.content ?? '';\n\n const responseMessage = new AIMessageChunk({\n content: token,\n });\n\n yield new ChatGenerationChunk({\n text: token,\n message: responseMessage,\n });\n await runManager?.handleLLMNewToken(token);\n }\n }\n }\n\n async _generate(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun,\n ): Promise<ChatResult> {\n let finalChunk: AIMessageChunk | undefined;\n for await (const chunk of this._streamResponseChunks(\n messages,\n options,\n runManager,\n )) {\n if (!finalChunk) {\n finalChunk = chunk.message;\n } else {\n finalChunk = concat(finalChunk, chunk.message);\n }\n }\n\n // Convert from AIMessageChunk to AIMessage since `generate` expects AIMessage.\n const nonChunkMessage = new AIMessage({\n id: finalChunk?.id,\n content: finalChunk?.content ?? '',\n tool_calls: finalChunk?.tool_calls,\n response_metadata: finalChunk?.response_metadata,\n usage_metadata: finalChunk?.usage_metadata,\n });\n return {\n generations: [\n {\n text:\n typeof nonChunkMessage.content === 'string'\n ? nonChunkMessage.content\n : '',\n message: nonChunkMessage,\n },\n ],\n };\n }\n}\n\nexport const createAzureAiInferenceChatModel: CreateChatModelFunction =\n options => {\n return new AzureAiInferenceChatModel(options);\n };\n"],"names":["BaseChatModel","createClient","AzureKeyCredential","messages","convertToAzureAiInferenceMessages","createSseStream","AIMessageChunk","ChatGenerationChunk","concat","AIMessage"],"mappings":";;;;;;;;;;;;;;;AAwBO,MAAM,kCACHA,yBAAA,CAEV;AAAA,EACE,SAAA;AAAA,EACA,QAAA;AAAA,EACA,MAAA;AAAA,EACQ,MAAA;AAAA,EAER,WAAA,CAAY;AAAA,IACV,SAAA;AAAA,IACA,QAAA;AAAA,IACA,MAAA;AAAA,IACA,GAAG;AAAA,GACL,EAA+B;AAC7B,IAAA,KAAA,CAAM,IAAI,CAAA;AACV,IAAA,IAAA,CAAK,SAAA,GAAY,SAAA;AACjB,IAAA,IAAA,CAAK,QAAA,GA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;AAC9C;;;;;"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var azureAiInferenceChatModel = require('./azure-ai-inference-chat-model.cjs.js');
|
|
4
|
+
var openAi = require('./open-ai.cjs.js');
|
|
5
|
+
|
|
6
|
+
const chatModels = {
|
|
7
|
+
openai: openAi.createOpenAiChatModel,
|
|
8
|
+
azureAiInference: azureAiInferenceChatModel.createAzureAiInferenceChatModel
|
|
9
|
+
};
|
|
10
|
+
const createChatModeForSdk = (sdk, options) => {
|
|
11
|
+
if (!(sdk in chatModels)) {
|
|
12
|
+
throw new Error(`Unsupported SDK type: ${sdk}`);
|
|
13
|
+
}
|
|
14
|
+
return chatModels[sdk](options);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
exports.createChatModeForSdk = createChatModeForSdk;
|
|
18
|
+
//# sourceMappingURL=index.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../../../src/services/chat-model/index.ts"],"sourcesContent":["import { createAzureAiInferenceChatModel } from './azure-ai-inference-chat-model';\nimport {\n CreateChatModelFunction,\n SdkType,\n CreateChatModelFunctionOptions,\n} from '../../types/chat-model';\nimport { createOpenAiChatModel } from './open-ai';\n\nconst chatModels: Record<SdkType, CreateChatModelFunction> = {\n openai: createOpenAiChatModel,\n azureAiInference: createAzureAiInferenceChatModel,\n};\n\nexport const createChatModeForSdk = (\n sdk: string,\n options: CreateChatModelFunctionOptions,\n) => {\n if (!(sdk in chatModels)) {\n throw new Error(`Unsupported SDK type: ${sdk}`);\n }\n\n return chatModels[sdk as SdkType](options);\n};\n"],"names":["createOpenAiChatModel","createAzureAiInferenceChatModel"],"mappings":";;;;;AAQA,MAAM,UAAA,GAAuD;AAAA,EAC3D,MAAA,EAAQA,4BAAA;AAAA,EACR,gBAAA,EAAkBC;AACpB,CAAA;AAEO,MAAM,oBAAA,GAAuB,CAClC,GAAA,EACA,OAAA,KACG;AACH,EAAA,IAAI,EAAE,OAAO,UAAA,CAAA,EAAa;AACxB,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,sBAAA,EAAyB,GAAG,CAAA,CAAE,CAAA;AAAA,EAChD;AAEA,EAAA,OAAO,UAAA,CAAW,GAAc,CAAA,CAAE,OAAO,CAAA;AAC3C;;;;"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var openai = require('@langchain/openai');
|
|
4
|
+
|
|
5
|
+
const createOpenAiChatModel = (options) => {
|
|
6
|
+
return new openai.ChatOpenAI({
|
|
7
|
+
configuration: {
|
|
8
|
+
apiKey: options.apiKey,
|
|
9
|
+
baseURL: options.endpoint
|
|
10
|
+
},
|
|
11
|
+
modelName: options.modelName
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
exports.createOpenAiChatModel = createOpenAiChatModel;
|
|
16
|
+
//# sourceMappingURL=open-ai.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"open-ai.cjs.js","sources":["../../../src/services/chat-model/open-ai.ts"],"sourcesContent":["import { CreateChatModelFunction } from '../../types/chat-model';\nimport { ChatOpenAI } from '@langchain/openai';\n\nexport const createOpenAiChatModel: CreateChatModelFunction = options => {\n return new ChatOpenAI({\n configuration: {\n apiKey: options.apiKey,\n baseURL: options.endpoint,\n },\n modelName: options.modelName,\n });\n};\n"],"names":["ChatOpenAI"],"mappings":";;;;AAGO,MAAM,wBAAiD,CAAA,OAAA,KAAW;AACvE,EAAA,OAAO,IAAIA,iBAAA,CAAW;AAAA,IACpB,aAAA,EAAe;AAAA,MACb,QAAQ,OAAA,CAAQ,MAAA;AAAA,MAChB,SAAS,OAAA,CAAQ;AAAA,KACnB;AAAA,IACA,WAAW,OAAA,CAAQ;AAAA,GACpB,CAAA;AACH;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sweetoburrito/backstage-plugin-ai-assistant-backend-module-model-provider-azure-ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"description": "The model-provider-azure-ai backend module for the ai-assistant plugin.",
|
|
6
6
|
"main": "dist/index.cjs.js",
|
|
@@ -33,7 +33,8 @@
|
|
|
33
33
|
"@azure/core-sse": "^2.3.0",
|
|
34
34
|
"@backstage/backend-plugin-api": "backstage:^",
|
|
35
35
|
"@langchain/core": "^0.3.72",
|
|
36
|
-
"@
|
|
36
|
+
"@langchain/openai": "^0.6.13",
|
|
37
|
+
"@sweetoburrito/backstage-plugin-ai-assistant-node": "^0.3.2"
|
|
37
38
|
},
|
|
38
39
|
"devDependencies": {
|
|
39
40
|
"@backstage/backend-test-utils": "backstage:^",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"azure-ai-inference-chat-model.cjs.js","sources":["../src/azure-ai-inference-chat-model.ts"],"sourcesContent":["import {\n BaseChatModel,\n BaseChatModelParams,\n} from '@langchain/core/language_models/chat_models';\nimport createClient, { ModelClient } from '@azure-rest/ai-inference';\nimport { AzureKeyCredential } from '@azure/core-auth';\nimport {\n AIMessage,\n AIMessageChunk,\n BaseMessage,\n} from '@langchain/core/messages';\nimport { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport { concat } from '@langchain/core/utils/stream';\nimport { ChatGenerationChunk, ChatResult } from '@langchain/core/outputs';\nimport { convertToAzureAiInferenceMessages } from './utils';\nimport { createSseStream } from '@azure/core-sse';\n\nexport interface ChatAzureAiInferenceInputs extends BaseChatModelParams {\n modelName: string;\n endpoint: string;\n apiKey: string;\n}\n\nexport class AzureAiInferenceChatModel\n extends BaseChatModel\n implements ChatAzureAiInferenceInputs\n{\n modelName: string;\n endpoint: string;\n apiKey: string;\n private client: ModelClient;\n\n constructor({\n modelName,\n endpoint,\n apiKey,\n ...rest\n }: ChatAzureAiInferenceInputs) {\n super(rest);\n this.modelName = modelName;\n this.endpoint = endpoint;\n this.apiKey = apiKey;\n this.client = createClient(endpoint, new AzureKeyCredential(apiKey));\n }\n\n _llmType(): string {\n return 'azure-ai-inference';\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n _options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun,\n ): AsyncGenerator<ChatGenerationChunk> {\n const aiInferenceMessages = convertToAzureAiInferenceMessages(messages);\n\n const response = await this.client\n .path('/chat/completions')\n .post({\n body: {\n stream: true,\n messages: aiInferenceMessages,\n model: this.modelName,\n },\n })\n .asNodeStream();\n\n const stream = response.body;\n\n if (!stream) {\n throw new Error('Azure AI Inference response stream is undefined');\n }\n\n if (response.status !== '200') {\n stream.destroy();\n throw new Error(\n `Failed to get chat completions. Operation failed with ${response.status} code.`,\n );\n }\n\n const sseStream = createSseStream(stream);\n\n for await (const event of sseStream) {\n if (event.data === '[DONE]') {\n return;\n }\n\n for (const choice of JSON.parse(event.data).choices) {\n const token = choice.delta?.content ?? '';\n\n const responseMessage = new AIMessageChunk({\n content: token,\n });\n\n yield new ChatGenerationChunk({\n text: token,\n message: responseMessage,\n });\n await runManager?.handleLLMNewToken(token);\n }\n }\n }\n\n async _generate(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun,\n ): Promise<ChatResult> {\n let finalChunk: AIMessageChunk | undefined;\n for await (const chunk of this._streamResponseChunks(\n messages,\n options,\n runManager,\n )) {\n if (!finalChunk) {\n finalChunk = chunk.message;\n } else {\n finalChunk = concat(finalChunk, chunk.message);\n }\n }\n\n // Convert from AIMessageChunk to AIMessage since `generate` expects AIMessage.\n const nonChunkMessage = new AIMessage({\n id: finalChunk?.id,\n content: finalChunk?.content ?? '',\n tool_calls: finalChunk?.tool_calls,\n response_metadata: finalChunk?.response_metadata,\n usage_metadata: finalChunk?.usage_metadata,\n });\n return {\n generations: [\n {\n text:\n typeof nonChunkMessage.content === 'string'\n ? nonChunkMessage.content\n : '',\n message: nonChunkMessage,\n },\n ],\n };\n }\n}\n"],"names":["BaseChatModel","createClient","AzureKeyCredential","messages","convertToAzureAiInferenceMessages","createSseStream","AIMessageChunk","ChatGenerationChunk","concat","AIMessage"],"mappings":";;;;;;;;;;;;;;;AAuBO,MAAM,kCACHA,yBAAA,CAEV;AAAA,EACE,SAAA;AAAA,EACA,QAAA;AAAA,EACA,MAAA;AAAA,EACQ,MAAA;AAAA,EAER,WAAA,CAAY;AAAA,IACV,SAAA;AAAA,IACA,QAAA;AAAA,IACA,MAAA;AAAA,IACA,GAAG;AAAA,GACL,EAA+B;AAC7B,IAAA,KAAA,CAAM,IAAI,CAAA;AACV,IAAA,IAAA,CAAK,SAAA,GAAY,SAAA;AACjB,IAAA,IAAA,CAAK,QAAA,GAAW,QAAA;AAChB,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AACd,IAAA,IAAA,CAAK,SAASC,6BAAA,CAAa,QAAA,EAAU,IAAIC,2BAAA,CAAmB,MAAM,CAAC,CAAA;AAAA,EACrE;AAAA,EAEA,QAAA,GAAmB;AACjB,IAAA,OAAO,oBAAA;AAAA,EACT;AAAA,EAEA,OAAO,qBAAA,CACLC,UAAA,EACA,QAAA,EACA,UAAA,EACqC;AACrC,IAAA,MAAM,mBAAA,GAAsBC,wCAAkCD,UAAQ,CAAA;AAEtE,IAAA,MAAM,WAAW,MAAM,IAAA,CAAK,OACzB,IAAA,CAAK,mBAAmB,EACxB,IAAA,CAAK;AAAA,MACJ,IAAA,EAAM;AAAA,QACJ,MAAA,EAAQ,IAAA;AAAA,QACR,QAAA,EAAU,mBAAA;AAAA,QACV,OAAO,IAAA,CAAK;AAAA;AACd,KACD,EACA,YAAA,EAAa;AAEhB,IAAA,MAAM,SAAS,QAAA,CAAS,IAAA;AAExB,IAAA,IAAI,CAAC,MAAA,EAAQ;AACX,MAAA,MAAM,IAAI,MAAM,iDAAiD,CAAA;AAAA,IACnE;AAEA,I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