Embeddings
Hopfield allows developers to easily query and validate responses from embeddings providers. You can use different APIs with type guarantees with Zod and composability.
Usage
Create and use embeddings from OpenAI, directly with an API client:
ts
ts
importhop from "hopfield";importopenai from "hopfield/openai";importOpenAI from "openai";consthopfield =hop .client (openai ).provider (newOpenAI ());constembeddings =hopfield .embedding ("text-embedding-ada-002", 3);constresponse = awaitembeddings .get ({input : ["ready", "set", "hop"] });constembeddingCount =response .data .length ;const embeddingCount: 3constembeddingLength =response .data [0].embedding .length ;const embeddingLength: 1536
ts
importhop from "hopfield";importopenai from "hopfield/openai";importOpenAI from "openai";consthopfield =hop .client (openai ).provider (newOpenAI ());constembeddings =hopfield .embedding ("text-embedding-ada-002", 3);constresponse = awaitembeddings .get ({input : ["ready", "set", "hop"] });constembeddingCount =response .data .length ;const embeddingCount: 3constembeddingLength =response .data [0].embedding .length ;const embeddingLength: 1536
Or if you prefer, you can only use Hopfield's Zod validations, and use the OpenAI SDK directly:
ts
ts
importhop from "hopfield";importopenai from "hopfield/openai";importOpenAI from "openai";constopenAIClient = newOpenAI ();consthopfield =hop .client (openai );constembeddings =hopfield .embedding ("text-embedding-ada-002", 3);constresponse = awaitopenAIClient .embeddings .create ({model :embeddings .model ,input : ["ready", "set", "hop"],});constparsed =embeddings .returnType .parse (response );constembeddingCount =parsed .data .length ;const embeddingCount: 3constembeddingLength =parsed .data [0].embedding .length ;const embeddingLength: 1536
ts
importhop from "hopfield";importopenai from "hopfield/openai";importOpenAI from "openai";constopenAIClient = newOpenAI ();consthopfield =hop .client (openai );constembeddings =hopfield .embedding ("text-embedding-ada-002", 3);constresponse = awaitopenAIClient .embeddings .create ({model :embeddings .model ,input : ["ready", "set", "hop"],});constparsed =embeddings .returnType .parse (response );constembeddingCount =parsed .data .length ;const embeddingCount: 3constembeddingLength =parsed .data [0].embedding .length ;const embeddingLength: 1536
Parameters
Model Name
The model name to use for the embedding. This parameter depends on the client
specified. The embedding length will change based on this parameter, since different text embeddings can have varying lengths.
ts
ts
constembeddings =hopfield .embedding ("text-embedding-ada-002");constresponse = awaitembeddings .get ({input : ["hop"] });constembedding =response .data [0].embedding ;const embedding: [number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, ... 1514 more ..., number]
ts
constembeddings =hopfield .embedding ("text-embedding-ada-002");constresponse = awaitembeddings .get ({input : ["hop"] });constembedding =response .data [0].embedding ;const embedding: [number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, number, ... 1514 more ..., number]
OpenAI
The OpenAI model name defaults to the value shown below. This is currently the only supported model.
ts
ts
import type {DefaultOpenAIEmbeddingModelName } from "hopfield/openai";(alias) type DefaultOpenAIEmbeddingModelName = "text-embedding-ada-002" import DefaultOpenAIEmbeddingModelName
ts
import type {DefaultOpenAIEmbeddingModelName } from "hopfield/openai";(alias) type DefaultOpenAIEmbeddingModelName = "text-embedding-ada-002" import DefaultOpenAIEmbeddingModelName
Embedding Count
The count of text embeddings to be returned. For all providers, this defaults to 1
. This is capped at 20
.
ts
ts
constembeddings =hopfield .embedding ("text-embedding-ada-002", 3);constresponse = awaitembeddings .get ({input : ["ready", "set", "hop"] });constembeddingLength =response .data .length ;const embeddingLength: 3constthirdEmbeddingLength =response .data [2].embedding .length ;const thirdEmbeddingLength: 1536
ts
constembeddings =hopfield .embedding ("text-embedding-ada-002", 3);constresponse = awaitembeddings .get ({input : ["ready", "set", "hop"] });constembeddingLength =response .data .length ;const embeddingLength: 3constthirdEmbeddingLength =response .data [2].embedding .length ;const thirdEmbeddingLength: 1536