Create Chat Completion ( openai)
One script reply has been approved by the moderators Verified

Created by adam186 44 days ago Viewed 126 times 0 Points

Create a chat completion for the provided messages.

No comments yet

Login to be able to comment
Points: 0
deno
One script reply has been approved by the moderators
Ap­pro­ved
import type { Resource } from "https://deno.land/x/windmill@v1.85.0/mod.ts";
import { removeObjectEmptyFields } from 'https://deno.land/x/windmill_helpers@v1.0.0/mod.ts'
import { Configuration, CreateChatCompletionRequest, OpenAIApi } from "npm:openai@3.2.1"

/**
 * You can read about the parameters at 
 * https://platform.openai.com/docs/api-reference/chat/create
 */
export async function main(
  auth: Resource<'openai'>,
  messages: {
    role: 'assistant' | 'system' | 'user',
    content: string,
    name?: string
  }[],
  model: 'gpt-3.5-turbo' | 'gpt-3.5-turbo-0301' = 'gpt-3.5-turbo',
  frequency_penalty?: number,
  logit_bias?: object,
  max_tokens?: number,
  n?: number,
  presence_penalty?: number,
  stop?: string,
  stream?: boolean,
  temperature?: number,
  top_p?: number,
  user?: string,
) {
  const configuration = new Configuration({
    apiKey: auth.api_key,
    organization: auth.organization_id
  });
  const openai = new OpenAIApi(configuration);
  
  const request: CreateChatCompletionRequest = {
    messages,
    model,
    frequency_penalty,
    logit_bias,
    max_tokens,
    n,
    presence_penalty,
    stop,
    stream,
    temperature,
    top_p,
    user
  }
  const response = await openai.createChatCompletion(
    removeObjectEmptyFields(request)
  )
  return response.data
}

Submitted by adam186 44 days ago

Edited 44 days ago

No comments yet

Login to be able to comment