notdiamond
Version:
TS/JS client for the NotDiamond API
219 lines (215 loc) • 10.1 kB
text/typescript
import { z } from 'zod';
declare const SupportedProvider: {
readonly OPENAI: "openai";
readonly ANTHROPIC: "anthropic";
readonly GOOGLE: "google";
readonly MISTRAL: "mistral";
readonly PERPLEXITY: "perplexity";
readonly COHERE: "cohere";
readonly TOGETHERAI: "togetherai";
};
declare const SupportedModel: {
readonly GPT_3_5_TURBO: "gpt-3.5-turbo";
readonly GPT_3_5_TURBO_0125: "gpt-3.5-turbo-0125";
readonly GPT_4: "gpt-4";
readonly GPT_4_0613: "gpt-4-0613";
readonly GPT_4_1106_PREVIEW: "gpt-4-1106-preview";
readonly GPT_4_TURBO: "gpt-4-turbo";
readonly GPT_4_TURBO_PREVIEW: "gpt-4-turbo-preview";
readonly GPT_4_TURBO_2024_04_09: "gpt-4-turbo-2024-04-09";
readonly GPT_4O_2024_05_13: "gpt-4o-2024-05-13";
readonly GPT_4O_2024_08_06: "gpt-4o-2024-08-06";
readonly GPT_4O: "gpt-4o";
readonly GPT_4O_MINI_2024_07_18: "gpt-4o-mini-2024-07-18";
readonly GPT_4O_MINI: "gpt-4o-mini";
readonly GPT_4_0125_PREVIEW: "gpt-4-0125-preview";
readonly GPT_4_5_PREVIEW: "gpt-4.5-preview";
readonly GPT_4_5_PREVIEW_2025_02_27: "gpt-4.5-preview-2025-02-27";
readonly CHATGPT_4O_LATEST: "chatgpt-4o-latest";
readonly O1_PREVIEW: "o1-preview";
readonly O1_PREVIEW_2024_09_12: "o1-preview-2024-09-12";
readonly O1_MINI: "o1-mini";
readonly O1_MINI_2024_09_12: "o1-mini-2024-09-12";
readonly CLAUDE_2_1: "claude-2.1";
readonly CLAUDE_3_OPUS_20240229: "claude-3-opus-20240229";
readonly CLAUDE_3_SONNET_20240229: "claude-3-sonnet-20240229";
readonly CLAUDE_3_5_SONNET_20240620: "claude-3-5-sonnet-20240620";
readonly CLAUDE_3_5_SONNET_20241022: "claude-3-5-sonnet-20241022";
readonly CLAUDE_3_5_SONNET_LATEST: "claude-3-5-sonnet-latest";
readonly CLAUDE_3_HAIKU_20240307: "claude-3-haiku-20240307";
readonly CLAUDE_3_5_HAIKU_20241022: "claude-3-5-haiku-20241022";
readonly CLAUDE_3_7_SONNET_LATEST: "claude-3-7-sonnet-latest";
readonly CLAUDE_3_7_SONNET_20250219: "claude-3-7-sonnet-20250219";
readonly GEMINI_PRO: "gemini-pro";
readonly GEMINI_1_PRO_LATEST: "gemini-1.0-pro-latest";
readonly GEMINI_15_PRO_LATEST: "gemini-1.5-pro-latest";
readonly GEMINI_15_PRO_EXP_0801: "gemini-1.5-pro-exp-0801";
readonly GEMINI_15_FLASH_LATEST: "gemini-1.5-flash-latest";
readonly GEMINI_2_0_FLASH: "gemini-2.0-flash";
readonly GEMINI_2_0_FLASH_001: "gemini-2.0-flash-001";
readonly COMMAND_R: "command-r";
readonly COMMAND_R_PLUS: "command-r-plus";
readonly MISTRAL_LARGE_LATEST: "mistral-large-latest";
readonly MISTRAL_LARGE_2407: "mistral-large-2407";
readonly MISTRAL_LARGE_2402: "mistral-large-2402";
readonly MISTRAL_MEDIUM_LATEST: "mistral-medium-latest";
readonly MISTRAL_SMALL_LATEST: "mistral-small-latest";
readonly CODESTRAL_LATEST: "codestral-latest";
readonly OPEN_MISTRAL_7B: "open-mistral-7b";
readonly OPEN_MIXTRAL_8X7B: "open-mixtral-8x7b";
readonly OPEN_MIXTRAL_8X22B: "open-mixtral-8x22b";
readonly MISTRAL_7B_INSTRUCT_V0_2: "Mistral-7B-Instruct-v0.2";
readonly MIXTRAL_8X7B_INSTRUCT_V0_1: "Mixtral-8x7B-Instruct-v0.1";
readonly MIXTRAL_8X22B_INSTRUCT_V0_1: "Mixtral-8x22B-Instruct-v0.1";
readonly LLAMA_3_70B_CHAT_HF: "Llama-3-70b-chat-hf";
readonly LLAMA_3_8B_CHAT_HF: "Llama-3-8b-chat-hf";
readonly QWEN2_72B_INSTRUCT: "Qwen2-72B-Instruct";
readonly LLAMA_3_1_8B_INSTRUCT_TURBO: "Meta-Llama-3.1-8B-Instruct-Turbo";
readonly LLAMA_3_1_70B_INSTRUCT_TURBO: "Meta-Llama-3.1-70B-Instruct-Turbo";
readonly LLAMA_3_1_405B_INSTRUCT_TURBO: "Meta-Llama-3.1-405B-Instruct-Turbo";
readonly PERPLEXITY_SONAR: "sonar";
readonly OPEN_MISTRAL_NEMO: "open-mistral-nemo";
readonly DEEPSEEK_R1: "DeepSeek-R1";
};
declare const ProviderModelMap: {
readonly openai: readonly ["gpt-3.5-turbo", "gpt-3.5-turbo-0125", "gpt-4", "gpt-4-0613", "gpt-4-1106-preview", "gpt-4-turbo", "gpt-4-turbo-preview", "gpt-4-turbo-2024-04-09", "gpt-4o-2024-05-13", "gpt-4o-2024-08-06", "gpt-4o", "gpt-4o-mini-2024-07-18", "gpt-4o-mini", "gpt-4-0125-preview", "o1-preview", "o1-preview-2024-09-12", "o1-mini", "o1-mini-2024-09-12", "chatgpt-4o-latest", "gpt-4.5-preview", "gpt-4.5-preview-2025-02-27"];
readonly anthropic: readonly ["claude-2.1", "claude-3-opus-20240229", "claude-3-sonnet-20240229", "claude-3-5-sonnet-20240620", "claude-3-5-sonnet-20241022", "claude-3-5-sonnet-latest", "claude-3-haiku-20240307", "claude-3-5-haiku-20241022", "claude-3-7-sonnet-latest", "claude-3-7-sonnet-20250219"];
readonly google: readonly ["gemini-pro", "gemini-1.0-pro-latest", "gemini-1.5-pro-latest", "gemini-1.5-pro-exp-0801", "gemini-1.5-flash-latest", "gemini-2.0-flash", "gemini-2.0-flash-001"];
readonly mistral: readonly ["mistral-large-latest", "mistral-large-2407", "mistral-large-2402", "mistral-medium-latest", "mistral-small-latest", "codestral-latest", "open-mistral-7b", "open-mixtral-8x7b", "open-mixtral-8x22b", "open-mistral-nemo"];
readonly perplexity: readonly ["sonar"];
readonly cohere: readonly ["command-r", "command-r-plus"];
readonly togetherai: readonly ["Mistral-7B-Instruct-v0.2", "Mixtral-8x7B-Instruct-v0.1", "Mixtral-8x22B-Instruct-v0.1", "Llama-3-70b-chat-hf", "Llama-3-8b-chat-hf", "Qwen2-72B-Instruct", "Meta-Llama-3.1-8B-Instruct-Turbo", "Meta-Llama-3.1-70B-Instruct-Turbo", "Meta-Llama-3.1-405B-Instruct-Turbo", "DeepSeek-R1"];
};
type ProviderModelMapType = typeof ProviderModelMap;
type SupportedProviderType = keyof ProviderModelMapType;
type ModelForProvider<T extends SupportedProviderType> = ProviderModelMapType[T][number];
interface Provider<T extends SupportedProviderType = SupportedProviderType> {
provider: T;
model: ModelForProvider<T>;
contextLength?: number;
inputPrice?: number;
customInputPrice?: number;
outputPrice?: number;
customOutputPrice?: number;
latency?: number;
customLatency?: number;
isCustom?: boolean;
systemPrompt?: string;
}
interface NotDiamondOptions {
apiKey?: string;
apiUrl?: string;
llmKeys?: Partial<Record<(typeof SupportedProvider)[keyof typeof SupportedProvider], string>>;
}
interface NotDiamondErrorResponse {
detail: string;
}
interface ToolFunction {
description?: string;
name: string;
parameters?: Record<string, unknown>;
}
interface Tool {
type: string;
function: ToolFunction;
}
interface Message {
content: string;
role: 'user' | 'assistant' | 'system';
}
interface ModelSelectOptions {
messages: Message[];
llmProviders: Provider[];
tools?: Tool[];
maxModelDepth?: number;
tradeoff?: 'cost' | 'latency';
preferenceId?: string;
hashContent?: boolean;
timeout?: number;
default?: Provider | number | string;
previousSession?: string;
responseModel?: z.ZodType<any>;
}
interface ModelSelectSuccessResponse {
providers: Provider[];
session_id: string;
}
interface Feedback {
accuracy: number;
}
interface FeedbackOptions {
sessionId: string;
feedback: Feedback;
provider: Provider;
}
interface FeedbackSuccessResponse {
session_id: string;
feedback: Feedback;
}
declare class NotDiamond {
private apiKey;
private apiUrl;
private modelSelectUrl;
private feedbackUrl;
private createUrl;
private llmKeys;
constructor(options?: NotDiamondOptions);
private getAuthHeader;
private postRequest;
/**
* Selects the best model for the given messages.
* @param options The options for the model.
* @returns The results of the model.
*/
modelSelect(options: ModelSelectOptions): Promise<ModelSelectSuccessResponse | NotDiamondErrorResponse>;
/**
* Sends feedback to the NotDiamond API.
* @param options The options for the feedback.
* @returns The results of the feedback.
*/
feedback(options: FeedbackOptions): Promise<FeedbackSuccessResponse | NotDiamondErrorResponse>;
/**
* Creates a preference id.
* @returns The preference id.
*/
createPreferenceId(): Promise<string | NotDiamondErrorResponse>;
/**
*
* @param options The options for the model.
* @returns A promise that resolves to the results of the model.
*/
private acreate;
/**
*
* @param options The options for the model.
* @param callback Optional callback function to handle the result.
* @returns A promise that resolves to the results of the model or a callback function
*/
create(options: ModelSelectOptions, runtimeArgs?: Record<string, string>, callback?: (error: Error | null, result?: {
content: string;
providers: Provider[];
}) => void): Promise<{
content: string;
providers: Provider<"openai" | "anthropic" | "perplexity" | "google" | "mistral" | "cohere" | "togetherai">[];
}> | undefined;
/**
* Streams the results of the model asynchronously.
* @param options The options for the model.
* @returns A promise that resolves to an object containing the provider and an AsyncIterable of strings.
*/
private astream;
/**
* Streams the results of the model.
* @param options The options for the model.
* @param callback Optional callback function to handle each chunk of the stream.
* @returns A promise that resolves to an object containing the provider and an AsyncIterable of strings or a callback function
*/
stream(options: ModelSelectOptions, runtimeArgs?: Record<string, string>, callback?: (error: Error | null, result?: {
provider: Provider;
chunk?: string;
}) => void): Promise<{
provider: Provider<"openai" | "anthropic" | "perplexity" | "google" | "mistral" | "cohere" | "togetherai">;
stream: AsyncIterable<string>;
}> | undefined;
}
export { type Feedback, type FeedbackOptions, type FeedbackSuccessResponse, type Message, type ModelSelectOptions, type ModelSelectSuccessResponse, NotDiamond, type NotDiamondErrorResponse, type NotDiamondOptions, SupportedModel, SupportedProvider, type Tool, type ToolFunction };