openai

Unofficial Deno wrapper for the Open Ai api

MIT License

Stars
73

Unofficial Deno wrapper for the Open AI API

Usage

Your Open AI Api key (found here) is needed for this library to work. We recommend setting it as an environment variable. Here is a configuration example.

import { OpenAI } from "https://deno.land/x/openai/mod.ts";

const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);

Completion

import { OpenAI } from "https://deno.land/x/openai/mod.ts";

const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);

const completion = await openAI.createCompletion({
  model: "davinci",
  prompt: "The meaning of life is",
});

console.log(completion.choices);

Chat Completion

import { OpenAI } from "https://deno.land/x/openai/mod.ts";

const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);

const chatCompletion = await openAI.createChatCompletion({
  model: "gpt-3.5-turbo",
  messages: [
    { "role": "system", "content": "You are a helpful assistant." },
    { "role": "user", "content": "Who won the world series in 2020?" },
    {
      "role": "assistant",
      "content": "The Los Angeles Dodgers won the World Series in 2020.",
    },
    { "role": "user", "content": "Where was it played?" },
  ],
});

console.log(chatCompletion);

Image

import { OpenAI } from "https://deno.land/x/openai/mod.ts";

const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);

const image = await openAI.createImage({
  prompt: "A unicorn in space",
});

console.log(image);

Edit

import { OpenAI } from "https://deno.land/x/openai/mod.ts";

const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);

const edit = await openAI.createEdit({
  model: "text-davinci-edit-001",
  input: "What day of the wek is it?",
  instruction: "Fix the spelling mistakes",
});

console.log(edit);

Image Edit

import { OpenAI } from "https://deno.land/x/openai/mod.ts";

const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);

const imageEdit = await openAI.createImageEdit({
  image: "@otter.png",
  mask: "@mask.png",
  prompt: "A cute baby sea otter wearing a beret",
  n: 2,
  size: "1024x1024",
});

console.log(imageEdit);

Image Variation

import { OpenAI } from "https://deno.land/x/openai/mod.ts";

const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);

const imageVariation = await openAI.createImageVariation({
  image: "@otter.png",
  n: 2,
  size: "1024x1024",
});

console.log(imageVariation);

Audio Transcription

import { OpenAI } from "https://deno.land/x/openai/mod.ts";

const openAI = new OpenAI(Deno.env.get("YOUR_API_KEY")!);

const transcription = await openAI.createTranscription({
  model: "whisper-1",
  file: "/path/to/your/audio/file.mp3",
});

console.log(transcription);

Maintainers

License

MIT

Badges
Extracted from project README
Tags Doc Checks License
Related Projects