Skip to content
On this page

Streaming chat

Hopfield provides a simple way to interact with streaming chat models. You can use various API providers with type guarantees with Zod.

Usage

Use streaming chat models from OpenAI with a few lines of code:

ts
ts
import hop from "hopfield";
import openai from "hopfield/openai";
import OpenAI from "openai";
 
const hopfield = hop.client(openai).provider(new OpenAI());
 
const chat = hopfield.chat().streaming();
 
const messages: hop.inferMessageInput<typeof chat>[] = [
{
role: "user",
content: "What's the coolest way to count to ten?",
},
];
 
const response = await chat.get({
messages,
});
 
// store all of the streaming chat chunks
const parts: hop.inferResult<typeof chat>[] = [];
 
for await (const part of response) {
parts.push(part);
 
// if the streaming delta contains new text content
if (part.choices[0].__type === "content") {
(property) __type: "stop" | "length" | "role" | "content" | "content_filter"
// action based on the delta for the streaming message content
await takeAction(part.choices[0].delta.content);
(property) delta: { content: string; }
}
}
ts
import hop from "hopfield";
import openai from "hopfield/openai";
import OpenAI from "openai";
 
const hopfield = hop.client(openai).provider(new OpenAI());
 
const chat = hopfield.chat().streaming();
 
const messages: hop.inferMessageInput<typeof chat>[] = [
{
role: "user",
content: "What's the coolest way to count to ten?",
},
];
 
const response = await chat.get({
messages,
});
 
// store all of the streaming chat chunks
const parts: hop.inferResult<typeof chat>[] = [];
 
for await (const part of response) {
parts.push(part);
 
// if the streaming delta contains new text content
if (part.choices[0].__type === "content") {
(property) __type: "stop" | "length" | "role" | "content" | "content_filter"
// action based on the delta for the streaming message content
await takeAction(part.choices[0].delta.content);
(property) delta: { content: string; }
}
}

Learn more

See how to use streaming results combined with type-driven prompt templates in the next section.

Released under the MIT License.