askexperts
Version:
AskExperts SDK: build and use AI experts - ask them questions and pay with bitcoin on an open protocol
117 lines (116 loc) • 4.16 kB
TypeScript
import { APIPromise } from "openai";
import { ChatCompletionCreateParams, ChatCompletion, ChatCompletionChunk } from "openai/resources/chat/completions";
import { OpenaiInterface } from "./index.js";
import { PricingResult } from "../experts/utils/ModelPricing.js";
import { LightningPaymentManager } from "../payments/LightningPaymentManager.js";
import { SimplePool } from "nostr-tools";
/**
* OpenAI interface implementation that uses AskExperts
* Provides a bridge between OpenAI API and AskExperts protocol
*/
export declare class OpenaiAskExperts implements OpenaiInterface {
/**
* Fixed fee in satoshis added to each transaction
*/
static readonly FEES = 6;
/**
* Static mapping of aliases to expert pubkeys
* When an alias is provided as the model, the corresponding pubkey will be used
*/
static readonly EXPERT_ALIASES: Record<string, string>;
/**
* The AskExpertsClient instance
*/
private client;
/**
* The LightningPaymentManager instance
*/
private paymentManager;
/**
* Profit margin percentage to add to each transaction
*/
private margin;
/**
* Map of active quotes with their resolution callbacks and stream flags
*/
private activeQuotes;
/**
* Creates a new OpenaiAskExperts instance
*
* @param paymentManager - The LightningPaymentManager instance
* @param options - Optional configuration
* @param options.compression - Custom compression implementation
* @param options.pool - SimplePool instance for relay operations
* @param options.discoveryRelays - Array of discovery relay URLs to use as fallback
*/
constructor(paymentManager: LightningPaymentManager, options?: {
pool?: SimplePool;
discoveryRelays?: string[];
margin?: number;
});
/**
* Gets pricing information for a model in sats per million tokens
* Always returns undefined as pricing is handled by AskExperts
*
* @param model - Model ID
* @returns Promise resolving to undefined
*/
pricing(model: string): Promise<PricingResult | undefined>;
/**
* Estimates the price of processing a prompt
* Uses AskExpertsClient to get a quote from an expert
*
* @param model - Model ID (used as expert pubkey or alias)
* @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
*
* @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>>;
/**
* Creates a chat completion
* Approves the payment for the quote and returns the result
*
* @param body - Chat completion parameters
* @param options - Optional parameters including quoteId
* @returns Promise resolving to chat completion or chunks
*/
private createChatCompletion;
/**
* Creates a streaming response from replies
*
* @param replies - The replies from the expert
* @returns AsyncIterable of ChatCompletionChunk objects
*/
private createStreamingResponse;
/**
* Creates a non-streaming response from replies
*
* @param replies - The replies from the expert
* @param model - The model name
* @returns ChatCompletion object
*/
private createNonStreamingResponse;
/**
* Callback for handling payments
* Called when a quote is accepted to process the payment
*
* @param quote - The quote to pay
* @param prompt - The prompt being processed
* @returns Promise resolving to payment proof
*/
private onPay;
/**
* Disposes of resources when the instance is no longer needed
*/
[Symbol.dispose](): void;
}