Edits history of script submission #13048 for ' Create a chat completion (telnyx)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Telnyx = {
    	apiKey: string
    }
    /**
     * Create a chat completion
     * Chat with a language model. This endpoint is consistent with the [OpenAI Chat Completions API](https://platform.openai.com/docs/api-reference/chat) and may be used with the OpenAI JS or Python SDK.
     */
    export async function main(
    	auth: Telnyx,
    	body: {
    		messages: {
    			content: string | { type: 'text' | 'image_url'; text?: string; image_url?: string }[]
    			role: 'system' | 'user' | 'assistant' | 'tool'
    		}[]
    		model?: string
    		stream?: false | true
    		temperature?: number
    		max_tokens?: number
    		tools?:
    			| {
    					type: 'function'
    					function: { name: string; description?: string; parameters?: {} }
    			  }
    			| {
    					type: 'retrieval'
    					retrieval: { bucket_ids: string[]; max_num_results?: number }
    			  }[]
    		tool_choice?: 'none' | 'auto' | 'required'
    		response_format?: { type: 'text' | 'json_object' }
    		guided_json?: {}
    		guided_regex?: string
    		guided_choice?: string[]
    		min_p?: number
    		n?: number
    		use_beam_search?: false | true
    		best_of?: number
    		length_penalty?: number
    		early_stopping?: false | true
    		logprobs?: false | true
    		top_logprobs?: number
    		frequency_penalty?: number
    		presence_penalty?: number
    		top_p?: number
    		openai_api_key?: string
    	}
    ) {
    	const url = new URL(`https://api.telnyx.com/v2/ai/chat/completions`)
    
    	const response = await fetch(url, {
    		method: 'POST',
    		headers: {
    			'Content-Type': 'application/json',
    			Authorization: 'Bearer ' + auth.apiKey
    		},
    		body: JSON.stringify(body)
    	})
    	if (!response.ok) {
    		const text = await response.text()
    		throw new Error(`${response.status} ${text}`)
    	}
    	return await response.json()
    }
    

    Submitted by hugo697 428 days ago