Skip to content

Commit 94b216f

Browse files
MattSchuragoerlersmahati
authored
Move Vector Embeddings Guide from HANA to Databases (#2507)
Co-authored-by: Adrian Görler <adrian.goerler@sap.com> Co-authored-by: Mahati Shankar <93712176+smahati@users.noreply.github.com>
1 parent 50103b1 commit 94b216f

5 files changed

Lines changed: 113 additions & 83 deletions

File tree

guides/databases/_menu.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
## [ PostgreSQL ](postgres.md)
1111
# [ Schema Evolution ](schema-evolution.md)
1212
# [ Performance Guide ](performance.md)
13+
# [ Vector Embeddings ](vector-embeddings.md)

guides/databases/hana.md

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -261,86 +261,6 @@ See the [Deploying to Cloud](../deploy/index.md) guide for information about how
261261

262262
The HANA Service provides dedicated support for native SAP HANA features as follows.
263263

264-
### Vector Embeddings
265-
266-
Vector embeddings let you add semantic search, recommendations, and generative AI features to your CAP application. Embeddings are numeric arrays that represent the meaning of unstructured data (text, images, etc.), making it possible to compare and search for items that are semantically related to each other or a user query.
267-
268-
#### Choose an Embedding Model
269-
270-
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.
271-
272-
Use the [SAP Generative AI Hub](https://community.sap.com/t5/technology-blogs-by-sap/how-sap-s-generative-ai-hub-facilitates-embedded-trustworthy-and-reliable/ba-p/13596153) 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).
273-
274-
#### Add Embeddings to Your CDS Model
275-
Use the `cds.Vector` type in your CDS model to store embeddings on SAP HANA Cloud. Set the dimension to match your embedding model (for example, 1536 embedding dimensions for OpenAI *text-embedding-3-small*).
276-
277-
```cds
278-
entity Books : cuid {
279-
title : String(111);
280-
description : LargeString;
281-
embedding : Vector(1536); // adjust dimensions to embedding model
282-
}
283-
```
284-
285-
#### Generate Embeddings
286-
Use an embedding model to convert your data (for example, book descriptions) into vectors. The [SAP Cloud SDK for AI](https://sap.github.io/ai-sdk/) makes it easy to call SAP AI Core services to generate these embeddings.
287-
288-
:::details Example using SAP Cloud SDK for AI
289-
```Java
290-
var aiClient = OpenAiClient.forModel(OpenAiModel.TEXT_EMBEDDING_3_SMALL);
291-
var response = aiClient.embedding(
292-
new OpenAiEmbeddingRequest(List.of(book.getDescription())));
293-
book.setEmbedding(CdsVector.of(response.getEmbeddingVectors().get(0)));
294-
```
295-
:::
296-
297-
#### Query for Similarity
298-
At runtime, use SAP HANA's built-in vector functions to search for similar items. For example, find books with embeddings similar to a user question:
299-
300-
::: code-group
301-
```Java [Java]
302-
// Compute embedding for user question
303-
var request = new OpenAiEmbeddingRequest(List.of("How to use vector embeddings in CAP?"));
304-
CdsVector userQuestion = CdsVector.of(
305-
aiClient.embedding(request).getEmbeddingVectors().get(0));
306-
307-
// Compute similarity between user question and book embeddings
308-
var similarity = CQL.cosineSimilarity( // computed on SAP HANA
309-
CQL.get(Books.EMBEDDING), userQuestion);
310-
311-
// Find Books related to user question ordered by similarity
312-
hana.run(Select.from(BOOKS).limit(10)
313-
.columns(b -> b.ID(), b -> b.title(), b -> similarity.as("similarity"))
314-
.orderBy(b -> b.get("similarity").desc())
315-
);
316-
```
317-
318-
```js [Node.js]
319-
const response = await new AzureOpenAiEmbeddingClient(
320-
'text-embedding-3-small'
321-
).run({
322-
input: 'How to use vector embeddings in CAP?'
323-
});
324-
325-
const questionEmbedding = response.getEmbedding();
326-
let similarBooks = await SELECT.from('Books')
327-
.where`cosine_similarity(embedding, to_real_vector(${questionEmbedding})) > 0.9`;
328-
```
329-
:::
330-
331-
:::tip Evolve embeddings with your model
332-
Store embeddings when you create or update your data. Regenerate embeddings if you change your embedding model.
333-
:::
334-
335-
:::tip Use SAP Cloud SDK for AI
336-
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).
337-
:::
338-
339-
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}
340-
341-
[Learn more about Vector Embeddings in CAP Java](../../java/cds-data#vector-embeddings) {.learn-more}
342-
343-
344264
### Geospatial Functions
345265

346266
CDS supports the special syntax for SAP HANA geospatial functions:
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
label: Vector Embeddings
3+
---
4+
# Vector Embeddings
5+
6+
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.
7+
8+
## Choose an Embedding Model
9+
10+
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.
11+
12+
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).
13+
14+
## Add Embeddings to Your CDS Model
15+
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*).
16+
17+
```cds
18+
extend Incidents with {
19+
embedding : Vector(768);
20+
}
21+
```
22+
23+
## Generate Embeddings
24+
Use an embedding model to convert your data (for example, incident titles and summaries) into vectors.
25+
26+
:::warning Evolve embeddings with your model
27+
Store embeddings when you create or update your data. Regenerate embeddings if you change your embedding model.
28+
:::
29+
30+
### Generate Embeddings on the Database
31+
32+
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:
33+
34+
```cds
35+
extend Incidents with {
36+
@cds.api.ignore
37+
embedding : Vector(768) = vector_embedding(
38+
'Title: ' || title || ', Summary: ' || summary,
39+
'DOCUMENT', 'SAP_GXY.20250407'
40+
) stored;
41+
}
42+
```
43+
44+
:::tip Prefer calculated elements for vector embeddings
45+
If the database calculates vector embeddings on write it automatically regenerates the embedding if the input data changes.
46+
:::
47+
48+
::: info Local Testing with H2 and SQLite
49+
On H2 and SQLite the `CQL.vectorEmbedding` function is emulated to support local testing.
50+
:::
51+
52+
> [!warning] Java only and <Beta/>
53+
> The `vector_embedding` function is currently in beta and only supported by the CAP Java runtime.
54+
55+
[Learn more about Vector Embeddings in CAP Java](../../java/cds-data#vector-embeddings) {.learn-more}
56+
57+
### Generate Embeddings Programmatically
58+
59+
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.
60+
61+
:::details Example using SAP Cloud SDK for AI
62+
```Java
63+
var aiClient = OpenAiClient.forModel(OpenAiModel.TEXT_EMBEDDING_3_SMALL);
64+
var response = aiClient.embedding(
65+
new OpenAiEmbeddingRequest(List.of(book.getDescription())));
66+
book.setEmbedding(CdsVector.of(response.getEmbeddingVectors().get(0)));
67+
```
68+
:::
69+
70+
:::tip Use SAP Cloud SDK for AI
71+
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).
72+
:::
73+
74+
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}
75+
76+
## Query for Similarity
77+
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.
78+
79+
::: code-group
80+
```Java [Java]
81+
// Compute embedding for user question
82+
var query = CQL.val(
83+
"Any incidents with solar inverters this month? How were they resolved?");
84+
var embedding = CQL.vectorEmbedding(query, TextType.QUERY, "SAP_GXY.20250407");
85+
86+
// Compute similarity between user question and incident embeddings
87+
var similarity = CQL.cosineSimilarity(CQL.get(Incidents.EMBEDDING), embedding);
88+
89+
// Find Incidents related to user question ordered by relevance
90+
Select.from(INCIDENTS)
91+
.columns(i -> similarity.times(100).as("relevance"),
92+
i -> i.ID(), i -> i.title(), i -> i.summary(), i -> i.date())
93+
.where(i -> similarity.gt(0.75))
94+
.orderBy(i -> i.get("relevance").desc());
95+
```
96+
97+
```js [Node.js]
98+
const response = await new AzureOpenAiEmbeddingClient(
99+
'text-embedding-3-small'
100+
).run({
101+
input: 'Any incidents with solar inverters this month? How were they resolved?'
102+
});
103+
104+
const questionEmbedding = response.getEmbedding();
105+
let similarIncidents = await SELECT.from('Incidents')
106+
.where`cosine_similarity(embedding, to_real_vector(${questionEmbedding})) > 0.75`;
107+
```
108+
:::
109+

java/cds-data.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ Map data can be nested and may contain nested maps and lists, which are serializ
330330

331331
## Vector Embeddings { #vector-embeddings }
332332

333-
In CDS, [vector embeddings](../guides/databases/hana#vector-embeddings) are stored in elements of type [`Vector`](/@external/cds/types).
333+
In CDS [vector embeddings](../guides/databases/vector-embeddings) are stored in elements of type `cds.Vector`:
334334

335335
CAP Java support the vector type on SAP HANA, as well as H2 and SQLite for local testing. On Postgres (beta) support for vectors requires the [pgvector](https://github.com/pgvector/pgvector) extension.
336336

java/working-with-cql/query-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,7 @@ Scalar functions are values that are calculated from other values. This calculat
16431643

16441644
#### Vector Functions
16451645

1646-
Vector functions allow you to compute similarity and distance of [vectors](../cds-data.md#vector-embeddings), as well as [vector embeddings](../../guides/databases/hana.md#vector-embeddings) of text data directly in the database.
1646+
Vector functions allow you to compute similarity and distance of [vectors](../cds-data.md#vector-embeddings), as well as [vector embeddings](../../guides/databases/vector-embeddings) of text data directly in the database.
16471647

16481648
##### Computing Vector Embeddings in SAP HANA <Beta />
16491649

@@ -1682,7 +1682,7 @@ On H2 and SQLite, the `vectorEmbedding` function is emulated. You can also use l
16821682

16831683
##### Computing Vector Similarity and Distance
16841684

1685-
You can use the functions, `CQL.cosineSimilarity`, and `CQL.l2Distance` (Euclidean distance) in queries to compute the similarity and distance of vectors. Distance functions are used in use cases such as finding similar items based on [vector embeddings](../../guides/databases/hana.md#vector-embeddings), for example to improve the response of an LLM to a user query. To use vector embeddings in functions, wrap them using `CQL.vector`:
1685+
You can use the functions, `CQL.cosineSimilarity`, and `CQL.l2Distance` (Euclidean distance) in queries to compute the similarity and distance of vectors. Distance functions are used in use cases such as finding similar items based on [vector embeddings](../../guides/databases/vector-embeddings), for example to improve the response of an LLM to a user query. To use vector embeddings in functions, wrap them using `CQL.vector`:
16861686

16871687
```Java
16881688
CqnVector vec = CQL.vector(embedding);

0 commit comments

Comments
 (0)