UNPKG

@axflow/models

Version:

Zero-dependency, modular SDK for building robust natural language applications

133 lines (131 loc) 4.67 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/anthropic/completion.ts var completion_exports = {}; __export(completion_exports, { AnthropicCompletion: () => AnthropicCompletion }); module.exports = __toCommonJS(completion_exports); var import_shared = require("@axflow/models/shared"); var ANTHROPIC_API_URL = "https://api.anthropic.com/v1/complete"; function headers(apiKey, version, customHeaders) { const headers2 = { accept: "application/json", "content-type": "application/json", ...customHeaders, "anthropic-version": version || "2023-06-01" }; if (typeof apiKey === "string") { headers2["x-api-key"] = apiKey; } return headers2; } async function run(request, options) { const url = options.apiUrl || ANTHROPIC_API_URL; const response = await (0, import_shared.POST)(url, { headers: headers(options.apiKey, options.version, options.headers), body: JSON.stringify({ ...request, stream: false }), fetch: options.fetch, signal: options.signal }); return response.json(); } async function streamBytes(request, options) { const url = options.apiUrl || ANTHROPIC_API_URL; const response = await (0, import_shared.POST)(url, { headers: headers(options.apiKey, options.version, options.headers), body: JSON.stringify({ ...request, stream: true }), fetch: options.fetch, signal: options.signal }); if (!response.body) { throw new import_shared.HttpError("Expected response body to be a ReadableStream", response); } return response.body; } function noop(chunk) { return chunk; } async function stream(request, options) { const byteStream = await streamBytes(request, options); return byteStream.pipeThrough(new AnthropicCompletionDecoderStream(noop)); } function chunkToToken(chunk) { return chunk.event === "completion" ? chunk.data.completion : ""; } async function streamTokens(request, options) { const byteStream = await streamBytes(request, options); return byteStream.pipeThrough(new AnthropicCompletionDecoderStream(chunkToToken)); } var AnthropicCompletion = class { static run = run; static stream = stream; static streamBytes = streamBytes; static streamTokens = streamTokens; }; var AnthropicCompletionDecoderStream = class _AnthropicCompletionDecoderStream extends TransformStream { static EVENT_LINES_RE = /^event:\s*(\w+)\r\ndata:\s*(.+)$/; static parse(lines) { lines = lines.trim(); if (lines.length === 0) { return null; } const match = lines.match(_AnthropicCompletionDecoderStream.EVENT_LINES_RE); try { const event = match[1]; const data = match[2]; return { event, data: JSON.parse(data) }; } catch (error) { throw new Error(`Expected well-formed streaming events but got ${JSON.stringify(lines)}`); } } static transformer(map) { let buffer = []; const decoder = new TextDecoder(); return (bytes, controller) => { const chunk = decoder.decode(bytes); for (let i = 0, len = chunk.length; i < len; ++i) { const bufferLen = buffer.length; const isEventSeparator = chunk[i] === "\n" && buffer[bufferLen - 3] === "\r" && buffer[bufferLen - 2] === "\n" && buffer[bufferLen - 1] === "\r"; if (!isEventSeparator) { buffer.push(chunk[i]); continue; } const event = _AnthropicCompletionDecoderStream.parse(buffer.join("")); if (event && event.event === "error") { const error = event.data.error; controller.error(`${error.type}: ${error.message}`); } else if (event) { controller.enqueue(map(event)); } buffer = []; } }; } constructor(map) { super({ transform: _AnthropicCompletionDecoderStream.transformer(map) }); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AnthropicCompletion });