|  | 
|  | 1 | +# sveltekit openai realtime api | 
|  | 2 | + | 
|  | 3 | +this is a sveltekit port of the [openai-realtime-console](https://github.com/openai/openai-realtime-console) repository. | 
|  | 4 | + | 
|  | 5 | +it allows you to easily use the openai realtime api in your sveltekit project. | 
|  | 6 | + | 
|  | 7 | +work in progress, but it should be functional. | 
|  | 8 | + | 
|  | 9 | +## how to use | 
|  | 10 | + | 
|  | 11 | +> [!WARNING] | 
|  | 12 | +> this is the setup for the client side only version of the realtime api, you will need to use a relay server to use this in production. | 
|  | 13 | +> see further down for more information on how to use a relay server. | 
|  | 14 | +
 | 
|  | 15 | +1. copy the `src/lib/realtime/` folder from this repository into your sveltekit projects `src/lib/` folder | 
|  | 16 | + | 
|  | 17 | +2. install the dependency: | 
|  | 18 | + | 
|  | 19 | +```bash | 
|  | 20 | +$ npm i openai/openai-realtime-api-beta | 
|  | 21 | +``` | 
|  | 22 | + | 
|  | 23 | +2. import the `Realtime` component into your svelte file and use it e.g. like so: | 
|  | 24 | + | 
|  | 25 | +```svelte | 
|  | 26 | +<script lang="ts"> | 
|  | 27 | +	import Realtime from '$lib/realtime/realtime.svelte'; | 
|  | 28 | +	import type { ItemType } from '@openai/realtime-api-beta/dist/lib/client'; | 
|  | 29 | +
 | 
|  | 30 | +	let startConversation: () => Promise<void>; | 
|  | 31 | +	let endConversation: () => Promise<void>; | 
|  | 32 | +
 | 
|  | 33 | +	let turnDetection: 'server_vad' | 'none' = 'server_vad'; | 
|  | 34 | +
 | 
|  | 35 | +	let items: ItemType[] = []; | 
|  | 36 | +
 | 
|  | 37 | +	let isConnected = false; | 
|  | 38 | +	let isRecording = false; | 
|  | 39 | +
 | 
|  | 40 | +	let startRecording: () => Promise<void>; | 
|  | 41 | +	let stopRecording: () => Promise<void>; | 
|  | 42 | +
 | 
|  | 43 | +	// set your api key here | 
|  | 44 | +	let apiKey = ''; | 
|  | 45 | +</script> | 
|  | 46 | +
 | 
|  | 47 | +{#if apiKey} | 
|  | 48 | +	<Realtime | 
|  | 49 | +		bind:startConversation | 
|  | 50 | +		bind:endConversation | 
|  | 51 | +		bind:isConnected | 
|  | 52 | +		bind:isRecording | 
|  | 53 | +		bind:startRecording | 
|  | 54 | +		bind:stopRecording | 
|  | 55 | +		bind:items | 
|  | 56 | +		{turnDetection} | 
|  | 57 | +		{apiKey} | 
|  | 58 | +	/> | 
|  | 59 | +{/if} | 
|  | 60 | +``` | 
|  | 61 | + | 
|  | 62 | +see `src/routes/+page.svelte` for a full example. | 
|  | 63 | + | 
|  | 64 | +## relay server | 
|  | 65 | + | 
|  | 66 | +for production use, you will need to use a relay server to use the realtime api. | 
|  | 67 | + | 
|  | 68 | +1. add `OPENAI_API_KEY` to your `.env` file | 
|  | 69 | + | 
|  | 70 | +2. tell the Realtime component to use the relay server: | 
|  | 71 | + | 
|  | 72 | +```svelte | 
|  | 73 | +<Realtime useRelayServer /> | 
|  | 74 | +``` | 
|  | 75 | + | 
|  | 76 | +then you have two options: | 
|  | 77 | + | 
|  | 78 | +### run the relay server with your sveltekit server | 
|  | 79 | + | 
|  | 80 | +change your `vite.config.ts` to this: | 
|  | 81 | + | 
|  | 82 | +```ts | 
|  | 83 | +import { realtimeWebSocketServer } from './src/lib/realtime/realtime_server'; | 
|  | 84 | +import { sveltekit } from '@sveltejs/kit/vite'; | 
|  | 85 | +import { defineConfig } from 'vite'; | 
|  | 86 | + | 
|  | 87 | +export default defineConfig({ | 
|  | 88 | +	plugins: [sveltekit(), realtimeWebSocketServer] | 
|  | 89 | +}); | 
|  | 90 | +``` | 
|  | 91 | + | 
|  | 92 | +### run the relay server independently | 
|  | 93 | + | 
|  | 94 | +- copy the `relay-server/` folder (identical to the code in the | 
|  | 95 | +  [openai-realtime-console](https://github.com/openai/openai-realtime-console) repository) | 
|  | 96 | +  from this repository into your project | 
|  | 97 | + | 
|  | 98 | +- install the dependencies: | 
|  | 99 | + | 
|  | 100 | +```bash | 
|  | 101 | +npm i dotenv openai/openai-realtime-api-beta | 
|  | 102 | +``` | 
|  | 103 | + | 
|  | 104 | +- run the relay server: | 
|  | 105 | + | 
|  | 106 | +```bash | 
|  | 107 | +$ node relay-server/index.js | 
|  | 108 | +``` | 
|  | 109 | + | 
|  | 110 | +- add the relay server url to your Realtime component: | 
|  | 111 | + | 
|  | 112 | +``` | 
|  | 113 | +<Realtime useRelayServer relayServer="http://localhost:8081" /> | 
|  | 114 | +``` | 
|  | 115 | + | 
|  | 116 | +## todos | 
|  | 117 | + | 
|  | 118 | +- [ ] add tests | 
|  | 119 | +- [ ] add more documentation | 
|  | 120 | +- [ ] show waveforms | 
|  | 121 | +- [ ] tool calling | 
|  | 122 | +- [ ] update ui | 
|  | 123 | + | 
|  | 124 | +## license | 
|  | 125 | + | 
|  | 126 | +MIT | 
0 commit comments