UNPKG

@convo-lang/convo-lang

Version:
63 lines 3.02 kB
import { secretManager } from "@iyio/common"; import { BaseOpenAiConvoCompletionService } from "../BaseOpenAiConvoCompletionService.js"; import { BaseOpenAiConvoConverter } from "../BaseOpenAiConvoConverter.js"; import { convoCompletionService, convoConversationConverterProvider } from "../convo.deps.js"; import { convoAnthropicInputType, convoAnthropicOutputType } from "./anthropic-lib.js"; import { defaultAnthropicChatModel, knownConvoAnthropicModels } from "./anthropic-models.js"; import { anthropicApiKeyParam, anthropicBaseUrlParam, anthropicChatModelParam, anthropicSecretsParam, anthropicVisionModelParam } from "./anthropic-params.js"; export const convoAnthropicModule = (scope) => { scope.implementService(convoCompletionService, createAnthropicConvoServiceFromScope); scope.addProvider(convoConversationConverterProvider, createAnthropicConvoConverterFromScope); }; export const createAnthropicConvoServiceFromScope = (scope) => { return new BaseOpenAiConvoCompletionService({ serviceId: 'anthropic', apiKey: scope.to(anthropicApiKeyParam).get(), apiBaseUrl: scope.to(anthropicBaseUrlParam).get(), completionsEndpoint: '/v1/messages', secretManager: scope.to(secretManager).get(), secretsName: scope.to(anthropicSecretsParam).get(), inputType: convoAnthropicInputType, outputType: convoAnthropicOutputType, models: knownConvoAnthropicModels, apiKeyHeader: 'x-api-key', apiKeyHeaderValuePrefix: null, headers: { 'content-type': 'application/json', 'anthropic-version': '2023-06-01', }, updateRequest: (body) => { body['max_tokens'] = 8000; } }); }; export const createAnthropicConvoConverterFromScope = (scope) => { return new BaseOpenAiConvoConverter({ chatModel: scope.to(anthropicChatModelParam).get() ?? defaultAnthropicChatModel.name, visionModel: scope.to(anthropicVisionModelParam).get(), supportedInputTypes: [convoAnthropicInputType], supportedOutputTypes: [convoAnthropicOutputType], models: knownConvoAnthropicModels, transformOutput: (output) => { const co = output; const out = { ...output }; out.choices = [{ index: 0, message: { role: co.role, content: co.content.filter?.((c) => c.type === 'text').map?.((c) => c.text)?.join('\n') ?? null, refusal: null, }, logprobs: null, finish_reason: co.stop_reason ?? 'stop', }]; out.usage = { prompt_tokens: co.usage.input_tokens, completion_tokens: co.usage.output_tokens, total_tokens: co.usage.input_tokens + co.usage.output_tokens }; return out; } }); }; //# sourceMappingURL=anthropic-service.js.map