export type SystemMessage = {role: 'system', content: string, name?: (string | null) | undefined}; export type TextContent = {type: 'text', text: string}; export type AudioData = {url: string}; export type AudioContent = {type: 'audio_url', audio_url: AudioData}; export type ImageData = {url: string}; export type ImageContent = {type: 'image_url', image_url: ImageData}; export type VideoData = {url: string}; export type VideoContent = {type: 'video_url', video_url: VideoData}; export type UserMessageContentMultiModal = TextContent | AudioContent | ImageContent | VideoContent; export type UserMessage = {role: 'user', content: string | Array, name?: (string | null) | undefined}; export type AssistantMessageToolCallFunction = {name: string, arguments: string}; export type AssistantMessageToolCall = {id: string, type: 'function', function: AssistantMessageToolCallFunction}; export type AssistantMessage = {role: 'assistant', content?: (string | null) | undefined, reasoning_content?: (string | null) | undefined, reasoning?: (string | null) | undefined, name?: (string | null) | undefined, tool_calls?: (Array | null) | undefined}; export type ToolMessage = {role: 'tool', content: string, tool_call_id: string, name?: (string | null) | undefined}; export type Message = SystemMessage | UserMessage | AssistantMessage | ToolMessage; export type StreamOptions = {include_usage?: (boolean | null) | undefined}; export type ChatCompleteBodyToolChoiceFunction = {name: string}; export type ChatCompleteBodyToolChoice = {type: 'function', function: ChatCompleteBodyToolChoiceFunction}; export type Function = {description?: (string | null) | undefined, name: string, parameters: {[key: string]: unknown}}; export type Tool = {type: 'function', function: Function}; export type ResponseFormatJsonSchemaSchema = {schema: {[key: string]: unknown}}; export type ResponseFormatJsonSchema = {type: 'json_schema', json_schema: ResponseFormatJsonSchemaSchema}; export type ResponseFormatJsonObject = {type: 'json_object'}; export type ResponseFormatRegex = {type: 'regex', schema: string}; export type ResponseFormatText = {type: 'text'}; export type ResponseFormat = ResponseFormatJsonSchema | ResponseFormatJsonObject | ResponseFormatRegex | ResponseFormatText; export type ContainerChatCompletionStreamBody = {model?: (string | null) | undefined, messages: Array, chat_template_kwargs?: ({[key: string]: unknown} | null) | undefined, eos_token?: (Array | null) | undefined, frequency_penalty?: (number | null) | undefined, logit_bias?: ({[key: string]: unknown} | null) | undefined, logprobs?: (boolean | null) | undefined, max_tokens?: (number | null) | undefined, min_p?: (number | null) | undefined, n?: (number | null) | undefined, parallel_tool_calls?: (boolean | null) | undefined, presence_penalty?: (number | null) | undefined, repetition_penalty?: (number | null) | undefined, reasoning_effort?: ('low' | 'medium' | 'high' | null) | undefined, reasoning_budget?: (number | null) | undefined, seed?: (Array | number | null) | undefined, stop?: (Array | null) | undefined, stream?: (boolean | null) | undefined, stream_options?: (StreamOptions | null) | undefined, parse_reasoning?: (boolean | null) | undefined, include_reasoning?: (boolean | null) | undefined, temperature?: (number | null) | undefined, tool_choice?: (ChatCompleteBodyToolChoice | string) | undefined, top_k?: (number | null) | undefined, top_logprobs?: (number | null) | undefined, top_p?: (number | null) | undefined, xtc_threshold?: (number | null) | undefined, xtc_probability?: (number | null) | undefined, tools?: (Array | null) | undefined, min_tokens?: (number | null) | undefined, response_format?: (ResponseFormat | null) | undefined}; export type CreateApiV1ChatCompletionsStreamArgs = {body: ContainerChatCompletionStreamBody}; export type CreateApiV1ChatCompletionsStreamOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; /** * Stream chat completions * * POST /v1/chat/completions#stream */ export const createApiV1ChatCompletionsStream = async (args: CreateApiV1ChatCompletionsStreamArgs, options?: CreateApiV1ChatCompletionsStreamOptions) => { const {baseUrl = "https://api.friendli.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "text/event-stream") const url = `${baseUrl.replace(/\/+$/, '')}/v1/chat/completions#stream${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return res.text() };