WebSim AI Capabilities Documentation

The websim object is globally available in your project and does not need to be imported or included separately.

AI Chat Completions

Sequence diagram of a chat request using websim.chat.completions.create with messages and a returned completion.
Request/response sequence for Chat: your app builds messages, sends them via websim.chat.completions.create, and receives completion.content to render.

Engage with language models to generate text, answer questions, or perform various language-based tasks.

Basic AI Chat

To interact with the AI chat, you use the websim.chat.completions.create method. You need to provide a history of messages. The AI will respond based on this conversation history.


// Initialize or retrieve your conversation history
let conversationHistory = [];

async function sendMessageToAI(userMessageText) {
  const newMessage = {
    role: "user",
    content: userMessageText,
  };
  conversationHistory.push(newMessage);

  // Optional: Keep only the last 10 messages to manage context length
  conversationHistory = conversationHistory.slice(-10);

  try {
    const completion = await websim.chat.completions.create({
      messages: [
        {
          role: "system", // The system message is optional
          content: "You are a friendly assistant who speaks in rhyming verse.",
        },
        ...conversationHistory,
      ],
    });

    const aiResponse = completion.content;
    console.log("AI Response:", aiResponse);

    // Add AI's response to the history
    conversationHistory.push({ role: "assistant", content: aiResponse });
    
    return aiResponse;
  } catch (error) {
    console.error("Error communicating with AI:", error);
    // Handle error appropriately
    return "Sorry, I couldn't process that.";
  }
}

// Example usage:
// sendMessageToAI("What is two plus two?").then(response => {
//   // Update your UI with the response
// });
            

Key Points:

JSON Output Mode

You can instruct the AI to respond in a structured JSON format. This is useful for tasks where you need predictable, parsable output.


async function getStructuredDataFromAI(promptText) {
  try {
    const completion = await websim.chat.completions.create({
      messages: [
        {
          role: "system",
          content: `Combine the following concepts into a single, coherent idea.
Respond directly with JSON, following this JSON schema, and no other text.
{
  concept: string;
  emoji: string;
}`,
        },
        {
          role: "user",
          content: [
            { type: "text", text: promptText }, // e.g., "eggs + fire"
          ],
        },
      ],
      json: true,
    });

    const result = JSON.parse(completion.content);
    let concept = result.concept;
    let emoji = result.emoji;

    console.log("Concept:", concept, "Emoji:", emoji);
    return result;

  } catch (error) {
    console.error("Error getting structured data from AI:", error);
  }
}

// Example usage:
// getStructuredDataFromAI("ocean + stars").then(data => {
//   if (data) {
//     // Use data.concept and data.emoji
//   }
// });
            

Key Points:

Image Input with JSON Output

The AI can also process images and provide structured JSON output based on the image content.


async function analyzeImageWithAI(imageDataUrl) {
  try {
    const completion = await websim.chat.completions.create({
      messages: [
        {
          role: "user",
          content: [
            {
              type: "text",
              text: `Determine if the image is a natural photograph of an animal.
Respond directly with JSON, following this JSON schema, and no other text.
{
  isPhotograph: boolean;
  isAnimal: boolean;
  isInNature: boolean;
}`,
            },
            {
              type: "image_url",
              image_url: { url: imageDataUrl }, // e.g., from a file input or canvas
            },
          ],
        },
      ],
      json: true,
    });

    let result = JSON.parse(completion.content);
    let isPhotograph = result.isPhotograph;
    let isAnimal = result.isAnimal;
    let isInNature = result.isInNature;

    console.log("Image Analysis:", result);
    return result;

  } catch (error) {
    console.error("Error analyzing image with AI:", error);
  }
}

Key Points:

Visual Overview

Diagram showing the websim object and its main capabilities: Chat, Image Generation, and Text to Speech.
An at-a-glance conceptual map of the websim object and its core APIs, showing how Chat, Image Generation, and Text to Speech connect to your app.

AI Image Generation

Pipeline diagram from prompt and options (aspect ratio, size, seed, transparency) to generated image URL.
ImageGen pipeline: combine a prompt with options (aspect ratio, dimensions, seed, transparency) to receive a URL you can display immediately.

Generate images from textual descriptions or existing images using websim.imageGen.

Pro Tip: websim.imageGen takes approximately 10 seconds. It is highly recommended to show a 10-second loading indicator to the user.

async function generateImageFromPrompt(promptText, aspectRatio = "1:1", seedValue, transparentBg = false) {
  try {
    const options = {
      prompt: promptText,
      aspect_ratio: aspectRatio, // "1:1", "16:9", "21:9", "3:2", "2:3", "4:5", "5:4", "3:4", "4:3", "9:16", "9:21"
      transparent: transparentBg,
    };

    if (seedValue !== undefined) {
      options.seed = seedValue; // Use a seed for reproducible variations
    }

    const result = await websim.imageGen(options);

    console.log("Generated Image URL:", result.url);
    // Use result.url to display the image, e.g., 
    return result.url;

  } catch (error) {
    console.error("Error generating image:", error);
    // Handle error
  }
}

// Example usage:
// generateImageFromPrompt("A crayon child's drawing of a turtle", "16:9").then(url => {
//   if (url) { /* display image */ }
// });

// generateImageFromPrompt("A photorealistic apple", "1:1", undefined, true).then(url => {
//  if (url) { /* display image with transparent background */ }
// });


// You can also specify width and height directly (overrides aspect_ratio)
async function generateImageWithCustomDimensions(promptText, width, height) {
  try {
    const result = await websim.imageGen({
      prompt: promptText,
      width: width,
      height: height,
    });
    console.log("Generated Image URL (custom dimensions):", result.url);
    return result.url;
  } catch (error) {
    console.error("Error generating image with custom dimensions:", error);
  }
}

// Example:
// generateImageWithCustomDimensions("A beautiful sunset over a calm ocean", 1024, 768);

            

Image-to-Image Generation

You can use existing images as references for generation by passing an image_inputs array. You can provide 1-4 images as base64 data URLs.


// Utility to convert a URL to a Data URL
async function urlToDataUrl(url) {
  const response = await fetch(url);
  const blob = await response.blob();
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onloadend = () => resolve(reader.result);
    reader.onerror = reject;
    reader.readAsDataURL(blob);
  });
}

async function generateCartoonFromImage(sourceImageUrl, promptText = "Convert this image to a cartoon style") {
  try {
    const base64Image = await urlToDataUrl(sourceImageUrl);
    
    const result = await websim.imageGen({
      prompt: promptText,
      image_inputs: [
        { url: base64Image }
      ],
    });
    
    return result.url;
  } catch (error) {
    console.error("Image-to-image error:", error);
  }
}
            

Key Parameters for websim.imageGen:

Result:

The method returns a promise that resolves to an object containing a url property. This URL points to the generated image.

Text to Speech (TTS)

Flow diagram converting text to an MP3 URL using a selected voice or voice ID.
TTS flow: provide text and a voice (language-gender code or ElevenLabs ID) to get an MP3 URL suitable for audio playback.

Convert text into spoken audio using websim.textToSpeech.


async function convertTextToSpeech(textToSpeak, voiceCode = "en-male") {
  try {
    const result = await websim.textToSpeech({
      text: textToSpeak,
      voice: voiceCode, // e.g., "en-male", "es-female"
    });

    console.log("Generated Speech Audio URL (MP3):", result.url);
    // Example: 
    return result.url;

  } catch (error) {
    console.error("Error converting text to speech:", error);
  }
}

// ElevenLabs voice IDs are also supported:
async function convertTextToSpeechWithVoiceId(textToSpeak, elevenLabsVoiceId) {
  try {
    const result = await websim.textToSpeech({
      text: textToSpeak,
      voice: elevenLabsVoiceId, // e.g., "pNInz6obpgDQGcFmaJgB"
    });
    return result.url;
  } catch (error) {
    console.error("Error with TTS voice ID:", error);
  }
}

Key Parameters for websim.textToSpeech:

Result:

The method returns a promise that resolves to an object containing a url property to an .mp3 audio file.