https://retellai.com/?utm_source=discord&utm_medium=seo logo
<@1145576664366055464> how to create custom llm
# 💬・general
a
@AI Evy how to create custom llm
a
Thanks for tagging me @ajaykumar_71943. I'll look through relevant sources and get back to you shortly.
### Answer To create a custom LLM with Retell AI, you need to set up a backend server that handles text exchanges with Retell server to provide responses to users [(1) ]() . This involves integrating your LLM via WebSocket . ## Overview The process involves setting up a backend server that communicates with Retell server through WebSocket connections . Your LLM server needs to send the first message upon WebSocket connection and respond to user interactions . ## Step-by-Step Process 1. Set up WebSocket Server First, add a basic websocket endpoint to your server [(2) ]() :
Copy code
javascript
import { RawData, WebSocket } from "ws";
import { Request } from "express";

var express = require('express') ;
var app = express() ;
var expressWs = require('express-ws') (app) ;
const port = 3000

app.ws("/llm-websocket/:call_id",
  async (ws: WebSocket, req: Request) => {
    const callId = req.params.call_id;

    ws.on("error", (err) => {
      console.error("Error received in LLM websocket client: ", err) ;
    }) ;
    ws.on("message", async (data: RawData, isBinary: boolean) => {
      console.log(data) ;
    }) ;
  },
) ;

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
}) ;
2. Create Response System Define the request and response interfaces :
Copy code
javascript
interface Utterance {
  role: "agent" | "user";
  content: string;
}

export interface RetellRequest {
  response_id?: number;
  transcript: Utterance[];
  interaction_type: "update_only" | "response_required" | "reminder_required";
}

export interface RetellResponse {
  response_id?: number;
  content: string;
  content_complete: boolean;
  end_call: boolean;
}
3. Integrate with Your LLM Here's an example using OpenAI [(3) ]() :
Copy code
typescript
import {
  OpenAIClient,
  AzureKeyCredential,
  ChatRequestMessage,
  GetChatCompletionsOptions,
} from "@azure/openai";
import { WebSocket } from "ws";

export class DemoLlmClient {
  private client: OpenAIClient;

  constructor() {
    this.client = new OpenAIClient(
      process.env.AZURE_OPENAI_ENDPOINT,
      new AzureKeyCredential(process.env.AZURE_OPENAI_KEY) ,
    ) ;
  }

  BeginMessage(ws: WebSocket) {
    const res: RetellResponse = {
      response_id: 0,
      content: beginSentence,
      content_complete: true,
      end_call: false,
    };
    ws.send(JSON.stringify(res) ) ;
  }

  async DraftResponse(request: RetellRequest, ws: WebSocket) {
    if (request.interaction_type === "update_only") {
      return;
    }
    const requestMessages: ChatRequestMessage[] = this.PreparePrompt(request) ;

    const option: GetChatCompletionsOptions = {
      temperature: 0.3,
      maxTokens: 200,
      frequencyPenalty: 1,
    };

    try {
      let events = await this.client.streamChatCompletions(
        process.env.AZURE_OPENAI_DEPLOYMENT_NAME,
        requestMessages,
        option,
      ) ;

      for await (const event of events) {
        if (event.choices.length >= 1) {
          let delta = event.choices[0].delta;
          if (!delta || !delta.content) continue;
          const res: RetellResponse = {
            response_id: request.response_id,
            content: delta.content,
            content_complete: false,
            end_call: false,
          };
          ws.send(JSON.stringify(res) ) ;
        }
      }
    } catch (err) {
      console.error("Error in gpt stream: ", err) ;
    } finally {
      const res: RetellResponse = {
        response_id: request.response_id,
        content: "",
        content_complete: true,
        end_call: false,
      };
      ws.send(JSON.stringify(res) ) ;
    }
  }
}
## Demo Repositories Retell AI provides complete demo repositories to help you get started [(1) ]() : - [Node.js Demo Repository]() - [Python Demo Repository]() These repositories show how to build an LLM solution with OpenAI/Azure OpenAI, start an LLM websocket server, and use Twilio to make phone calls with Retell agents programmatically .
​ To learn more, see the sources I considered: [[1] Custom LLM Overview](https://docs.retellai.com/integrate-llm/overview) [[2] Setup WebSocket Server](https://docs.retellai.com/integrate-llm/setup-websocket-server) [[3] Integrate LLM](https://docs.retellai.com/integrate-llm/integrate-llm) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
a
is llm and conversational same