UNPKG

askexperts

Version:

AskExperts SDK: build and use AI experts - ask them questions and pay with bitcoin on an open protocol

52 lines 2.27 kB
import { OpenAI } from "openai"; import { getOpenRouter } from "../experts/utils/OpenRouter.js"; import { OpenaiOpenRouter } from "./OpenaiOpenRouter.js"; import { OpenaiAskExperts } from "./OpenaiAskExperts.js"; export * from "./OpenaiOpenRouter.js"; export * from "./OpenaiAskExperts.js"; /** * Creates an OpenAI instance that implements OpenaiInterface * * @param apiKey - OpenAI API key * @param baseURL - OpenAI base URL * @param defaultHeaders - Optional default headers * @returns OpenAI instance implementing OpenaiInterface */ export function createOpenAI(options) { // Parse the baseURL to check the hostname const url = new URL(options?.baseURL || "https://askexperts.io"); // If the hostname is openrouter.ai, return an OpenaiOpenRouter instance if (url.hostname === "openrouter.ai") { // Check the options if (!options?.apiKey) throw new Error("Option apiKey is required"); // Create the base OpenAI client const openai = new OpenAI({ apiKey: options.apiKey, baseURL: options.baseURL, defaultHeaders: options.defaultHeaders || { "HTTP-Referer": "https://askexperts.io", // Site URL for rankings on openrouter.ai "X-Title": "AskExperts", // Site title for rankings on openrouter.ai }, }); // Return an OpenaiOpenRouter instance return new OpenaiOpenRouter(openai, options.openRouter || getOpenRouter(), options.margin); } // If the hostname is askexperts.ai, return an OpenaiAskExperts instance else if (url.hostname === "askexperts.io") { // This is required to work with expert-llm proxies if (!options?.paymentManager) throw new Error("Option paymentManager is required"); // Return an OpenaiAskExperts instance return new OpenaiAskExperts(options.paymentManager, { pool: options.pool, discoveryRelays: options.discoveryRelays, margin: options.margin, }); } else { // For other hostnames, throw an exception throw new Error(`Unsupported baseURL: '${options?.baseURL}'. Only openrouter.ai and askexperts.ai are supported.`); } } //# sourceMappingURL=index.js.map