UNPKG

askexperts

Version:

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

86 lines (85 loc) 2.85 kB
import OpenAI, { APIPromise } from "openai"; import { ChatCompletionCreateParams, ChatCompletion, ChatCompletionChunk } from "openai/resources/chat/completions"; import { OpenaiInterface } from "./index.js"; import { OpenRouter } from "../experts/utils/OpenRouter.js"; import { PricingResult } from "../experts/utils/ModelPricing.js"; /** * OpenAI interface implementation that uses OpenRouter * Provides pricing and estimation functions */ export declare class OpenaiOpenRouter implements OpenaiInterface { /** * The underlying OpenAI client */ private openai; /** * The OpenRouter instance for pricing */ private openRouter; /** * Profit margin (e.g., 0.1 for 10%) */ private margin; /** * Average output token count for pricing estimates */ private avgOutputCount; /** * Number of outputs processed (for averaging) */ private outputCount; /** * Map of active quotes with their model and content */ private activeQuotes; /** * Creates a new OpenaiOpenRouter instance * * @param openai - The underlying OpenAI client * @param openRouter - The OpenRouter instance for pricing * @param margin - Profit margin (default: 0) */ constructor(openai: OpenAI, openRouter: OpenRouter, margin?: number); /** * Gets pricing information for a model in sats per million tokens * Delegates to the OpenRouter instance * * @param model - Model ID * @returns Promise resolving to pricing information or undefined if not available */ pricing(model: string): Promise<PricingResult | undefined>; /** * Count tokens using gpt-tokenizer * * @param text - Text to count tokens for * @returns Token count */ private countTokens; /** * Estimates the price of processing a prompt * Implements the logic from OpenaiProxyExpertBase.onPromptPrice * * @param model - Model ID * @param content - The chat completion parameters * @returns Promise resolving to the estimated price object */ getQuote(model: string, content: ChatCompletionCreateParams): Promise<{ amountSats: number; quoteId: string; }>; /** * Execute a chat completion request * Implementation of the interface method * * @param quoteId - Quote ID for the request * @param options - Additional options for the request * @returns Promise resolving to chat completion or chunks */ execute(quoteId: string, options?: any): APIPromise<ChatCompletion> | APIPromise<AsyncIterable<ChatCompletionChunk>>; /** * Updates the average output token count based on new output * * @param outputContent - The content to count tokens for */ private updateAverageOutputCount; }