You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
342
336
343
-
In CAP Java, vector embeddings are represented by the `CdsVector` type, which allows a unified handling of different vector representations such as `float[]` and `String`:
337
+
In CAP Java, vectors are represented by the `CdsVector` type, which allows a unified handling of different vector representations such as `float[]` and `String`:
344
338
345
339
```Java
346
-
// Vector embedding of text, for example, from SAP GenAI Hub or via LangChain4j
CdsVector v1 =CdsVector.of(embedding); // float[] format
350
-
CdsVector v2 =CdsVector.of("[0.42, 0.73, 0.28, ...]"); // String format
351
-
```
352
-
353
-
You can use the functions, `CQL.cosineSimilarity` or `CQL.l2Distance` (Euclidean distance) in queries to compute the similarity or distance of embeddings in the vector space. To use vector embeddings in functions, wrap them using `CQL.vector`:
In CDS QL queries, elements of type `Vector` are excluded from the select list by default.
349
+
:::
382
350
351
+
CAP Java supports multiple [vector functions](./working-with-cql/query-api.md#vector-functions) that allow you to compute vector embeddings, similarity, and distance directly in the database.
Copy file name to clipboardExpand all lines: java/working-with-cql/query-api.md
+66Lines changed: 66 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1640,6 +1640,72 @@ Scalar functions are values that are calculated from other values. This calculat
1640
1640
1641
1641
See [`Concat`](#string-expressions) StringExpression
1642
1642
1643
+
1644
+
#### VectorFunctions
1645
+
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.
1647
+
1648
+
##### ComputingVectorEmbeddings in SAPHANA<Beta/>
1649
+
1650
+
CAPJava supports 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 via `CQL.vectorEmbedding` to generate vector embeddings from text data directly in SAP HANA.
1651
+
1652
+
To automatically generate vector embeddings on write in the database, you can define a calculated element [on-write](../../cds/cdl#on-write) using the `vector_embedding` function:
1653
+
1654
+
```cds
1655
+
extend Incidents with {
1656
+
@cds.api.ignore
1657
+
embedding :cds.Vector(768) = vector_embedding(
1658
+
'title: '|| title ||', summary: '|| summary,
1659
+
'DOCUMENT', 'SAP_GXY.20250407') stored;
1660
+
}
1661
+
```
1662
+
1663
+
InJava queries, use the `CQL.vectorEmbedding` function to compute vector embeddings:
1664
+
1665
+
```java
1666
+
var userQuery =CQL.val("""
1667
+
Have we seen incidents with solar inverters this month,
1668
+
and how were they resolved?
1669
+
""");
1670
+
var v =CQL.vectorEmbedding(userQuery, TextType.QUERY, "SAP_GXY.20250407");
1671
+
```
1672
+
1673
+
OnH2 and SQLite, the `vectorEmbedding` function is emulated. You can also use local [ONNX](https://onnx.ai) embedding models, which can be added for local testing via [LangChain4j embeddings](https://github.com/langchain4j/langchain4j/tree/main/embeddings):
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`:
1686
+
1687
+
```Java
1688
+
CqnVector vec =CQL.vector(embedding);
1689
+
1690
+
var similarIncidents = db.run(Select.from(INCIDENTS).where(i ->
.columns(i -> i.title(), i -> similarity.times(100).as("similarity"))
1703
+
.where(i -> similarity.gt(0.75))
1704
+
.orderBy(i -> i.get("similarity").desc());
1705
+
1706
+
Result similarIncidents = db.run(query, CdsVector.of(embedding));
1707
+
```
1708
+
1643
1709
#### Case-When-ThenExpressions
1644
1710
1645
1711
Use a case expression to compute a value based on the evaluation of conditions. The following query converts the stock of Books into a textual representation as 'stockLevel':
0 commit comments