-
Notifications
You must be signed in to change notification settings - Fork 150
Move Vector Embeddings Guide from HANA to Databases #2507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+113
−83
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
dd5dbf5
Move vector embeddings out of HANA section
MattSchur 80e5930
Remove vector embeddings section from HANA guide
MattSchur 98623ff
Add Vector Embeddings link to database menu
MattSchur 2742cce
Update link for vector embeddings documentation
MattSchur d98f9a5
revise
MattSchur a6194ba
Apply suggestions from code review
MattSchur 6c3fdb0
Apply suggestions from code review
MattSchur 3036194
vector_embedding
MattSchur 7581070
Move AI SDK tip
MattSchur 9f69d2e
Apply suggestions from code review
MattSchur 691fafb
Apply suggestion from @MattSchur
MattSchur f64e9b1
cleanup
agoerler 1890886
learn-more
MattSchur 17897a7
Apply suggestion from @MattSchur
MattSchur 5c75601
Apply suggestion from @MattSchur
MattSchur 94ac411
cosmetics
smahati ad55673
Merge branch 'main' into vector-embeddings
smahati b55c9d0
fix broken link
smahati c2ffe62
Merge branch 'main' into vector-embeddings
smahati 0a8513b
fix links
smahati File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| --- | ||
| label: Vector Embeddings | ||
| --- | ||
| # Vector Embeddings | ||
|
|
||
| Vector embeddings convert unstructured content (text, images, and so on) into numeric vectors that encode semantics (meaning). Comparing these vectors enables semantic search, recommendations, and enhanced generative AI features in your CAP application. For example retrieving related records, ranking results by relevance, or augmenting prompts for LLMs. | ||
|
|
||
| ## Choose an Embedding Model | ||
|
|
||
| Choose an embedding model that fits your use case and data (for example English or multilingual text). The model determines the number of dimensions of the resulting output vector. Check the documentation of the respective embedding model for details. | ||
|
|
||
| Use the [SAP Generative AI Hub](https://www.sap.com/products/artificial-intelligence/generative-ai-hub.html) for unified consumption of embedding models and LLMs across different vendors and open-source models. Check for available models on the [SAP AI Launchpad](https://help.sap.com/docs/ai-launchpad/sap-ai-launchpad-user-guide/models-and-scenarios-in-generative-ai-hub-fef463b24bff4f44a33e98bb1e4f3148#models). | ||
|
|
||
| ## Add Embeddings to Your CDS Model | ||
| Use the built-in CDL [Vector type](../../cds/types) in your CDS model to store embeddings. Set the vector dimensions to match the embedding model (for example, 768 for *SAP_GXY.20250407*). | ||
|
|
||
| ```cds | ||
| extend Incidents with { | ||
| embedding : Vector(768); | ||
| } | ||
| ``` | ||
|
|
||
| ## Generate Embeddings | ||
| Use an embedding model to convert your data (for example, incident titles and summaries) into vectors. | ||
|
|
||
| :::warning Evolve embeddings with your model | ||
| Store embeddings when you create or update your data. Regenerate embeddings if you change your embedding model. | ||
| ::: | ||
|
|
||
| ### Generate Embeddings on the Database | ||
|
|
||
| To generate vector embeddings on write in SAP HANA, you can use the [vector_embedding](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-sql-reference-guide/vector-embedding-function-vector) function as calculated element [on-write](../../cds/cdl#on-write) with embedding models from [SAP HANA NLP](https://help.sap.com/docs/hana-cloud-database/sap-hana-cloud-sap-hana-database-vector-engine-guide/creating-text-embeddings-with-nlp-51eb170d038d4099a9bbb85c08fda888) or a configured remote source from SAP AI Core: | ||
|
|
||
| ```cds | ||
| extend Incidents with { | ||
| @cds.api.ignore | ||
| embedding : Vector(768) = vector_embedding( | ||
| 'Title: ' || title || ', Summary: ' || summary, | ||
| 'DOCUMENT', 'SAP_GXY.20250407' | ||
| ) stored; | ||
| } | ||
| ``` | ||
|
|
||
|
MattSchur marked this conversation as resolved.
|
||
| :::tip Prefer calculated elements for vector embeddings | ||
| If the database calculates vector embeddings on write it automatically regenerates the embedding if the input data changes. | ||
| ::: | ||
|
|
||
|
MattSchur marked this conversation as resolved.
|
||
| ::: info Local Testing with H2 and SQLite | ||
| On H2 and SQLite the `CQL.vectorEmbedding` function is emulated to support local testing. | ||
| ::: | ||
|
|
||
| > [!warning] Java only and <Beta/> | ||
| > The `vector_embedding` function is currently in beta and only supported by the CAP Java runtime. | ||
|
|
||
| [Learn more about Vector Embeddings in CAP Java](../../java/cds-data#vector-embeddings) {.learn-more} | ||
|
|
||
| ### Generate Embeddings Programmatically | ||
|
|
||
| Alternatively, you can compute vector embeddings in your application layer using the [SAP Cloud SDK for AI](https://sap.github.io/ai-sdk/) to call SAP AI Core services for generating embeddings. | ||
|
MattSchur marked this conversation as resolved.
|
||
|
|
||
| :::details Example using SAP Cloud SDK for AI | ||
| ```Java | ||
| var aiClient = OpenAiClient.forModel(OpenAiModel.TEXT_EMBEDDING_3_SMALL); | ||
| var response = aiClient.embedding( | ||
| new OpenAiEmbeddingRequest(List.of(book.getDescription()))); | ||
| book.setEmbedding(CdsVector.of(response.getEmbeddingVectors().get(0))); | ||
| ``` | ||
| ::: | ||
|
|
||
|
MattSchur marked this conversation as resolved.
|
||
| :::tip Use SAP Cloud SDK for AI | ||
| Use the [SAP Cloud SDK for AI](https://sap.github.io/ai-sdk/) for unified access to embedding models and large language models (LLMs) from [SAP AI Core](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/what-is-sap-ai-core). | ||
| ::: | ||
|
|
||
| Learn more about the [SAP Cloud SDK for AI (Java)](https://sap.github.io/ai-sdk/docs/java/getting-started) or the [SAP Cloud SDK for AI (JavaScript)](https://sap.github.io/ai-sdk/docs/js/getting-started) {.learn-more} | ||
|
|
||
| ## Query for Similarity | ||
| At runtime, use vector functions to search for similar items. In an example Retrieval-Augmented Generation (RAG) scenario, use `CQL.cosineSimilarity` to enhance the context of a user query for the LLM. First, compute the vector embedding of the user query and use it to find related incidents. | ||
|
|
||
| ::: code-group | ||
| ```Java [Java] | ||
| // Compute embedding for user question | ||
| var query = CQL.val( | ||
| "Any incidents with solar inverters this month? How were they resolved?"); | ||
| var embedding = CQL.vectorEmbedding(query, TextType.QUERY, "SAP_GXY.20250407"); | ||
|
|
||
| // Compute similarity between user question and incident embeddings | ||
| var similarity = CQL.cosineSimilarity(CQL.get(Incidents.EMBEDDING), embedding); | ||
|
|
||
| // Find Incidents related to user question ordered by relevance | ||
| Select.from(INCIDENTS) | ||
| .columns(i -> similarity.times(100).as("relevance"), | ||
| i -> i.ID(), i -> i.title(), i -> i.summary(), i -> i.date()) | ||
| .where(i -> similarity.gt(0.75)) | ||
| .orderBy(i -> i.get("relevance").desc()); | ||
| ``` | ||
|
|
||
| ```js [Node.js] | ||
| const response = await new AzureOpenAiEmbeddingClient( | ||
| 'text-embedding-3-small' | ||
| ).run({ | ||
| input: 'Any incidents with solar inverters this month? How were they resolved?' | ||
| }); | ||
|
|
||
| const questionEmbedding = response.getEmbedding(); | ||
| let similarIncidents = await SELECT.from('Incidents') | ||
| .where`cosine_similarity(embedding, to_real_vector(${questionEmbedding})) > 0.75`; | ||
| ``` | ||
| ::: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.