1"use strict";(self.webpackChunkspring_ai_alibaba_website=self.webpackChunkspring_ai_alibaba_website||[]).push([[1202],{6210:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>a,contentTitle:()=>r,default:()=>c,frontMatter:()=>o,metadata:()=>i,toc:()=>l});const i=JSON.parse('{"id":"rag/embeddings","title":"embeddings","description":"[[EmbeddingModel]]","source":"@site/integration/rag/embeddings.md","sourceDirName":"rag","slug":"/rag/embeddings","permalink":"/integration/rag/embeddings","draft":false,"unlisted":false,"editUrl":"https://github.com/spring-ai-alibaba/website/tree/main/integration/rag/embeddings.md","tags":[],"version":"current","lastUpdatedBy":"Ken Liu","lastUpdatedAt":1765761850000,"frontMatter":{},"sidebar":"integrationSidebar","previous":{"title":"Document Parser \u4f7f\u7528\u6307\u5357","permalink":"/integration/rag/document-parsers"},"next":{"title":"DashScope Embeddings","permalink":"/integration/rag/embeddings/dashscope-embeddings"}}');var s=t(74848),d=t(28453);const o={},r="Embeddings Model API",a={},l=[{value:"API Overview",id:"api-overview",level:2},{value:"EmbeddingModel",id:"embeddingmodel",level:3},{value:"EmbeddingRequest",id:"embeddingrequest",level:4},{value:"EmbeddingResponse",id:"embeddingresponse",level:4},{value:"Embedding",id:"embedding",level:4},{value:"Available Implementations [[available-implementations]]",id:"available-implementations-available-implementations",level:2}];function m(e){const n={a:"a",code:"code",em:"em",h1:"h1",h2:"h2",h3:"h3",h4:"h4",header:"header",li:"li",p:"p",pre:"pre",ul:"ul",...(0,d.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(n.p,{children:"[[EmbeddingModel]]"}),"\n",(0,s.jsx)(n.header,{children:(0,s.jsx)(n.h1,{id:"embeddings-model-api",children:"Embeddings Model API"})}),"\n",(0,s.jsx)(n.p,{children:"Embeddings are numerical representations of text, images, or videos that capture relationships between inputs."}),"\n",(0,s.jsx)(n.p,{children:"Embeddings work by converting text, image, and video into arrays of floating point numbers, called vectors.\nThese vectors are designed to capture the meaning of the text, images, and videos.\nThe length of the embedding array is called the vector's dimensionality."}),"\n",(0,s.jsx)(n.p,{children:"By calculating the numerical distance between the vector representations of two pieces of text, an application can determine the similarity between the objects used to generate the embedding vectors."}),"\n",(0,s.jsxs)(n.p,{children:["The ",(0,s.jsx)(n.code,{children:"EmbeddingModel"})," interface is designed for straightforward integration with embedding models in AI and machine learning.\nIts primary function is to convert text into numerical vectors, commonly referred to as embeddings.\nThese embeddings are crucial for various tasks such as semantic analysis and text classification."]}),"\n",(0,s.jsx)(n.p,{children:"The design of the EmbeddingModel interface centers around two primary goals:"}),"\n",(0,s.jsxs)(n.ul,{children:["\n",(0,s.jsxs)(n.li,{children:["\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.em,{children:"Portability"}),": This interface ensures easy adaptability across various embedding models.\nIt allows developers to switch between different embedding techniques or models with minimal code changes.\nThis design aligns with Spring's philosophy of modularity and interchangeability."]}),"\n"]}),"\n",(0,s.jsxs)(n.li,{children:["\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.em,{children:"Simplicity"}),": EmbeddingModel simplifies the process of converting text to embeddings.\nBy providing straightforward methods like ",(0,s.jsx)(n.code,{children:"embed(String text)"})," and ",(0,s.jsx)(n.code,{children:"embed(Document document)"}),", it takes the complexity out of dealing with raw text data and embedding algorithms. This design choice makes it easier for developers, especially those new to AI, to utilize embeddings in their applications without delving deep into the underlying mechanics."]}),"\n"]}),"\n"]}),"\n",(0,s.jsx)(n.h2,{id:"api-overview",children:"API Overview"}),"\n",(0,s.jsxs)(n.p,{children:["The Embedding Model API is built on top of the generic ",(0,s.jsx)(n.a,{href:"https://github.com/spring-projects/spring-ai/tree/main/spring-ai-model/src/main/java/org/springframework/ai/model%5BSpring",children:"https://github.com/spring-projects/spring-ai/tree/main/spring-ai-model/src/main/java/org/springframework/ai/model[Spring"}
1)," AI Model API], which is a part of the Spring AI library.\nAs such, the EmbeddingModel interface extends the ",(0,s.jsx)(n.code,{children:"Model"})," interface, which provides a standard set of methods for interacting with AI models. The ",(0,s.jsx)(n.code,{children:"EmbeddingRequest"})," and ",(0,s.jsx)(n.code,{children:"EmbeddingResponse"})," classes extend from the ",(0,s.jsx)(n.code,{children:"ModelRequest"})," and ",(0,s.jsx)(n.code,{children:"ModelResponse"})," are used to encapsulate the input and output of the embedding models, respectively."]}),"\n",(0,s.jsx)(n.p,{children:"The Embedding API in turn is used by higher-level components to implement Embedding Models for specific embedding models, such as OpenAI, Titan, Azure OpenAI, Ollie, and others."}),"\n",(0,s.jsx)(n.p,{children:"Following diagram illustrates the Embedding API and its relationship with the Spring AI Model API and the Embedding Models:"}),"\n",(0,s.jsxs)(n.p,{children:["image",":embeddings-api",".jpg[title=Embeddings API,align=center,width=900]"]}),"\n",(0,s.jsx)(n.h3,{id:"embeddingmodel",children:"EmbeddingModel"}),"\n",(0,s.jsxs)(n.p,{children:["This section provides a guide to the ",(0,s.jsx)(n.code,{children:"EmbeddingModel"})," interface and associated classes."]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-java",children:'public interface EmbeddingModel extends Model<EmbeddingRequest, EmbeddingResponse> {\n\n\t@Override\n\tEmbeddingResponse call(EmbeddingRequest request);\n\n\n\t/**\n\t * Embeds the given document\'s content into a vector.\n\t * @param document the document to embed.\n\t * @return the embedded vector.\n\t */\n\tfloat[] embed(Document document);\n\n\t/**\n\t * Embeds the given text into a vector.\n\t * @param text the text to embed.\n\t * @return the embedded vector.\n\t */\n\tdefault float[] embed(String text) {\n\t\tAssert.notNull(text, "Text must not be null");\n\t\treturn this.embed(List.of(text)).iterator().next();\n\t}\n\n\t/**\n\t * Embeds a batch of texts into vectors.\n\t * @param texts list of texts to embed.\n\t * @return list of list of embedded vectors.\n\t */\n\tdefault List<float[]> embed(List<String> texts) {\n\t\tAssert.notNull(texts, "Texts must not be null");\n\t\treturn this.call(new EmbeddingRequest(texts, EmbeddingOptions.EMPTY))\n\t\t\t.getResults()\n\t\t\t.stream()\n\t\t\t.map(Embedding::getOutput)\n\t\t\t.toList();\n\t}\n\n\t/**\n\t * Embeds a batch of texts into vectors and returns the {@link EmbeddingResponse}.\n\t * @param texts list of texts to embed.\n\t * @return the embedding response.\n\t */\n\tdefault EmbeddingResponse embedForResponse(List<String> texts) {\n\t\tAssert.notNull(texts, "Texts must not be null");\n\t\treturn this.call(new EmbeddingRequest(texts, EmbeddingOptions.EMPTY));\n\t}\n\n\t/**\n\t * @return the number of dimensions of the embedded vectors. It is generative\n\t * specific.\n\t */\n\tdefault int dimensions() {\n\t\treturn embed("Test String").size();\n\t}\n\n}\n'})}),"\n",(0,s.jsxs)(n.p,{children:["The embed methods offer various options for converting text into embeddings, accommodating single str
1ings, structured ",(0,s.jsx)(n.code,{children:"Document"})," objects, or batches of text."]}),"\n",(0,s.jsxs)(n.p,{children:["Multiple shortcut methods are provided for embedding text, including the ",(0,s.jsx)(n.code,{children:"embed(String text)"})," method, which takes a single string and returns the corresponding embedding vector.\nAll shortcuts are implemented around the ",(0,s.jsx)(n.code,{children:"call"})," method, which is the primary method for invoking the embedding model."]}),"\n",(0,s.jsx)(n.p,{children:"Typically the embedding returns a lists of floats, representing the embeddings in a numerical vector format."}),"\n",(0,s.jsxs)(n.p,{children:["The ",(0,s.jsx)(n.code,{children:"embedForResponse"})," method provides a more comprehensive output, potentially including additional information about the embeddings."]}),"\n",(0,s.jsx)(n.p,{children:"The dimensions method is a handy tool for developers to quickly ascertain the size of the embedding vectors, which is important for understanding the embedding space and for subsequent processing steps."}),"\n",(0,s.jsx)(n.h4,{id:"embeddingrequest",children:"EmbeddingRequest"}),"\n",(0,s.jsxs)(n.p,{children:["The ",(0,s.jsx)(n.code,{children:"EmbeddingRequest"})," is a ",(0,s.jsx)(n.code,{children:"ModelRequest"})," that takes a list of text objects and optional embedding request options.\nThe following listing shows a truncated version of the EmbeddingRequest class, excluding constructors and other utility methods:"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-java",children:"public class EmbeddingRequest implements ModelRequest<List<String>> {\n\tprivate final List<String> inputs;\n\tprivate final EmbeddingOptions options;\n\t// other methods omitted\n}\n\n"})}),"\n",(0,s.jsx)(n.h4,{id:"embeddingresponse",children:"EmbeddingResponse"}),"\n",(0,s.jsxs)(n.p,{children:["The structure of the ",(0,s.jsx)(n.code,{children:"EmbeddingResponse"})," class is as follows:"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-java",children:"public class EmbeddingResponse implements ModelResponse<Embedding> {\n\n\tprivate List<Embedding> embeddings;\n\tprivate EmbeddingResponseMetadata metadata = new EmbeddingResponseMetadata();\n\t// other methods omitted\n}\n\n"})}),"\n",(0,s.jsxs)(n.p,{children:["The ",(0,s.jsx)(n.code,{children:"EmbeddingResponse"})," class holds the AI Model's output, with each ",(0,s.jsx)(n.code,{children:"Embedding"})," instance containing the result vector data from a single text input."]}),"\n",(0,s.jsxs)(n.p,{children:["The ",(0,s.jsx)(n.code,{children:"EmbeddingResponse"})," class also carries a ",(0,s.jsx)(n.code,{children:"EmbeddingResponseMetadata"})," metadata about the AI Model's response."]}),"\n",(0,s.jsx)(n.h4,{id:"embedding",children:"Embedding"}),"\n",(0,s.jsxs)(n.p,{children:["The ",(0,s.jsx)(n.code,{children:"Embedding"})," represents a single embedding vector."]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-java",children:"public class Embedding implements ModelResult<float[]> {\n\tprivate float[] embedding;\n\tprivate Integer index;\n\tprivate EmbeddingResultMetadata metadata;\n\t// other methods omitted\n}\n"})}),"\n",(0,s.jsx)(n.h2,{id:"available-implementations-available-implementations",children:"Available Implementations [[available-implementations]]"}),"\n",(0,s.jsxs)(n.p,{children:["Internally the various ",(0,s.jsx)(n.code,{children:"EmbeddingModel"})," implementations use different low-level libraries and APIs to perform the embedding tasks. The following are some of the available implementations of the ",(0,s.jsx)(n.code,{children:"EmbeddingModel"})," implementations:"]})]})}function c(e={}){const{wrapper:n}={...(0,d.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(m,{...e})}):m(e)}},28453:(e,n,t)=>{t.d(n,{R:()=>o,x:()=>r});var i=t(96540);const s={},d=i.createContext(s);function o(e){const n=i.useContext(d);return i.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function r(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(s):e.components||s:o(e.components),i.createElement(d.Provider,{value:n},e.children)}}}]);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.