The lil’bots platform comes with OpenAI access built in. You can use the OpenAI API to build bots that can generate text, answer questions, generate images and more.

Using the OpenAI API

To use OpenAI, you can make requests to the OpenAI API without including an API key. The requests will be recognized and authroized by the lil’bots platform.

The following endpoints are supported (including any nexted routes):

  • /v1/models - list the models available for use docs
  • /v1/chat - chat with a model / generate text docs
  • /v1/audio - generate audio from text (text to speech) and transcribe text docs
  • /v1/images - generate images from text docs

To read the full OpenAI API documentation, visit the OpenAI API Reference.

Using the OpenAI SDK - JavaScript Runtime

You can use the OpenAI SDK in your bots to make requests to the OpenAI API. The SDK is available in the JavaScript runtime by installing it from deno.land:

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

You can then use the SDK to make requests to the OpenAI API. Here’s an example of a simple bot that uses the OpenAI API to answer a question:

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

const openai = new OpenAI();

export async function main(inputs, params) {
	const { question } = inputs;

	const chatCompletion = await openai.chat.completions.create({
    	messages: [{ role: 'user', content: question }],
    	model: 'gpt-4o',
  	});

	const response = chatCompletion.choices[0].message.content

	return [{
		title: "Answer",
		message: response
	}]
}

Generating Speech with OpenAI

You can use the OpenAI API to generate speech from text. Here’s an example of a bot that uses the OpenAI API to generate speech from text:

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

const openai = new OpenAI();

export async function main(inputs, params) {
	const speechFile = "./speech.mp3"

	const mp3 = await openai.audio.speech.create({
    	model: "tts-1",
    	voice: "alloy",
    	input: "Today is a wonderful day to build something people love!",
  	});
  	const buffer = Buffer.from(await mp3.arrayBuffer());
  	await fs.promises.writeFile(speechFile, buffer);

	return {
		file: speechFile
	}
}

Generating Images with OpenAI

You can use the OpenAI API to generate images from text. Here’s an example of a bot that uses the OpenAI API to generate an image from text:

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

export async function main(inputs, params, accounts) {
  const openai = new OpenAI();

  const response = await openai.images.generate({
    prompt: inputs.prompt,
    n: 1,
    size: params.size,
  });

  const imageUrl = response.data[0].url;

  return [
    {
      type: "image",
      url: imageUrl,
      title: "Generated Image",
      description: `Image generated from prompt: "${inputs.prompt}"`
    }
  ];
}