Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | /**
* A role in a chat conversation.
*/
export type ChatRole = 'system' | 'user' | 'assistant' | 'tool';
/**
* A single tool call made by the model.
*/
export type ToolCall = {
id: string;
type: 'function';
index?: number;
function: { name: string; arguments: string };
};
/**
* A tool definition passed to the model.
*/
export type Tool = {
type: 'function';
function: {
name: string;
description?: string;
parameters?: {
type: 'object';
properties?: Record<string, unknown>;
required?: string[];
};
};
};
/**
* Chat message from the model or system (no tool metadata).
*/
export type SystemMessage = { role: 'system'; content: string };
/**
* End-user turn (no tool metadata).
*/
export type UserMessage = { role: 'user'; content: string };
/**
* Assistant turn; may include tool calls. `content` may be null when the model
* only returns tool calls.
*/
export type AssistantMessage = {
role: 'assistant';
content: string | null;
tool_calls?: ToolCall[];
};
/**
* Result of a tool invocation, correlated by {@link ToolCall.id}.
*/
export type ToolMessage = {
role: 'tool';
content: string;
tool_call_id: string;
};
/**
* A single message in a chat conversation (discriminated by `role`).
*/
export type ChatMessage =
| SystemMessage
| UserMessage
| AssistantMessage
| ToolMessage;
/**
* Token usage statistics for a completion request.
*/
export type Usage = {
prompt_tokens: number;
completion_tokens: number;
total_tokens: number;
};
/**
* Options shared by raw sampling requests.
*/
export type SampleOptions = {
temperature?: number;
top_p?: number;
seed?: number;
max_tokens?: number;
stop?: string | string[];
};
/**
* Parameters for a raw token-prediction request (bypasses chat template).
*/
export type SampleParams = {
model: string;
prompt: string;
} & SampleOptions;
/**
* Result of a raw token-prediction request.
*/
export type SampleResult = {
text: string;
};
/**
* Parameters for a chat completion request.
*/
export type ChatParams = {
model: string;
messages: ChatMessage[];
tools?: Tool[];
max_tokens?: number;
temperature?: number;
top_p?: number;
stop?: string | string[];
seed?: number;
n?: number;
/** When `true`, the response is an SSE stream of {@link ChatStreamChunk}s. */
stream?: boolean;
};
/**
* Partial tool-call fragment in a streaming completion delta (OpenAI-style SSE).
* Fields arrive incrementally across multiple chunks.
*/
export type ChatStreamToolCallDelta = {
index?: number;
id?: string;
type?: 'function';
function?: { name?: string; arguments?: string };
};
/**
* Assistant fragment from a streaming `/v1/chat/completions` response after
* the Open /v1 client normalizes each SSE event. `role` is always `'assistant'`;
* `content` / `tool_calls` are present only when the wire event included them.
*/
export type AssistantStreamDelta = {
role: 'assistant';
content?: string;
tool_calls?: ChatStreamToolCallDelta[];
};
/** @alias {@link AssistantStreamDelta} */
export type ChatStreamDelta = AssistantStreamDelta;
/**
* A single chunk from a streaming chat completion response.
*/
export type ChatStreamChunk = {
id: string;
model: string;
choices: {
delta: AssistantStreamDelta;
index: number;
finish_reason: string | null;
}[];
};
/**
* A single choice in a chat completion response.
*/
export type ChatChoice = {
message: ChatMessage;
index: number;
finish_reason: string | null;
};
/**
* Result of a chat completion request.
*/
export type ChatResult = {
id: string;
model: string;
choices: ChatChoice[];
usage: Usage;
};
/**
* Minimal service interface required by `makeChatClient`.
*/
export type ChatService = {
chat(params: ChatParams & { stream: true }): AsyncIterable<ChatStreamChunk>;
chat(params: ChatParams & { stream?: false }): Promise<ChatResult>;
};
/**
* Minimal service interface required by `makeSampleClient`.
*/
export type SampleService = {
sample: (params: SampleParams) => Promise<SampleResult>;
};
/**
* Configuration information for a language model.
* Contains the model identifier and optional configuration parameters.
*
* @template Options - The type of options supported by the model
*/
export type ModelInfo<Options = unknown> = {
model: string;
options?: Partial<Options>;
};
/**
* Interface for a language model that can be loaded, unloaded, and used for text generation.
* Provides a standardized interface for interacting with different language model implementations.
*
* @template Options - The type of options supported by the model
* @template Response - The type of response generated by the model
*/
export type LanguageModel<Options, Response> = {
/**
* Retrieves information about the model configuration.
*
* @returns A promise that resolves to the model information
*/
getInfo: () => Promise<ModelInfo<Options>>;
/**
* Loads the model into memory and keeps it alive indefinitely.
*
* @returns A promise that resolves when the model is loaded.
*/
load: () => Promise<void>;
/**
* Unloads the model from memory.
*
* @returns A promise that resolves when the model is unloaded.
*/
unload: () => Promise<void>;
/**
* Generates a response from the model based on the provided prompt.
*
* @param prompt - The prompt to complete.
* @param options - The options to pass to the model.
* @returns A promise that resolves to an async iterable of response chunks, or rejects if an error occurs.
*/
sample: (
prompt: string,
options?: Partial<Options>,
) => Promise<{
stream: AsyncIterable<Response>;
abort: () => Promise<void>;
}>;
};
/**
* Configuration for creating a language model instance.
* Specifies which model to use and any model-specific options.
*
* @template Options - The type of options supported by the model
*/
export type InstanceConfig<Options> = {
model: string;
options?: Partial<Options>;
};
/**
* Service interface for creating language model instances.
* Provides a factory pattern for instantiating language models with specific configurations.
*
* @template Config - The type of configuration accepted by the service
* @template Options - The type of options supported by created models
* @template Response - The type of response generated by created models
*/
export type LanguageModelService<Config, Options, Response> = {
/**
* Creates a new language model instance with the specified configuration.
*
* @param config - The configuration for the model instance
* @returns A promise that resolves to a language model instance
*/
makeInstance: (
config: InstanceConfig<Config>,
) => Promise<LanguageModel<Options, Response>>;
};
|