UNPKG

aiwrapper

Version:

A Universal AI Wrapper for JavaScript & TypeScript

115 lines (114 loc) 3.96 kB
var __defProp = Object.defineProperty; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; import { OpenAIChatCompletionsLang } from "../openai/openai-chat-completions-lang.js"; class GroqLang extends OpenAIChatCompletionsLang { constructor(options) { const modelName = options.model || "llama3-70b-8192"; super({ apiKey: options.apiKey, model: modelName, systemPrompt: options.systemPrompt || "", baseURL: "https://api.groq.com/openai/v1", bodyProperties: options.bodyProperties || {}, maxTokens: options.maxTokens }); } async chat(messages, options) { throw new Error("Not implemented"); } extractThinking(content) { const thinkRegex = /<think>([\s\S]*?)<\/think>/g; const matches = content.match(thinkRegex); if (!matches || matches.length === 0) { return { thinking: "", answer: content }; } const thinking = matches.map((match) => match.replace(/<think>|<\/think>/g, "").trim()).join("\n"); const answer = content.replace(thinkRegex, "").trim(); return { thinking, answer }; } processChunkForThinking(fullContent, result) { } async callAPI(endpoint, body, onData) { const response = await fetch(`${this._config.baseURL}${endpoint}`, { method: "POST", headers: __spreadValues(__spreadValues({ "Content-Type": "application/json" }, this._config.apiKey ? { "Authorization": `Bearer ${this._config.apiKey}` } : {}), this._config.headers), body: JSON.stringify(body) }).catch((err) => { throw new Error(err); }); await this.processResponse(response, onData); return response; } async processResponse(response, onData) { var _a; const reader = (_a = response.body) == null ? void 0 : _a.getReader(); if (!reader) return; const decoder = new TextDecoder(); let buffer = ""; try { while (true) { const { done, value } = await reader.read(); if (done) break; buffer += decoder.decode(value, { stream: true }); let lineEnd = buffer.indexOf("\n"); while (lineEnd !== -1) { const line = buffer.substring(0, lineEnd).trim(); buffer = buffer.substring(lineEnd + 1); if (line.startsWith("data: ")) { const dataValue = line.substring(6); if (dataValue === "[DONE]") { onData({ finished: true }); } else { try { const data = JSON.parse(dataValue); onData(data); } catch (e) { console.error("Error parsing JSON:", e); } } } lineEnd = buffer.indexOf("\n"); } } if (buffer.trim() && buffer.startsWith("data: ")) { const dataValue = buffer.substring(6).trim(); if (dataValue === "[DONE]") { onData({ finished: true }); } else if (dataValue) { try { const data = JSON.parse(dataValue); onData(data); } catch (e) { console.error("Error parsing JSON:", e); } } } onData({ finished: true }); } catch (e) { console.error("Error processing response stream:", e); throw e; } finally { reader.releaseLock(); } } } export { GroqLang }; //# sourceMappingURL=groq-lang.js.map