UNPKG

@axflow/models

Version:

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

108 lines (107 loc) 3.57 kB
// src/anthropic/completion.ts import { POST, HttpError } from "@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 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 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 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) }); } }; export { AnthropicCompletion };