Skip to content

OpenAI Streaming Example

This example shows how to integrate oRPC with the OpenAI Streaming API to build a chatbot.

Basic Example

ts
import 
OpenAI
from 'openai'
const
openapi
= new
OpenAI
()
const
complete
=
os
.
input
(
z
.
object
({
content
:
z
.
string
() }))
.
handler
(async function* ({
input
}) {
const
stream
= await
openapi
.
chat
.
completions
.
create
({
model
: 'gpt-4o',
messages
: [{
role
: 'user',
content
:
input
.
content
}],
stream
: true,
}) yield *
stream
}) const
router
= {
complete
}
type
ClientContext
= {
disableEIRetry
?: boolean }
const
link
= new
RPCLink
<
ClientContext
>({
url
: 'https://example.com/rpc',
eventIteratorShouldRetry
: (
_
, {
context
}) => !
context
?.
disableEIRetry
,
}) const
client
:
RouterClient
<typeof
router
,
ClientContext
> =
createORPCClient
(
link
)
const
stream
= await
client
.
complete
(
{
content
: 'Hello, world!' },
{
context
: {
disableEIRetry
: true } }
) for await (const
chunk
of
stream
) {
console
.
log
(
chunk
.
choices
[0]?.
delta
?.
content
|| '')
}

Note: Disable event iterator retries when streaming chatbot responses.

Learn more about RPCLink and Event Iterator.

Released under the MIT License.