Non-streaming chat
Hopfield provides a simple way to interact with chat models. You can use different API providers with type guarantees with Zod.
Usage
Use chat models from OpenAI:
ts
ts
importhop from "hopfield";importopenai from "hopfield/openai";importOpenAI from "openai";consthopfield =hop .client (openai ).provider (newOpenAI ());constchat =hopfield .chat ();constmessages :hop .inferMessageInput <typeofchat >[] = [{role : "user",content : "How do you count to ten?",},];constresponse = awaitchat .get ({messages ,});constresponseType =response .choices [0].__type ;if (responseType === "stop") {constmessage =response .choices [0].message ;}
ts
importhop from "hopfield";importopenai from "hopfield/openai";importOpenAI from "openai";consthopfield =hop .client (openai ).provider (newOpenAI ());constchat =hopfield .chat ();constmessages :hop .inferMessageInput <typeofchat >[] = [{role : "user",content : "How do you count to ten?",},];constresponse = awaitchat .get ({messages ,});constresponseType =response .choices [0].__type ;if (responseType === "stop") {constmessage =response .choices [0].message ;}
Parameters
Model Name
The model name to use for the embedding.
ts
const hopfield = hop.client(openai).provider(new OpenAI());
const chat = hopfield.chat("gpt-4-0613");
const hopfield = hop.client(openai).provider(new OpenAI());
const chat = hopfield.chat("gpt-4-0613");
OpenAI
The default model name is shown below. To override this, you must use a model which is enabled on your OpenAI account.
ts
ts
import type {DefaultOpenAIChatModelName } from "hopfield/openai";
ts
import type {DefaultOpenAIChatModelName } from "hopfield/openai";
All possible model names are shown below (reach out if we are missing one!)
ts
ts
import type {OpenAIChatModelName } from "hopfield/openai";
ts
import type {OpenAIChatModelName } from "hopfield/openai";
Response Count
The number of chat responses to be returned (this is usually referred to as n
). For all providers, this defaults to 1
. This is capped at 20
.
ts
const hopfield = hop.client(openai).provider(new OpenAI());
const chat = hopfield.chat("gpt-4-0613", 10);
const hopfield = hop.client(openai).provider(new OpenAI());
const chat = hopfield.chat("gpt-4-0613", 10);
The response can then be safely used:
ts
ts
constmessages :hop .inferMessageInput <typeofchat >[] = [{role : "user",content : "What's the best way to get a bunch of chat responses?",},];constresponse = awaitchat .get ({messages ,});constchatCount =response .choices .length ;
ts
constmessages :hop .inferMessageInput <typeofchat >[] = [{role : "user",content : "What's the best way to get a bunch of chat responses?",},];constresponse = awaitchat .get ({messages ,});constchatCount =response .choices .length ;