Implementing ChatGPT with Stream Chat

3 min read
Ryan Kavanaugh
Ryan Kavanaugh
Published February 7, 2024

With the advent and increasing popularity of artificial intelligence (AI) large language models (LLMs) like ChatGPT and Google's Gemini, many customers wish to implement these solutions to maintain feature parity in the marketplace and provide highly engaging and memorable experiences for their user base.

As an industry-leading solution, Stream evolves alongside these market shifts and provides an easy implementation with ChatGPT. Follow these simple steps to supercharge your app with ChatGPT with a lightning fast implementation.

First, set up an OpenAI account, create an API Key, head to the docs page, and navigate to the tutorial here for setting up your OpenAI API credentials locally.

Step One of Implementation

Next, you will use your existing Stream Chat App or create a new Stream Chat App by clicking “Create App” in the dashboard.

Step Two of Implementation

To quickly test this implementation locally, you can then set up a local server to receive all of your API requests from the Stream webhook with Ngrok. Set up a Ngrok local server, create a free account, and then add an auth token.

Auth token command & Ngrok local server for testing.

Next, set the Stream webhook to send events to your Ngrok Server by including this link in your Webhook URL below. For this example, we assume you set the Port the 80 for the Ngrok server.

Now, we will create a NodeJS script that will exist server-side for your production deployment and create the 2-way communication between Stream Chat and ChatGPT. In this example, it will run locally on Port 80.

Receive the Stream new message webhook event.

javascript"/", (req, res) => {
  let body="";
  req.on("data", (chunk) => {

    // Payload from Stream Webhook
    req.on("end", async () => {
    let parsedBody = JSON.parse(body);

Next, save the message sent by the user, the Channel ID, Channel Type, and User ID.

// Capture all the relevant data from the Stream message object
const channelID =;
const channelType =;
const incomingMessage = parsedBody.message.text.toLowerCase();
const user =;

Now, we will send the user’s message to ChatGPT.

// Send Chat GPT the user's message
const GPTResponse = await{
  messages: [{ role: "system", content: incomingMessage }],
  model: "gpt-3.5-turbo",

Finally, we will send the message response from ChatGPT back to the same channel and from a user named “ChatGPT, AI Assistant, etc.”

  // Separate out GPT's reponse from other data fields
      let messageResponse = GPTResponse.choices[0].message.content

    // Send the Chat GPT response back as a message to the same channel
    // Initialize Chat & fetch the channel
    const client = StreamChat.getInstance(process.env.KEY,process.env.SECRET);
    const channel =, channelID, {})

    // Send GPT response to the channel 
        const gptmessage = {
          text: messageResponse,
          user_id: AIAssitant

Here is the entire script for connecting Stream to ChatGPT.

const cors = require("cors");
const port = process.env.PORT || 80;
const express = require("express");
const app = express();
const OpenAI = require("openai");
const openai = new OpenAI();
const StreamChat = require("stream-chat").StreamChat;
// ====================================================================
// This Chat GPT Implementation is created in 3 easy steps:
// Step 1: Save relevant data from the Stream message object provided via webhook event
// Step 2: Send Chat GPT the user's message/question and get the response
// Step 3. Send the Chat GPT reponse back as a message to the same channel

Now you have successfully implemented ChatGPT with Stream Chat! Please contact us with any questions.

decorative lines
Integrating Video With Your App?
We've built a Video and Audio solution just for you. Check out our APIs and SDKs.
Learn more ->