Step 1: Update Your WebSocket Connection

If you’re already using the OpenAI Realtime API, simply update your WebSocket connection URL:

const socket = new WebSocket('wss://ws.squiggle.ai/v1/realtime');

Step 2: Initialize the Connection

Initialize the connection as you normally would with the OpenAI Realtime API. Squiggle is fully compatible, so your existing code should work without modifications.

Step 3: Handle Squiggle Insights

Squiggle will send additional messages with insights via the websocket connection.

Using Node ws Library

If you are using the Node.js ws library, you can use theonmessage callback to detect new insights:

socket.onmessage = function(event) {
  const data = JSON.parse(event.data);
  if (data.type === 'insights.updated') {
    console.log('Received Session insights:', data.insights);
    // Handle the insight (e.g., display summary, extracted data, highlights, or flags)
  } else {
    // Handle regular OpenAI messages
  }
};

Using the Realtime API JavaScript Client

OpenAI has published a Realtime API client that provides a layer of abstraction over using the ws library directly. You can use the client to handle Squiggle insights:

    client.on('realtime.event', (realtimeEvent: RealtimeEvent) => {
      // Update reference profile when insights are received
      if (realtimeEvent.event.type === 'insights.updated') {
        // Handle the insight (e.g., display summary, extracted data, highlights, or flags)
      }
    });

Using the Squiggle REST API

You can also asycnhronously fetch insights related to sessions via the Squiggle REST API, including after the session has ended. Check out the API Reference for more details.

Step 4: Customize Your Configuration (Optional)

You can customize how Squiggle generates insights using the Squiggle REST API. Send a POST request to https://api.squiggle.ai/api/configure with your desired configuration:

fetch('https://api.squiggle.ai/api/configure', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    apiKey: 'your-open-ai-api-key',
    configuration: {
      identifier: 'customConfigIdentifier',
      insightFrequency: 10, // Generate insights for every 10 conversation items
      customFields: [
        {
          name: 'userName',
          description: 'The name of the current user.',
          type: 'string'
        },
        {
          name: 'requestedProduct',
          description: 'The name of the product or service the user is requesting.',
          type: 'string'
        },
      ],
      flagInstructions: "Flag any instances of abusive or inappropriate content from users, and identify any potential AI safety risks or concerning AI behaviors in the conversation.",
      highlightInstructions: "Provide at least 3 and up to 10 key highlights (quotes or notes) that capture key aspects of the conversation. Specifically highlight any positive or negative quotes shared about the company's products or services."
    }
  })
})
.then(response => response.json())
.then(data => console.log('Configuration updated:', data));

That’s it! You’re now ready to use Squiggle in your application. Check out the Squiggle API Reference for more detailed information on configuring your setup.