Skip to content

Commit 77cc0c7

Browse files
authored
Merge pull request #2159 from Danielle9897/RDoc-3468-extend-querying-pre-made-embeddings
RDoc-3468 Get embeddings generation tasks details + exception info
2 parents 81419e7 + 32c59ed commit 77cc0c7

File tree

4 files changed

+477
-450
lines changed

4 files changed

+477
-450
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
import Admonition from '@theme/Admonition';
2+
import Tabs from '@theme/Tabs';
3+
import TabItem from '@theme/TabItem';
4+
import CodeBlock from '@theme/CodeBlock';
5+
6+
<Admonition type="note" title="">
7+
8+
* RavenDB can serve as a vector database, see [Why choose RavenDB as your vector database](../../../ai-integration/vector-search/ravendb-as-vector-database.mdx#why-choose-ravendb-as-your-vector-database).
9+
10+
* Vector search can be performed on:
11+
* Raw text stored in your documents.
12+
* Pre-made embeddings that you created yourself and stored using these [Data types](../../../ai-integration/vector-search/data-types-for-vector-search.mdx#numerical-data).
13+
* Pre-made embeddings that are automatically generated from your document content by RavenDB's
14+
**embeddings generation tasks** using external service providers, as explained below.
15+
* In this article:
16+
* [Embeddings generation - overview](../../../ai-integration/generating-embeddings/overview.mdx#embeddings-generation---overview)
17+
* [Embeddings generation - process flow](../../../ai-integration/generating-embeddings/overview.mdx#embeddings-generation---process-flow)
18+
* [Supported providers](../../../ai-integration/generating-embeddings/overview.mdx#supported-providers)
19+
* [Creating an embeddings generation task](../../../ai-integration/generating-embeddings/overview.mdx#creating-an-embeddings-generation-task)
20+
* [Monitoring the tasks](../../../ai-integration/generating-embeddings/overview.mdx#monitoring-the-tasks)
21+
* [Get embeddings generation task details](../../../ai-integration/generating-embeddings/overview.mdx#get-embeddings-generation-task-details)
22+
23+
</Admonition>
24+
25+
## Embeddings generation - overview
26+
27+
<Admonition type="note" title="">
28+
29+
#### Embeddings generation - process flow
30+
31+
* **Define an Embeddings Generation Task**:
32+
Specify a [connection string](../../../ai-integration/connection-strings/connection-strings-overview.mdx) that defines the AI provider and model for generating embeddings.
33+
Define the source content - what parts of the documents will be used to create the embeddings.
34+
35+
* **Source content is processed**:
36+
1. The task extracts the specified content from the documents.
37+
2. If a processing script is defined, it transforms the content before further processing.
38+
3. The text is split according to the defined chunking method; a separate embedding will be created for each chunk.
39+
4. Before contacting the provider, RavenDB checks the [embeddings cache](../../../ai-integration/generating-embeddings/embedding-collections.mdx#the-embeddings-cache-collection)
40+
to determine whether an embedding already exists for the given content from that provider.
41+
5. If a matching embedding is found, it is reused, avoiding unnecessary requests.
42+
If no cached embedding is found, the transformed and chunked content is sent to the configured AI provider.
43+
44+
* **Embeddings are generated by the AI provider**:
45+
The provider generates embeddings and sends them back to RavenDB.
46+
If quantization was defined in the task, RavenDB applies it to the embeddings before storing them.
47+
48+
* **Embeddings are stored in your database**:
49+
* Each embedding is stored as an attachment in a [dedicated collection](../../../ai-integration/generating-embeddings/embedding-collections.mdx#the-embeddings-collection).
50+
* RavenDB maintains an [embeddings cache](../../../ai-integration/generating-embeddings/embedding-collections.mdx#the-embeddings-cache-collection),
51+
allowing reuse of embeddings for the same source content and reducing provider calls.
52+
Cached embeddings expire after a configurable duration.
53+
54+
* **Perform vector search:**
55+
Once the embeddings are stored, you can perform vector searches on your document content by:
56+
* Running a [dynamic query](../../../ai-integration/vector-search/vector-search-using-dynamic-query.mdx#querying-pre-made-embeddings-generated-by-tasks), which automatically creates an auto-index for the search.
57+
* Defining a [static index](../../../ai-integration/vector-search/vector-search-using-static-index.mdx#indexing-pre-made-text-embeddings) to store and query embeddings efficiently.
58+
59+
The query search term is split into chunks, and each chunk is looked up in the cache.
60+
If not found, RavenDB requests an embedding from the provider and caches it.
61+
The embedding (cached or newly created) is then used to compare against stored vectors.
62+
63+
* **Continuous processing**:
64+
* Embeddings generation tasks are [Ongoing Tasks](../../../studio/database/tasks/ongoing-tasks/general-info.mdx) that process documents as they change.
65+
Before contacting the provider after a document change, the task first checks the cache to see if a matching embedding already exists, avoiding unnecessary requests.
66+
* The requests to generate embeddings from the source text are sent to the provider in batches.
67+
The batch size is configurable, see the [Ai.Embeddings.MaxBatchSize](../../../server/configuration/ai-integration-configuration.mdx#aiembeddingsmaxbatchsize) configuration key.
68+
* A failed embeddings generation task will retry after the duration set in the
69+
[Ai.Embeddings.MaxFallbackTimeInSec](../../../server/configuration/ai-integration-configuration.mdx#aiembeddingsmaxfallbacktimeinsec) configuration key.
70+
71+
</Admonition>
72+
73+
<Admonition type="note" title="">
74+
75+
#### Supported providers
76+
77+
* The following service providers are supported for auto-generating embeddings using tasks:
78+
79+
* [OpenAI & OpenAI-compatible providers](../../../ai-integration/connection-strings/open-ai.mdx)
80+
* [Azure Open AI](../../../ai-integration/connection-strings/azure-open-ai.mdx)
81+
* [Google AI](../../../ai-integration/connection-strings/google-ai.mdx)
82+
* [Hugging Face](../../../ai-integration/connection-strings/hugging-face.mdx)
83+
* [Ollama](../../../ai-integration/connection-strings/ollama.mdx)
84+
* [Mistral AI](../../../ai-integration/connection-strings/mistral-ai.mdx)
85+
* [bge-micro-v2](../../../ai-integration/connection-strings/embedded.mdx) (a local embedded model within RavenDB)
86+
87+
</Admonition>
88+
89+
![flow chart](../assets/embeddings-generation-task-flow.png)
90+
91+
![flow chart](../assets/vector-search-flow.png)
92+
93+
## Creating an embeddings generation task
94+
95+
* An embeddings generation tasks can be created from:
96+
* The **AI Tasks view in the Studio**, where you can create, edit, and delete tasks. Learn more in [AI Tasks - list view](../../../ai-integration/ai-tasks-list-view.mdx).
97+
* The **Client API** - see [Configuring an embeddings generation task - from the Client API](../../../ai-integration/generating-embeddings/embeddings-generation-task.mdx#configuring-an-embeddings-generation-task---from-the-client-api)
98+
* From the Studio:
99+
100+
![Add ai task 1](../assets/add-ai-task-1.png)
101+
102+
1. Go to the **AI Hub** menu.
103+
2. Open the **AI Tasks** view.
104+
3. Click **Add AI Task** to add a new task.
105+
106+
![Add ai task 2](../assets/add-ai-task-2.png)
107+
108+
* See the complete details of the task configuration in the [Embeddings generation task](../../../ai-integration/generating-embeddings/embeddings-generation-task.mdx) article.
109+
110+
## Monitoring the tasks
111+
112+
* The status and state of each embeddings generation task are visible in the [AI Tasks - list view](../../../ai-integration/ai-tasks-list-view.mdx).
113+
114+
* Task performance and activity over time can be analyzed in the _AI Tasks Stats_ view,
115+
where you can track processing duration, batch sizes, and overall progress.
116+
Learn more about the functionality of the stats view in the [Ongoing Tasks Stats](../../../studio/database/stats/ongoing-tasks-stats/overview.mdx) article.
117+
118+
* The number of embeddings generation tasks across all databases can also be monitored using [SNMP](../../../server/administration/snmp/snmp-overview.mdx).
119+
The following SNMP OIDs provide relevant metrics:
120+
* [5.1.11.25](../../../server/administration/snmp/snmp-overview.mdx#511125) – Total number of enabled embeddings generation tasks.
121+
* [5.1.11.26](../../../server/administration/snmp/snmp-overview.mdx#511126) – Total number of active embeddings generation tasks.
122+
123+
## Get embeddings generation task details
124+
125+
* Besides viewing the list of tasks in the [AI Tasks - list view](../../../ai-integration/ai-tasks-list-view.mdx) in the Studio,
126+
you can also retrieve embeddings generation task details programmatically.
127+
128+
* This is useful when issuing a vector search query that references an embeddings generation task,
129+
where it's important to verify that the task exists beforehand. For example:
130+
* when [Querying pre-made embeddings generated by tasks](../../../ai-integration/vector-search/vector-search-using-dynamic-query#querying-pre-made-embeddings-generated-by-tasks)
131+
* or when [Indexing numerical data and querying using text input](../../../ai-integration/vector-search/vector-search-using-static-index#indexing-numerical-data-and-querying-using-text-input)
132+
133+
* There are two ways to check if an embeddings generation task exists:
134+
* Using `GetOngoingTaskInfoOperation`.
135+
* Accessing the full list of embeddings generation tasks from the database record.
136+
137+
138+
<Tabs groupId='languageSyntax'>
139+
<TabItem value="Get_task_info_via_operataion" label="Get_task_info_via_operataion">
140+
```csharp
141+
// Define the get task operation, pass the task NAME
142+
var getOngoingTaskOp =
143+
new GetOngoingTaskInfoOperation("theEmbeddingsGenerationTaskName", OngoingTaskType.EmbeddingsGeneration);
144+
145+
// Execute the operation by by passing it to Maintenance.Send
146+
// Explicitly cast the result to the "EmbeddingsGeneration" type
147+
var task = (EmbeddingsGeneration)store.Maintenance.Send(getOngoingTaskOp);
148+
149+
// Verify the task exists
150+
if (task != null)
151+
{
152+
// Access any of the task details
153+
var taskStatus = task.TaskState;
154+
155+
// Access the task identifier
156+
var taskIdentifier = task.Configuration.Identifier;
157+
}
158+
```
159+
</TabItem>
160+
<TabItem value="Get_task_info_via_database_record" label="Get_task_info_via_database_record">
161+
```csharp
162+
// Define the get database record operation, pass your database name
163+
var getDatabaseRecordOp = new GetDatabaseRecordOperation("yourDatabaseName");
164+
165+
// Execute the operation by passing it to Maintenance.Send
166+
var dbRecord = store.Maintenance.Server.Send(getDatabaseRecordOp);
167+
168+
// Access the list of embeddings generation tasks
169+
var tasks = dbRecord.EmbeddingsGenerations;
170+
171+
if (tasks.Count > 0)
172+
{
173+
// Access the first task
174+
var task = tasks[0];
175+
176+
// Access any of the task details
177+
var isTaskDisabled = task.Disabled;
178+
179+
// Access the task identifier
180+
var taskIdentifier = task.Identifier;
181+
}
182+
```
183+
</TabItem>
184+
</Tabs>

0 commit comments

Comments
 (0)