Ayende @ Rahienhttps://ayende.com/blog/Ayende @ RahienCopyright (C) Ayende Rahien 2004 - 2021 (c) 202660PropertySphere bot: understanding images<p>In <a href="http://discord.gg/ravendb">Discord </a>or in our <a href="http://github.com/ravendb/ravendb/discussions/">GitHub discussions</a>.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203622-B/propertysphere-bot-understanding-images?Key=889433c1-a0cb-4633-b096-390e184c9159https://ayende.com/blog/203622-B/propertysphere-bot-understanding-images?Key=889433c1-a0cb-4633-b096-390e184c91592025年12月26日 12:00:00 GMTPropertySphere's intelligent Telegram bot<p>In <a href="https://ayende.com/blog/203620-B/propertysphere-sample-application?key=2ab9814a35aa48ee86f3f3f9b1accd57">the previous post</a>, I introduced the <a href="https://github.com/ayende/samples.properties">PropertySphere</a> sample application (you can also <a href="https://www.youtube.com/watch?v=XOdXDNIGzxE">watch the video introducing it here</a>). In this post, I want to go over how we build a Telegram bot for this application, so Renters can communicate with the application, check their status, raise issues, and even pay their bills.</p><p>I’m using Telegram here because the process of creating a new bot is trivial, the API is really fun to work with, and it takes very little effort.</p><blockquote><p>Compare that to something like WhatsApp, where just the <em>process</em> for creating a bot is a PITA. </p></blockquote><p>Without further ado, let’s look at what the Telegram bot looks like:</p><p><img src="/blog/Images/ykeP1CWNETKtFlzQteq9ow.png"/></p><p>There are a bunch of interesting things that you can see in the screenshot. We communicate with the bot on the other end using natural text. There aren't a lot of screens / options that you have to go through, it is just natural mannerism.</p><p>The process is pretty streamlined from the perspective of the user. What does that look like from the implementation perspective? A lot of the time, that kind of interface involves… <em>big </em>amount of complexity in the backend.</p><p>Here is what I usually think when I consider those demos:</p><p><img src="/blog/Images/SDGqd664wltq5nf1C8FRqQ.png"/></p><p>In our example, we can implement all of this in about 250 lines of code. The magic behind it is the fact that we can rely on RavenDB’s AI Agents feature to do most of the heavy lifting for us.</p><p>Inside RavenDB, this is defined as follows:</p><p><img src="/blog/Images/ivSxYW8aNkfTU0f4jxAjPA.png"/></p><p>For this post, however, we’ll look at how we actually built this AI-powered Telegram bot. The full code is <a href="https://github.com/ayende/samples.properties/blob/master/Services/PropertyAgent.cs">here</a> if you want to browse through it. </p><h3>What model is used here?</h3><blockquote><p>It’s worth mentioning that I’m not using anything fancy, the agent is using baseline <code>gpt-4.1-mini</code> for the demo. There is no need for training or customization, the way we create the agent already takes care of that.</p></blockquote><p>Here is the overall agent definition: </p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'><span class="token class-name"><span class="token namespace">store<span class="token punctuation">.</span></span>AI.CreateAgent</span><span class="token punctuation">(</span>
<span class="token keyword">new</span> <span class="token class-name">AiAgentConfiguration</span>
<span class="token punctuation">{</span>
<span class="token class-name">Name</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Property Assistant"</span></span><span class="token punctuation">,</span>
<span class="token class-name">Identifier</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"property-agent"</span></span><span class="token punctuation">,</span>
<span class="token class-name">ConnectionStringName</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Property Management AI Model"</span></span><span class="token punctuation">,</span>
<span class="token class-name">SystemPrompt</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"""
You are a property management assistant for renters.
... redacted ...
Do NOT discuss non-property topics.
"""</span></span><span class="token punctuation">,</span>
<span class="token class-name">Parameters</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token comment">// Visible to the model:</span>
<span class="token keyword">new</span> <span class="token class-name">AiAgentParameter</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"currentDate"</span></span><span class="token punctuation">,</span>
<span class="token string-literal"><span class="token string">"Current date in yyyy-MM-dd format"</span></span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token comment">// Agent scope only, not visible to the model directly</span>
<span class="token keyword">new</span> <span class="token class-name">AiAgentParameter</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"renterId"</span></span><span class="token punctuation">,</span>
<span class="token string-literal"><span class="token string">"Renter ID; answer only for this renter"</span></span><span class="token punctuation">,</span> sendToModel<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token keyword">new</span> <span class="token class-name">AiAgentParameter</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"renterUnits"</span></span><span class="token punctuation">,</span>
<span class="token string-literal"><span class="token string">"List of unit IDs occupied by the renter"</span></span><span class="token punctuation">,</span> sendToModel<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token class-name">SampleObject</span> <span class="token operator">=</span> <span class="token class-name">JsonConvert.SerializeObject</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Reply</span>
<span class="token punctuation">{</span>
<span class="token class-name">Answer</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Detailed answer to query (markdown syntax)"</span></span><span class="token punctuation">,</span>
<span class="token class-name">Followups</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Likely follow-ups"</span></span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token comment">// redacted</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><hr/></p><p>The code above will create an agent with the given prompt. It turns out that a lot of work actually goes into that prompt to explain to the AI model exactly what its role is, what it is meant to do, etc. </p><p>I reproduced the entire prompt below so you can read it more easily, but take into account that you’ll likely tweak it a <em>lot</em>, and that it is usually much longer than what we have here (although what we have below is quite functional, as you can see from the screenshots).</p><h3>The agent’s prompt</h3><blockquote><p>You are a property management assistant for renters.</p><p>Provide information about rent, utilities, debts, service requests, and property details.</p><p>Be professional, helpful, and responsive to renters’ needs.</p><p>You can answer in Markdown format. Make sure to use ticks (`) whenever you discuss identifiers.</p><p>Do not suggest actions that are not explicitly allowed by the tools available to you.</p><p>Do NOT discuss non-property topics. Answer only for the current renter.</p><p>When discussing amounts, always format them as currency with 2 decimal places.</p></blockquote><p>The way RavenDB deals with AI Agents, we define two very important aspects of them. First, we have the parameters, which define the scope of the system. In this case, you can see that we pass the <code>currentDate</code>, as well as provide the <code>renterId </code>and <code>renterUnits </code>that this agent is going to deal with. </p><p>We expose the current date to the model, but not the renter ID or the units that define the scope (we’ll touch on that in a bit). The model needs the current date so it will understand when it is running and have context for things like “last month”. But we don’t need to give it the IDs, they have no meaning and are instead used to define the scope of a particular conversation with the model. </p><p>The sample object we use defines the structure of the reply that we require the model to give us. In this case, we want to get a textual message from the model in Markdown format, as well as a separate array of likely follow-ups that we can provide to the user.</p><p>In order to do its job, the agent needs to be able to access the system. RavenDB handles that by letting you define queries that the model can ask the agent to execute when it needs more information. Here are some of them:</p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'><span class="token class-name">Queries</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token keyword">new</span> <span class="token class-name">AiAgentToolQuery</span>
<span class="token punctuation">{</span>
<span class="token class-name">Name</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"GetRenterInfo"</span></span><span class="token punctuation">,</span>
<span class="token class-name">Description</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Retrieve renter profile details"</span></span><span class="token punctuation">,</span>
<span class="token class-name">Query</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"from Renters where id() = </span><span class="token interpolation"><span class="token punctuation">$</span><span class="token expression">renterId</span></span><span class="token string">"</span></span><span class="token punctuation">,</span>
<span class="token class-name">ParametersSampleObject</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"{}"</span></span><span class="token punctuation">,</span>
<span class="token class-name">Options</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">AiAgentToolQueryOptions</span>
<span class="token punctuation">{</span>
<span class="token class-name">AllowModelQueries</span> <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
<span class="token class-name">AddToInitialContext</span> <span class="token operator">=</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token keyword">new</span> <span class="token class-name">AiAgentToolQuery</span>
<span class="token punctuation">{</span>
<span class="token class-name">Name</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"GetOutstandingDebts"</span></span><span class="token punctuation">,</span>
<span class="token class-name">Description</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Retrieve renter's outstanding debts (unpaid balances)"</span></span><span class="token punctuation">,</span>
<span class="token class-name">Query</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"""
from index 'DebtItems/Outstanding'
where RenterIds in (</span><span class="token interpolation"><span class="token punctuation">$</span><span class="token expression">renterId</span></span><span class="token string">) and AmountOutstanding > 0
order by DueDate asc
limit 10
"""</span></span><span class="token punctuation">,</span>
<span class="token class-name">ParametersSampleObject</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"{}"</span></span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token keyword">new</span> <span class="token class-name">AiAgentToolQuery</span>
<span class="token punctuation">{</span>
<span class="token class-name">Name</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"GetUtilityUsage"</span></span><span class="token punctuation">,</span>
<span class="token class-name">Description</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"""
Retrieve utility usage for renter's unit within a date
range (for calculating bills)
"""</span></span><span class="token punctuation">,</span>
<span class="token class-name">Query</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"""
from 'Units'
where id() in (</span><span class="token interpolation"><span class="token punctuation">$</span><span class="token expression">renterUnits</span></span><span class="token string">)
select
timeseries(from 'Power'
between </span><span class="token interpolation"><span class="token punctuation">$</span><span class="token expression">startDate</span></span><span class="token string"> and </span><span class="token interpolation"><span class="token punctuation">$</span><span class="token expression">endDate</span></span><span class="token string">
group by 1d
select sum()),
timeseries(from 'Water'
between </span><span class="token interpolation"><span class="token punctuation">$</span><span class="token expression">startDate</span></span><span class="token string"> and </span><span class="token interpolation"><span class="token punctuation">$</span><span class="token expression">endDate</span></span><span class="token string">
group by 1d
select sum())
"""</span></span><span class="token punctuation">,</span>
<span class="token class-name">ParametersSampleObject</span> <span class="token operator">=</span>
<span class="token string-literal"><span class="token string">"""
{
"startDate": "yyyy-MM-dd",
"endDate": "yyyy-MM-dd"
}
"""</span></span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">]</span></code></pre><hr/></p><p>The first query in the previous snippet, <code>GetRenterInfo, </code>is interesting. You can see that it is marked as: <code>AllowModelQueries = false, AddToInitialContext = true. W</code>hat does that mean?</p><p>It means that as part of creating a new conversation with the model, we are going to run the query to get all the renter’s details and add that to the initial context we send to the model. That allows us to provide the model with the information it will likely need upfront. </p><p>Note that we use the <code>$renterId</code> and <code>$renterUnits</code> parameters in the queries. While they aren’t exposed directly to the model, they affect what information the model can see. This is a good thing, since it means we place guardrails very early on. The model simply cannot see any information that is out of scope for it.</p><h2>The model can ask for additional information when it needs to…</h2><p>An important observation about the design of AI agents with RavenDB: note that we provided the model with a bunch of potential queries that it can run. <code>GetRenterInfo </code>is run at the beginning, since it gives us the initial context, but the rest are left for the judgment of the model.</p><p>The model can decide what queries it needs to run in order to answer the user’s questions, and it does so of its own accord. This decision means that once you have defined the set of queries and operations that the model can run, you are mostly done. The AI is smart enough to figure out what to do and then act according to your data.</p><p>Here is an example of what this looks like from the backend:</p><p><img src="/blog/Images/IKdfEV3eVXzR_jl_InBldw.png"/></p><p>Here you can see that the user asked about their utilities, the model then ran the appropriate query and formulated an answer for the user.</p><h3>The follow-ups UX pattern</h3><blockquote><p>You might have noticed that we asked the model for follow-up questions that the user may want to ask. This is a hidden way to guide the <em>user</em> toward the set of operations that the model supports. </p><p>The model will generate the follow-ups based on its own capabilities (queries and actions that it knows it can run), so this is a pretty simple way to “tell” that to the user without being obnoxious about it.</p></blockquote><p>Let’s look at how things work when we actually use this to build the bot, then come back to the rest of the agent’s definition. </p><h2>Plugging the model into Telegram</h2><p>We looked at the agent’s definition so far - let’s see how we actually use that. The Telegram’s API is really nice, basically boiling down to:</p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'>_botClient <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TelegramBotClient</span><span class="token punctuation">(</span>botSecretToken<span class="token punctuation">)</span><span class="token punctuation">;</span>
_botClient<span class="token punctuation">.</span><span class="token function">StartReceiving</span><span class="token punctuation">(</span>
<span class="token class-name">HandleUpdateAsync</span><span class="token punctuation">,</span>
<span class="token class-name">HandleErrorAsync</span><span class="token punctuation">,</span>
<span class="token keyword">new</span> <span class="token class-name">ReceiverOptions</span>
<span class="token punctuation">{</span>
<span class="token class-name">AllowedUpdates</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token class-name">UpdateType.Message</span><span class="token punctuation">,</span>
<span class="token class-name">UpdateType.CallbackQuery</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
_cts<span class="token punctuation">.</span>Token
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">async</span> <span class="token class-name">Task</span> <span class="token class-name">HandleUpdateAsync</span><span class="token punctuation">(</span><span class="token class-name">ITelegramBotClient</span> botClient<span class="token punctuation">,</span>
<span class="token class-name">Update</span> update<span class="token punctuation">,</span> <span class="token class-name">CancellationToken</span> cancellationToken<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">switch</span> <span class="token punctuation">(</span>update<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">case</span> <span class="token punctuation">{</span> <span class="token class-name">Message</span><span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token class-name">Text</span><span class="token punctuation">:</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> messageText <span class="token punctuation">}</span> message <span class="token punctuation">}</span><span class="token punctuation">:</span>
<span class="token keyword">await</span> <span class="token class-name">ProcessMessageAsync</span><span class="token punctuation">(</span>botClient<span class="token punctuation">,</span>
<span class="token class-name"><span class="token namespace">message<span class="token punctuation">.</span></span>Chat.Id.ToString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
messageText<span class="token punctuation">,</span>
cancellationToken<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">break</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><hr/></p><p>And then the Telegram API will call the <code>HandleUpdateAsync</code> method when there is a new message to the bot. Note that you may actually get multiple (concurrent messages), maybe from different chats, at the same time.</p><p>We’ll focus on the process message function, where we start by checking exactly who we are talking to:</p><p><hr/><pre class='line-numbers language-javascript'><code class='line-numbers language-javascript'><span class="token keyword">async</span> Task <span class="token function">ProcessMessageAsync</span><span class="token punctuation">(</span><span class="token parameter">ITelegramBotClient botClient<span class="token punctuation">,</span>
string chatId<span class="token punctuation">,</span> string messageText<span class="token punctuation">,</span> CancellationToken cancellationToken</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
using <span class="token keyword">var</span> session <span class="token operator">=</span> _documentStore<span class="token punctuation">.</span><span class="token function">OpenAsyncSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> renter <span class="token operator">=</span> <span class="token keyword">await</span> session<span class="token punctuation">.</span>Query<span class="token operator"><</span>Renter<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">FirstOrDefaultAsync</span><span class="token punctuation">(</span><span class="token parameter">r</span> <span class="token operator">=></span> r<span class="token punctuation">.</span>TelegramChatId <span class="token operator">==</span> chatId<span class="token punctuation">,</span>
cancellationToken<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>renter <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">await</span> botClient<span class="token punctuation">.</span><span class="token function">SendMessage</span><span class="token punctuation">(</span>chatId<span class="token punctuation">,</span>
<span class="token string">"Sorry, your Telegram account is not linked to a renter profile."</span><span class="token punctuation">,</span>
<span class="token literal-property property">cancellationToken</span><span class="token operator">:</span> cancellationToken
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> conversationId <span class="token operator">=</span> $<span class="token string">"chats/{chatId}/{DateTime.Today:yyyy-MM-dd}"</span><span class="token punctuation">;</span>
<span class="token comment">// more code in the next snippet</span>
<span class="token punctuation">}</span></code></pre><hr/></p><p>Telegram uses the term chat ID in their API, but it is what I would call the renter’s ID. When we register renters, we also record their Telegram chat ID, which means that when we get a message from a user, we can check whether they are a valid renter in our system. If not, we fail early and are done.</p><p>If they are, this is where things start to get interesting. Look at the conversation ID that we generated in the last line. RavenDB uses the notion of a conversation with the agent to hold state. The conversation we create here means that the bot will use the same conversation with the user for the same day.</p><p>Another way to do that would be to keep the same conversation ID open for the same user. Since RavenDB will automatically handle summarizing and trimming the conversation, either option is fine and mostly depends on your scenario.</p><p>The next stage is to create the actual conversation. To do that, we need to provide the model with the right context it is looking for:</p><p><hr/><pre class='line-numbers language-bash'><code class='line-numbers language-bash'>var renterUnits <span class="token operator">=</span> await session.Query<span class="token operator"><</span>Lease<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span>
.Where<span class="token punctuation">(</span>l <span class="token operator">=</span><span class="token operator">></span> l.RenterIds.Contains<span class="token punctuation">(</span>renter.Id<span class="token operator">!</span><span class="token punctuation">))</span>
.Select<span class="token punctuation">(</span>l <span class="token operator">=</span><span class="token operator">></span> l.UnitId<span class="token punctuation">)</span>
.ToListAsync<span class="token punctuation">(</span>cts<span class="token punctuation">)</span><span class="token punctuation">;</span>
var conversation <span class="token operator">=</span> _documentStore.AI.Conversation<span class="token punctuation">(</span><span class="token string">"property-agent"</span>,
conversationId,
new AiConversationCreationOptions
<span class="token punctuation">{</span>
Parameters <span class="token operator">=</span> new Dictionary<span class="token operator"><</span>string, object?<span class="token operator">></span>
<span class="token punctuation">{</span>
<span class="token punctuation">[</span><span class="token string">"renterId"</span><span class="token punctuation">]</span> <span class="token operator">=</span> renter.Id,
<span class="token punctuation">[</span><span class="token string">"renterUnits"</span><span class="token punctuation">]</span> <span class="token operator">=</span> renterUnits,
<span class="token punctuation">[</span><span class="token string">"currentDate"</span><span class="token punctuation">]</span> <span class="token operator">=</span> DateTime.Today.ToString<span class="token punctuation">(</span><span class="token string">"yyyy-MM-dd"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><hr/></p><p>You can see that we pass the renter ID and the relevant units for the renter to the model. Those form the creation parameters for the conversation and cannot be changed. That is one of the reasons why you may want to have a different conversation per day, to get the updated values if they changed.</p><p>With that done, we can send the results back to the model and then to the user, like so:</p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'><span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token keyword">await</span> <span class="token class-name"><span class="token namespace">conversation<span class="token punctuation">.</span></span>RunAsync</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">PropertyAgent.Reply</span><span class="token punctuation">></span></span><span class="token punctuation">(</span>cts<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> replyMarkup <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReplyKeyboardMarkup</span><span class="token punctuation">(</span><span class="token class-name"><span class="token namespace">result<span class="token punctuation">.</span></span>Answer.Followups
.Select</span><span class="token punctuation">(</span>text <span class="token operator">=</span><span class="token operator">></span> <span class="token keyword">new</span> <span class="token class-name">KeyboardButton</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">ToArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token class-name">ResizeKeyboard</span> <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
<span class="token class-name">OneTimeKeyboard</span> <span class="token operator">=</span> <span class="token boolean">true</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">await</span> <span class="token class-name"><span class="token namespace">botClient<span class="token punctuation">.</span></span>SendMessage</span><span class="token punctuation">(</span>
chatId<span class="token punctuation">,</span>
<span class="token class-name"><span class="token namespace">result<span class="token punctuation">.</span></span>Answer.Answer</span><span class="token punctuation">,</span>
replyMarkup<span class="token punctuation">:</span> replyMarkup<span class="token punctuation">,</span>
cancellationToken<span class="token punctuation">:</span> cts<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><hr/></p><p>The <code>RunAsync()</code> method handles the entire interaction with the model, and most of the code is just dealing with the reply markup for Telegram.</p><p>If you look closely at the chat screenshot above, you can see that we aren’t just asking the model questions, we get the bot to perform actions. For example, paying the rent. Here is what this looks like:</p><p><img src="/blog/Images/yZoNCftwkqpV06ap7pNWAg.png"/></p><p>How does this work?</p><h2>Paying the rent through the bot</h2><p>When we looked at the agent, we saw that we exposed some queries that the agent can run. But that isn’t the complete picture, we also give the model the ability to run actions. Here is what this looks like from the agent’s definition side:</p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'><span class="token class-name">Actions</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
<span class="token keyword">new</span> <span class="token class-name">AiAgentToolAction</span>
<span class="token punctuation">{</span>
<span class="token class-name">Name</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"ChargeCard"</span></span><span class="token punctuation">,</span>
<span class="token class-name">Description</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"""
Record a payment for one or more outstanding debts. The
renter can pay multiple debt items in a single transaction.
Can pay using any stored card on file.
"""</span></span><span class="token punctuation">,</span>
<span class="token class-name">ParametersSampleObject</span> <span class="token operator">=</span> <span class="token class-name">JsonConvert.SerializeObject</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ChargeCardArgs</span>
<span class="token punctuation">{</span>
<span class="token class-name">DebtItemIds</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"debtitems/1-A"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"debtitems/2-A"</span></span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token class-name">PaymentMethod</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Card"</span></span><span class="token punctuation">,</span>
<span class="token class-name">Card</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Last 4 digits of the card"</span></span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">]</span></code></pre><hr/></p><p>The idea here is that we expose to the model the kinds of actions it can request, and we specify what parameters it should pass to them, etc. What we are <em>not</em> doing here is giving the model control over actually running any code or modifying any data.</p><p>Instead, when the model needs to charge a card, it will have to call <em>your</em> code and go through validation, business logic, and authorization. Here is what this looks like on the other side. When we create a conversation, we specify handlers for all the actions we need to take, like so:</p><p><hr/><pre class='line-numbers language-javascript'><code class='line-numbers language-javascript'>conversation<span class="token punctuation">.</span>Handle<span class="token operator"><</span>PropertyAgent<span class="token punctuation">.</span>ChargeCardArgs<span class="token operator">></span><span class="token punctuation">(</span><span class="token string">"ChargeCard"</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token parameter">args</span> <span class="token operator">=></span>
<span class="token punctuation">{</span>
using <span class="token keyword">var</span> paySession <span class="token operator">=</span> _documentStore<span class="token punctuation">.</span><span class="token function">OpenAsyncSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> renterWithCard <span class="token operator">=</span> <span class="token keyword">await</span> paySession<span class="token punctuation">.</span>LoadAsync<span class="token operator"><</span>Renter<span class="token operator">></span><span class="token punctuation">(</span>renter<span class="token punctuation">.</span>Id<span class="token operator">!</span><span class="token punctuation">,</span> cts<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> card <span class="token operator">=</span> renterWithCard<span class="token operator">?.</span>CreditCards
<span class="token punctuation">.</span><span class="token function">FirstOrDefault</span><span class="token punctuation">(</span><span class="token parameter">c</span> <span class="token operator">=></span> c<span class="token punctuation">.</span>Last4Digits <span class="token operator">==</span> args<span class="token punctuation">.</span>Card<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>card <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">InvalidOperationException</span><span class="token punctuation">(</span>
$<span class="token string">"Card ending in {args.Card} not found in your profile."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> totalPaid <span class="token operator">=</span> <span class="token keyword">await</span> PaymentService<span class="token punctuation">.</span><span class="token function">CreatePaymentForDebtsWithCardAsync</span><span class="token punctuation">(</span>
paySession<span class="token punctuation">,</span>
renter<span class="token punctuation">.</span>Id<span class="token operator">!</span><span class="token punctuation">,</span>
args<span class="token punctuation">.</span>DebtItemIds<span class="token punctuation">,</span>
card<span class="token punctuation">,</span>
args<span class="token punctuation">.</span>PaymentMethod<span class="token punctuation">,</span>
cts<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> $<span class="token string">"Charged {totalPaid:C2} to {card.Type}"</span> <span class="token operator">+</span>
$<span class="token string">" ending in {card.Last4Digits}."</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><hr/></p><p>Note that we do some basic validation, then we call the <code>CreatePaymentForDebtsWithCardAsync()</code>method to perform the actual operation. It is also fun that we can just return a message string to give the model an idea about what the result of the action is. </p><p>Inside <code>CreatePaymentForDebtsWithCardAsync(),</code>we also verify that the debts we are asked to pay are associated with the current renter; we may have to apply additional logic, etc. The concept is that we assume the model is <em>not</em> to be trusted, so we need to carefully validate the input and use our code to verify that everything is fine.</p><h1>Summary</h1><p>This post has gone on for quite a while, so I think we’ll stop here. As a reminder, the <a href="https://github.com/ayende/samples.properties">PropertySphere</a> sample application code is available. And if you are one of those who prefer videos to text, you can <a href="https://www.youtube.com/watch?v=XOdXDNIGzxE">watch the video here</a>. </p><p>In <a href="https://ayende.com/blog/203622-B/propertysphere-bot-understanding-images?key=889433c1a0cb4633b096390e184c9159">the next post</a>, I’m going to show you how we can make the bot even smarter by adding visual recognition to the mix.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203621-B/propertyspheres-intelligent-telegram-bot?Key=1802d1b2-62ad-4016-9d1f-cac2ebf1acffhttps://ayende.com/blog/203621-B/propertyspheres-intelligent-telegram-bot?Key=1802d1b2-62ad-4016-9d1f-cac2ebf1acff2025年12月22日 12:00:00 GMTPropertySphere sample application<p>This post introduces the <a href="https://github.com/ayende/samples.properties">PropertySphere</a> sample application. I’m going to talk about some aspects of the sample application in this post, then in the next one, we will introduce AI into the mix.</p><blockquote><p>You can also <a href="https://www.youtube.com/watch?v=XOdXDNIGzxE">watch me walk through the entire application in this video</a>.</p></blockquote><p>This is based on a real-world scenario from a customer. One of the nicest things about AI being so easy to use is that I can generate throwaway code for a conversation with a customer that is actually a full-blown application.</p><blockquote><p><a href="https://github.com/ayende/samples.properties">The full code for the sample application is available on GitHub</a>.</p></blockquote><p>Here is the application dashboard, so you can get some idea about what this is all about:</p><p><img src="/blog/Images/vkRtc5XjRQjjIvDAGgqZ-Q.png"/></p><p>The idea is that you have Properties (apartment buildings), which have Units (apartments), which you then Lease to Renters. Note the capitalized words in the last sentence, those are the key domain entities that we work with.</p><p>Note that this dashboard shows quite a lot of data from many different places in the system. The map defines which properties we are looking at. It’s not just a static map, it is interactive. You can zoom in on a region to apply a spatial filter to the data in the dashboard. </p><p>Let’s take a closer look at what we are doing here. I’m primarily a backend guy, so I’m ignoring what the front end is doing to focus on the actual behavior of the system.</p><p>Here is what a typical endpoint will return for the dashboard:</p><p><hr/><pre class='line-numbers language-javascript'><code class='line-numbers language-javascript'><span class="token punctuation">[</span><span class="token function">HttpGet</span><span class="token punctuation">(</span><span class="token string">"status/{status}"</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
<span class="token keyword">public</span> IActionResult <span class="token function">GetByStatus</span><span class="token punctuation">(</span><span class="token parameter">string status<span class="token punctuation">,</span> <span class="token punctuation">[</span>FromQuery<span class="token punctuation">]</span> string<span class="token operator">?</span> boundsWkt</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">var</span> docQuery <span class="token operator">=</span> _session
<span class="token punctuation">.</span>Query<span class="token operator"><</span>ServiceRequests_ByStatusAndLocation<span class="token punctuation">.</span>Result<span class="token punctuation">,</span>
ServiceRequests_ByStatusAndLocation<span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=></span> x<span class="token punctuation">.</span>Status <span class="token operator">==</span> status<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">OrderByDescending</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=></span> x<span class="token punctuation">.</span>OpenedAt<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">Take</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>string<span class="token punctuation">.</span><span class="token function">IsNullOrWhiteSpace</span><span class="token punctuation">(</span>boundsWkt<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
docQuery <span class="token operator">=</span> docQuery<span class="token punctuation">.</span><span class="token function">Spatial</span><span class="token punctuation">(</span>
<span class="token parameter">x</span> <span class="token operator">=></span> x<span class="token punctuation">.</span>Location<span class="token punctuation">,</span> <span class="token parameter">spatial</span> <span class="token operator">=></span> spatial<span class="token punctuation">.</span><span class="token function">Within</span><span class="token punctuation">(</span>boundsWkt<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">var</span> results <span class="token operator">=</span> docQuery<span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span><span class="token parameter">x</span> <span class="token operator">=></span> <span class="token keyword">new</span>
<span class="token punctuation">{</span>
x<span class="token punctuation">.</span>Id<span class="token punctuation">,</span>
x<span class="token punctuation">.</span>Status<span class="token punctuation">,</span>
x<span class="token punctuation">.</span>OpenedAt<span class="token punctuation">,</span>
x<span class="token punctuation">.</span>UnitId<span class="token punctuation">,</span>
x<span class="token punctuation">.</span>PropertyId<span class="token punctuation">,</span>
x<span class="token punctuation">.</span>Type<span class="token punctuation">,</span>
x<span class="token punctuation">.</span>Description<span class="token punctuation">,</span>
PropertyName <span class="token operator">=</span> RavenQuery<span class="token punctuation">.</span>Load<span class="token operator"><</span>Property<span class="token operator">></span><span class="token punctuation">(</span>x<span class="token punctuation">.</span>PropertyId<span class="token punctuation">)</span><span class="token punctuation">.</span>Name<span class="token punctuation">,</span>
UnitNumber <span class="token operator">=</span> RavenQuery<span class="token punctuation">.</span>Load<span class="token operator"><</span>Unit<span class="token operator">></span><span class="token punctuation">(</span>x<span class="token punctuation">.</span>UnitId<span class="token punctuation">)</span><span class="token punctuation">.</span>UnitNumber
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ToList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token function">Ok</span><span class="token punctuation">(</span>results<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><hr/></p><p>We use a static index (we’ll see exactly why in a bit) to query for all the service requests by status and location, and then we project data from the document, including <em>related</em> document properties (the last two lines in the <code>Select</code> call).</p><p>A ServiceRequest doesn’t have a location, it gets that from its associated Property, so during indexing, we pull that from the relevant Property, like so:</p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'><span class="token class-name">Map</span> <span class="token operator">=</span> requests <span class="token operator">=</span><span class="token operator">></span>
from sr <span class="token keyword">in</span> requests
let property <span class="token operator">=</span> <span class="token class-name">LoadDocument</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Property</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token class-name"><span class="token namespace">sr<span class="token punctuation">.</span></span>PropertyId</span><span class="token punctuation">)</span>
select <span class="token keyword">new</span> <span class="token class-name">Result</span>
<span class="token punctuation">{</span>
<span class="token class-name">Id</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">sr<span class="token punctuation">.</span></span>Id</span><span class="token punctuation">,</span>
<span class="token class-name">Status</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">sr<span class="token punctuation">.</span></span>Status</span><span class="token punctuation">,</span>
<span class="token class-name">OpenedAt</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">sr<span class="token punctuation">.</span></span>OpenedAt</span><span class="token punctuation">,</span>
<span class="token class-name">UnitId</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">sr<span class="token punctuation">.</span></span>UnitId</span><span class="token punctuation">,</span>
<span class="token class-name">PropertyId</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">sr<span class="token punctuation">.</span></span>PropertyId</span><span class="token punctuation">,</span>
<span class="token class-name">Type</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">sr<span class="token punctuation">.</span></span>Type</span><span class="token punctuation">,</span>
<span class="token class-name">Description</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">sr<span class="token punctuation">.</span></span>Description</span><span class="token punctuation">,</span>
<span class="token class-name">Location</span> <span class="token operator">=</span> <span class="token class-name">CreateSpatialField</span><span class="token punctuation">(</span><span class="token class-name"><span class="token namespace">property<span class="token punctuation">.</span></span>Latitude</span><span class="token punctuation">,</span> <span class="token class-name"><span class="token namespace">property<span class="token punctuation">.</span></span>Longitude</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span></code></pre><hr/></p><p>You can see how we load the related Property and then index its location for the spatial query (last line).</p><p>You can see more interesting features when you drill down to the Unit page, where both its current status and its utility usage are displayed. That is handled using RavenDB’s time series feature, and then projected to a nice view on the frontend:</p><p><img src="/blog/Images/k21t_u3JYDeXsjc-d33YaQ.png"/></p><p>In the backend, this is handled using the following action call:</p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'><span class="token punctuation">[</span><span class="token class-name">HttpGet</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"unit/{*unitId}"</span></span><span class="token punctuation">)</span><span class="token punctuation">]</span>
public <span class="token class-name">IActionResult</span> <span class="token class-name">GetUtilityUsage</span><span class="token punctuation">(</span>string unitId<span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token class-name">FromQuery</span><span class="token punctuation">]</span> <span class="token class-name">DateTime</span><span class="token operator">?</span> from<span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token class-name">FromQuery</span><span class="token punctuation">]</span> <span class="token class-name">DateTime</span><span class="token operator">?</span> to<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">var</span> unit <span class="token operator">=</span> _session<span class="token punctuation">.</span>Load<span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Unit</span><span class="token punctuation">></span></span><span class="token punctuation">(</span>unitId<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>unit <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> <span class="token class-name">NotFound</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Unit not found"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> fromDate <span class="token operator">=</span> from <span class="token operator">?</span><span class="token operator">?</span> <span class="token class-name">DateTime.Today.AddMonths</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> toDate <span class="token operator">=</span> to <span class="token operator">?</span><span class="token operator">?</span> <span class="token class-name">DateTime.Today</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> result <span class="token operator">=</span> _session<span class="token punctuation">.</span>Query<span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Unit</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span>u <span class="token operator">=</span><span class="token operator">></span> <span class="token class-name"><span class="token namespace">u<span class="token punctuation">.</span></span>Id</span> <span class="token operator">==</span> unitId<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span>u <span class="token operator">=</span><span class="token operator">></span> <span class="token keyword">new</span>
<span class="token punctuation">{</span>
<span class="token class-name">PowerUsage</span> <span class="token operator">=</span> <span class="token class-name">RavenQuery.TimeSeries</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Power"</span></span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span>ts <span class="token operator">=</span><span class="token operator">></span> <span class="token class-name"><span class="token namespace">ts<span class="token punctuation">.</span></span>Timestamp</span> <span class="token operator">>=</span> fromDate <span class="token operator">&&</span> <span class="token class-name"><span class="token namespace">ts<span class="token punctuation">.</span></span>Timestamp</span> <span class="token operator"><=</span> toDate<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">GroupBy</span><span class="token punctuation">(</span>g <span class="token operator">=</span><span class="token operator">></span> <span class="token class-name"><span class="token namespace">g<span class="token punctuation">.</span></span>Hours</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span>g <span class="token operator">=</span><span class="token operator">></span> <span class="token class-name"><span class="token namespace">g<span class="token punctuation">.</span></span>Sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">ToList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token class-name">WaterUsage</span> <span class="token operator">=</span> <span class="token class-name">RavenQuery.TimeSeries</span><span class="token punctuation">(</span>u<span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Water"</span></span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">Where</span><span class="token punctuation">(</span>ts <span class="token operator">=</span><span class="token operator">></span> <span class="token class-name"><span class="token namespace">ts<span class="token punctuation">.</span></span>Timestamp</span> <span class="token operator">>=</span> fromDate <span class="token operator">&&</span> <span class="token class-name"><span class="token namespace">ts<span class="token punctuation">.</span></span>Timestamp</span> <span class="token operator"><=</span> toDate<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">GroupBy</span><span class="token punctuation">(</span>g <span class="token operator">=</span><span class="token operator">></span> <span class="token class-name"><span class="token namespace">g<span class="token punctuation">.</span></span>Hours</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span>g <span class="token operator">=</span><span class="token operator">></span> <span class="token class-name"><span class="token namespace">g<span class="token punctuation">.</span></span>Sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">ToList</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">FirstOrDefault</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token class-name">Ok</span><span class="token punctuation">(</span><span class="token keyword">new</span>
<span class="token punctuation">{</span>
<span class="token class-name">UnitId</span> <span class="token operator">=</span> unitId<span class="token punctuation">,</span>
<span class="token class-name">UnitNumber</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">unit<span class="token punctuation">.</span></span>UnitNumber</span><span class="token punctuation">,</span>
<span class="token class-name">From</span> <span class="token operator">=</span> fromDate<span class="token punctuation">,</span>
<span class="token class-name">To</span> <span class="token operator">=</span> toDate<span class="token punctuation">,</span>
<span class="token class-name">PowerUsage</span> <span class="token operator">=</span> result<span class="token operator">?</span><span class="token punctuation">.</span>PowerUsage<span class="token operator">?</span><span class="token punctuation">.</span>Results<span class="token operator">?</span>
<span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span>r <span class="token operator">=</span><span class="token operator">></span> <span class="token keyword">new</span> <span class="token class-name">UsageDataPoint</span>
<span class="token punctuation">{</span>
<span class="token class-name">Timestamp</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">r<span class="token punctuation">.</span></span>From</span><span class="token punctuation">,</span>
<span class="token class-name">Value</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">r<span class="token punctuation">.</span></span>Sum</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ToList</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">?</span><span class="token operator">?</span> <span class="token keyword">new</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">UsageDataPoint</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token class-name">WaterUsage</span> <span class="token operator">=</span> result<span class="token operator">?</span><span class="token punctuation">.</span>WaterUsage<span class="token operator">?</span><span class="token punctuation">.</span>Results<span class="token operator">?</span>
<span class="token punctuation">.</span><span class="token function">Select</span><span class="token punctuation">(</span>r <span class="token operator">=</span><span class="token operator">></span> <span class="token keyword">new</span> <span class="token class-name">UsageDataPoint</span>
<span class="token punctuation">{</span>
<span class="token class-name">Timestamp</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">r<span class="token punctuation">.</span></span>From</span><span class="token punctuation">,</span>
<span class="token class-name">Value</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">r<span class="token punctuation">.</span></span>Sum</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ToList</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">?</span><span class="token operator">?</span> <span class="token keyword">new</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">UsageDataPoint</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><hr/></p><p>As you can see, we run a single query to fetch data from multiple time series, which allows us to render this page.</p><p>By now, I think you have a pretty good grasp of what the application is about. So get ready for <a href="https://ayende.com/blog/203621-B/propertyspheres-intelligent-telegram-bot?key=1802d1b262ad40169d1fcac2ebf1acff">the </a><em><a href="https://ayende.com/blog/203621-B/propertyspheres-intelligent-telegram-bot?key=1802d1b262ad40169d1fcac2ebf1acff">next </a></em><a href="https://ayende.com/blog/203621-B/propertyspheres-intelligent-telegram-bot?key=1802d1b262ad40169d1fcac2ebf1acff">post</a>, where I will talk about how to add AI capabilities to the mix.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203620-B/propertysphere-sample-application?Key=2ab9814a-35aa-48ee-86f3-f3f9b1accd57https://ayende.com/blog/203620-B/propertysphere-sample-application?Key=2ab9814a-35aa-48ee-86f3-f3f9b1accd572025年12月19日 12:00:00 GMTRavenDB Kubernetes Operator <p>I’m happy to announce the official release of the RavenDB Kubernetes Operator. </p><p>As organizations use Kubernetes for more and more parts of their infrastructure, the complexity of deploying databases in such an environment is quite a challenge. For RavenDB, you need to handle certificates, persistence, and upgrades, and it is easy for that to become a bottleneck. This release bridges the gap between RavenDB’s ease of use and the declarative power of Kubernetes.</p><p><img src="/blog/Images/f7WrI1kJoEBQT5keUwCnQQ.png"/></p><p>If you are new to the concept, think of an operator as software that acts like a Site Reliability Engineer (SRE). Kubernetes is excellent at managing stateless applications, but databases require specific knowledge to manage correctly (e.g., "Don't upgrade all nodes at once" or "Ensure the leader is stable before restarting").</p><p>The RavenDB Operator extends the Kubernetes API. It allows you to define <em>what</em> you want your cluster to look like (the "Manifest"), and the Operator works tirelessly in the background to make sure your infrastructure matches that state.</p><h3>Why This Matters</h3><p>Previously, deploying a secure, clustered RavenDB instance on K8s required manual configuration of StatefulSets, Services, and complex TLS certificate chains.</p><p>With the RavenDB Kubernetes Operator, everything is driven by a single custom resource: <code>RavenDBCluster</code>. You provide the specs, and the Operator handles the heavy lifting, ensuring your deployments are fully reproducible, secure, and declarative.</p><p>Here is what the Operator brings to the table:</p><ul><li>Automated Security & Certificate Management: Whether you are using Let’s Encrypt or Self-Signed certificates, the Operator handles bootstrapping, distribution, and rotation.For the Operator’s own webhook certificate, the Operator uses cert-manager behind the scenes, since that is not exposed externally.</li><li>Safe Rolling Upgrades: Database upgrades can be scary. The Operator orchestrates upgrades node-by-node, using safety gates to ensure the cluster is healthy and data is safe before moving to the next node. If a gate fails, the upgrade stops automatically.</li><li>Flexible External Access: Exposing a database outside K8s is often a networking headache. We’ve added dedicated support for AWS NLB, Azure Load Balancer, HAProxy, Traefik, and NGINX, giving you production-ready access strategies out of the box.</li><li>Storage Orchestration: Declarative control over your data, logs, and audit volumes, supporting local paths, AWS EBS, and Azure Disks.</li><li>One-Click Deploy: Using our official Helm chart, you can spin up a fully operational cluster in minutes.</li></ul><p>The Operator is available now via Helm and works on EKS, AKS, Kind, Minikube, and Kubeadm clusters.</p><ul><li>ArtifactHub (Helm Chart):<a href="https://artifacthub.io/packages/helm/ravendb-operator/ravendb-operator">Get the Chart</a></li><li>GitHub Repository:<a href="https://github.com/ravendb/ravendb-operator">View Source & Examples</a></li><li>Quickstart Guide:<a href="https://www.google.com/search?q=https://github.com/ravendb/ravendb-operator%3Ftab%3Dreadme-ov-file%23installation">Read the Docs</a></li></ul><p>We look forward to seeing what you build with RavenDB on Kubernetes!</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203587-B/ravendb-kubernetes-operator?Key=1d5af4d7-9a6e-42a5-84a2-5d446342e2bbhttps://ayende.com/blog/203587-B/ravendb-kubernetes-operator?Key=1d5af4d7-9a6e-42a5-84a2-5d446342e2bb2025年12月15日 12:00:00 GMTChoosing "naked" vms or Kubernetes for hosting databases in the cloud<p>We are a database company, and many of our customers and users are running in the cloud. Fairly often, we field questions about the recommended deployment pattern for RavenDB.</p><p>Given the… rich landscape of DevOps options, RavenDB supports all sorts of deployment models:</p><ul><li>Embedded in your application</li><li>Physical hardware (from a Raspberry Pi to massive servers)</li><li>Virtual machines in the cloud</li><li>Docker</li><li>AWS / Azure marketplaces</li><li>Kubernetes</li><li>Ansible</li><li>Terraform</li></ul><p>As well as some pretty fancy permutations of the above in every shape and form.</p><p>With so many choices, the question is: what do you <em>recommend?</em> In particular, we were recently asked about deployment to a “naked machine” in the cloud versus using Kubernetes. The core requirements are to ensure high performance and high availability. </p><p>Our short answer is almost always: Best to go with direct VMs and skip Kubernetes for RavenDB.</p><p>While Kubernetes has revolutionized the deployment of stateless microservices, deploying stateful applications, particularly databases, on K8s introduces significant complexities that often outweigh the benefits, especially when performance and operational simplicity are paramount.</p><blockquote><p>A great quote in the DevOps world is “<a href="https://cloudscaling.com/blog/cloud-computing/the-history-of-pets-vs-cattle/">cattle, not pets</a>”, in reference to how you should manage your servers. That works great if you are dealing with stateless services. But when it comes to data management, your databases are <em>cherished</em> pets, and you should treat them as such. </p></blockquote><h2>The Operational Complexity of Kubernetes for Stateful Systems</h2><p>Using an orchestration layer like Kubernetes complicates the operational management of persistent state. While K8s provides tools for stateful workloads, they require a deep understanding of storage classes, Persistent Volumes (PVs), and Persistent Volume Claims (PVCs).</p><p>Consider a common, simple maintenance task: Changing a VM's disk type or size.</p><p>As a VM, this is typically a very easy operation and can be done with no downtime.The process is straightforward, well-documented, and often takes minutes.</p><p>For K8s, this becomes a significantly more complex task. You have to go deep into Kubernetes storage primitives to figure out how to properly migrate the data to a new disk specification. </p><p>There is an <code>allowVolumeExpansion: true</code> option that <em>should</em> make it work, but the details matter, and for databases, that is usually something DBAs are really careful about.</p><p>Databases tend to <em>care</em> about their disk. So what happens if we don’t want to just change the size of the disk, but also its <em>type?</em> Such as moving from Standard to Premium. Doing that using VMs is as simple as changing the size. You may need to detach, change, and reattach the disk, but that is a well-trodden path.</p><p>In Kubernetes, you need to run a migration, delete the StatefulSets, make the configuration change, and reapply (crossing your fingers and hoping everything works).</p><h2>Database nodes are not homogeneous</h2><p>Databases running in a cluster configuration often require granular control over node upgrades and maintenance. I may want to designate a node as “this one is doing backups”, so it needs a bigger disk. Easy to do if each node is a dedicated VM, but much harder in practice inside K8s.</p><p>A recent example we ran into is controlling the upgrade process of a cluster. As any database administrator can tell you, upgrades are something you approach cautiously. RavenDB has <em>great</em> support for running cross-version clusters.</p><p>In other words, take a node in your cluster, upgrade that to an updated version (including across major versions!), and it will just work. That allows you to dip your toes into the waters with a single node, instead of doing a hard switch to the new version.</p><p>In a VM environment: Upgrading a single node in a RavenDB cluster is a simple, controlled process. You stop the database on the VM, perform the upgrade (often just replacing binaries), start the database, and allow the cluster to heal and synchronize. This allows you to manage the cluster's rolling upgrades with precision.</p><p>In K8s: Performing a targeted upgrade on just one node of the cluster is <em>hard</em>. The K8s deployment model (StatefulSets) is designed to manage homogeneous replicas. While you can use features like "on delete" update strategy, blue/green deployments, or canary releases, they add layers of abstraction and complexity that are necessary for stateless services but actively harmful for stateful systems. </p><h1>Summary</h1><p>For mission-critical database infrastructure where high performance, high availability, and operational simplicity are non-negotiable, the added layer of abstraction introduced by Kubernetes for managing persistence often introduces more friction than value.</p><p>While Kubernetes is an excellent platform for stateless services, we strongly recommend deploying RavenDB directly on dedicated Virtual Machines. This provides a cleaner operational surface, simpler maintenance procedures, and more direct control over the underlying resources—all critical factors for a stateful, high-performance database cluster.</p><p>Remember, your database nodes are <em>cherished</em> pets, don’t make them sleep in the barn with the cattle.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203555-A/choosing-naked-vms-or-kubernetes-for-hosting-databases-in-the-cloud?Key=12c9738a-271c-40a8-97c1-6ad4d1cfe089https://ayende.com/blog/203555-A/choosing-naked-vms-or-kubernetes-for-hosting-databases-in-the-cloud?Key=12c9738a-271c-40a8-97c1-6ad4d1cfe0892025年12月11日 12:00:00 GMTImplementing Agentic Reminders in RavenDB<p>A really interesting problem for developers building agentic systems is moving away from chatting with the AI model. For example, consider the following conversation:</p><p><img src="/blog/Images/kigqhoDT1Wih1A8CpRXj9A.png"/></p><p>This is a pretty simple scenario where we need to actually step out of the chat and do something else. This seems like an obvious request, right? But it turns out to be a bit complex to build.</p><p>The reason for that is simple. AI models don’t actually behave like you would expect them to if your usage is primarily as a chat interface. Here is a typical invocation of a model in code:</p><p><hr/><pre class='line-numbers language-csharp'><code class='line-numbers language-csharp'><span class="token keyword">class</span> <span class="token class-name">MessageTuple</span><span class="token punctuation">(</span>NamedTuple<span class="token punctuation">)</span><span class="token punctuation">:</span>
role<span class="token punctuation">:</span> <span class="token class-name">str</span>
content<span class="token punctuation">:</span> str
<span class="token return-type class-name">def</span> <span class="token function">call_model</span><span class="token punctuation">(</span>
<span class="token named-parameter punctuation">message_history</span><span class="token punctuation">:</span> List<span class="token punctuation">[</span>MessageTuple<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token named-parameter punctuation">tools</span><span class="token punctuation">:</span> List<span class="token punctuation">[</span>Callable<span class="token punctuation">]</span> <span class="token operator">=</span> None
<span class="token punctuation">)</span><span class="token punctuation">:</span>
pass # redacted</code></pre><hr/></p><p>In other words, it is the responsibility of the caller to keep track of the conversation and send the <em>entire</em> conversation to the agent on each round. Here is what this looks like in code:</p><p><hr/><pre class='line-numbers language-json'><code class='line-numbers language-json'>conversation_history = <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token property">"role"</span><span class="token operator">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span>
<span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"When do I get my anniversary gift?"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
<span class="token property">"role"</span><span class="token operator">:</span> <span class="token string">"agent"</span><span class="token punctuation">,</span>
<span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"Based on our records, your two-year anniversary is in three days. This milestone means you're eligible for a gift card as part of our company's recognition program.\nOur policy awards a 100ドル gift card for each year of service. Since you've completed two years, a 200ドル gift card will be sent to you via SMS on October 1, 2025."</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">{</span>
<span class="token property">"role"</span><span class="token operator">:</span> <span class="token string">"user"</span><span class="token punctuation">,</span>
<span class="token property">"content"</span><span class="token operator">:</span> <span class="token string">"Remind me to double check I got that in a week"</span>
<span class="token punctuation">}</span>
<span class="token punctuation">]</span></code></pre><hr/></p><p>Let’s assume that we have a tool call for setting up reminders for users. In RavenDB, this looks like the screenshot below (<a href="https://docs.ravendb.net/7.1/ai-integration/ai-agents/creating-ai-agents/creating-ai-agents_api#action-tools">more on agentic actions in RavenDB here</a>):</p><p><img src="/blog/Images/uVtTZ17MGY_Vx9AVw05Y_Q.png"/></p><p>And in the backend, we have the following code:</p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'><span class="token class-name"><span class="token namespace">conversation<span class="token punctuation">.</span></span>Handle</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">CreateReminderArgs</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"CreateReminder"</span></span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span>args<span class="token punctuation">)</span> <span class="token operator">=</span><span class="token operator">></span>
<span class="token punctuation">{</span>
using <span class="token keyword">var</span> session <span class="token operator">=</span> _documentStore<span class="token punctuation">.</span><span class="token function">OpenAsyncSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> at <span class="token operator">=</span> <span class="token class-name">DateTime.Parse</span><span class="token punctuation">(</span>args<span class="token punctuation">.</span>at<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> reminder <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Reminder</span>
<span class="token punctuation">{</span>
<span class="token class-name">EmployeeId</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">request<span class="token punctuation">.</span></span>EmployeeId</span><span class="token punctuation">,</span>
<span class="token class-name">ConversationId</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">conversation<span class="token punctuation">.</span></span>Id</span><span class="token punctuation">,</span>
<span class="token class-name">Message</span> <span class="token operator">=</span> args<span class="token punctuation">.</span>msg<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">await</span> <span class="token class-name"><span class="token namespace">session<span class="token punctuation">.</span></span>StoreAsync</span><span class="token punctuation">(</span>reminder<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name"><span class="token namespace">session<span class="token punctuation">.</span></span>Advanced.GetMetadataFor</span><span class="token punctuation">(</span>reminder<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"@refresh"</span></span><span class="token punctuation">]</span> <span class="token operator">=</span> at<span class="token punctuation">;</span>
<span class="token keyword">await</span> <span class="token class-name"><span class="token namespace">session<span class="token punctuation">.</span></span>SaveChangesAsync</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> $<span class="token string-literal"><span class="token string">"Reminder set for {at} {reminder.Id}"</span></span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><hr/></p><p>This code uses several of RavenDB’s features to perform its task. First we have the conversation handler, which is the backend handling for the tool call we just saw. Next we have the use of the <code>@refresh </code>feature of RavenDB. <a href="https://ayende.com/blog/203203-B/scheduling-with-ravendb">I recently posted about how you can use this feature for scheduling</a>. </p><p>In short, we set up a <a href="https://docs.ravendb.net/6.2/client-api/data-subscriptions/what-are-data-subscriptions/">RavenDB Subscription</a> Task to be called when those reminders should be raised. Here is what the subscription looks like:</p><hr/><p><pre class='line-numbers language-javascript'><code class='line-numbers language-javascript'>from Reminders <span class="token keyword">as</span> r
where r<span class="token punctuation">.</span><span class="token string">'@metadata'</span><span class="token punctuation">.</span><span class="token string">'@refresh'</span> <span class="token operator">!=</span> <span class="token keyword">null</span></code></pre><hr/><p>And here is the client code to actually handle it:</p><hr/><pre class='line-numbers language-javascript'><code class='line-numbers language-javascript'><span class="token keyword">async</span> Task <span class="token function">HandleReminder</span><span class="token punctuation">(</span><span class="token parameter">Reminder reminder</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">var</span> conversation <span class="token operator">=</span> _documentStore<span class="token punctuation">.</span><span class="token constant">AI</span><span class="token punctuation">.</span><span class="token function">Conversation</span><span class="token punctuation">(</span>
<span class="token literal-property property">agentId</span><span class="token operator">:</span> <span class="token string">"smartest-agent"</span><span class="token punctuation">,</span>
reminder<span class="token punctuation">.</span>ConversationId<span class="token punctuation">,</span>
<span class="token literal-property property">creationOptions</span><span class="token operator">:</span> <span class="token keyword">null</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
conversation<span class="token punctuation">.</span><span class="token function">AddArtificialActionWithResponse</span><span class="token punctuation">(</span>
<span class="token string">"GetRaisedReminders"</span><span class="token punctuation">,</span> reminder<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> result <span class="token operator">=</span> <span class="token keyword">await</span> conversation<span class="token punctuation">.</span><span class="token function">RunAsync</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">await</span> <span class="token function">MessageUser</span><span class="token punctuation">(</span>conversation<span class="token punctuation">,</span> result<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><hr/><p>The question now is, what should we <em>do</em> with the reminder?</p><p>Going back to the top of this post, we know that we need to add the reminder to the conversation. The problem is that this isn’t part of the actual model of the conversation. This is neither a user prompt nor a model answer. How do we deal with this?</p><p>We use a really elegant approach here: we inject an artificial tool call into the conversation history. This makes the model think that it checked for reminders and received one in return, even though this happened outside the chat. This lets the agent respond naturally, as if the reminder were part of the ongoing conversation, preserving the full context.</p><p>Finally, since we’re not actively chatting with the user at this point, we need to send a message prompting them to check back on the conversation with the model.</p><h2>Summary</h2><p>This is a high-level post, meant specifically to give you some ideas about how you can take your agentic systems to a higher level than a simple chat with the model. The reminder example is a pretty straightforward example, but a truly powerful one. It transforms a simple chat into a much more complex interaction model with the AI.</p><p>RavenDB’s unique approach of "inserting" a tool call back into the conversation history effectively tells the AI model, "I've checked for reminders and found a reminder for this user." This allows the agent to handle the reminder within the context of the original conversation, rather than initiating a new one. It also allows the agent to maintain a single, coherent conversational thread with the user, even when the system needs to perform background tasks and re-engage with them later.</p><p>You can also use the same infrastructure to create a <em>new</em> conversation, if that makes sense in your domain, and use the previous conversation as “background material”, so to speak. There is a wide variety of options available to fit your exact scenario.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" /></p>
https://ayende.com/blog/203523-C/implementing-agentic-reminders-in-ravendb?Key=a4ed6127-e4fa-4df6-b2a5-4f17b1792a3bhttps://ayende.com/blog/203523-C/implementing-agentic-reminders-in-ravendb?Key=a4ed6127-e4fa-4df6-b2a5-4f17b1792a3b2025年12月08日 12:00:00 GMTRecording: Build AI that understands your business<p>I gave the following talk at Microsoft Ignite 2025:</p><p>Connecting LLMs to your secure, operational database involves complexity, security risks, and hallucinations. This session shows how to build context-aware AI agents directly on your existing data, going from live database to production-ready, secure AI agent in hours. You'll see how to ship personalized experiences that will define the next generation of software. RavenDB's CEO will demonstrate this approach.</p><p><iframe width="1840" height="1035" title="Build AI that understands your business | ODSP1462" src="https://www.youtube.com/embed/CpdCQZzT-Rk" frameborder="0" allowfullscreen="" referrerpolicy="strict-origin-when-cross-origin" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"></iframe></p>https://ayende.com/blog/203491-B/recording-build-ai-that-understands-your-business?Key=c7410eb4-6886-4390-b631-faedd7d7231ehttps://ayende.com/blog/203491-B/recording-build-ai-that-understands-your-business?Key=c7410eb4-6886-4390-b631-faedd7d7231e2025年12月05日 12:00:00 GMTRecording: From CRUD TO AI – building an intelligent Telegram bot in < 200 lines of code with RavenDB<p>Want to see how modern applications handle complexity, scale, and cutting-edge features without becoming unmanageable? In this deep-dive webinar, we move From CRUD to AI Agents, showcasing how RavenDB, a high-performance document database, simplifies the development of a complex Property Management application.</p><p><iframe width="1840" height="1035" title="From CRUD to AI Agents How To Build a Telegram Bot with Vector Search in 200 Lines of Code | RavenDB" src="https://www.youtube.com/embed/XOdXDNIGzxE" frameborder="0" allowfullscreen="" referrerpolicy="strict-origin-when-cross-origin" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"></iframe></p>https://ayende.com/blog/203459-B/recording-from-crud-to-ai-building-an-intelligent-telegram-bot-in-200-lines-of-code-with-ravendb?Key=61bbfd87-8de3-48e7-81a3-6f7fc69d3eb9https://ayende.com/blog/203459-B/recording-from-crud-to-ai-building-an-intelligent-telegram-bot-in-200-lines-of-code-with-ravendb?Key=61bbfd87-8de3-48e7-81a3-6f7fc69d3eb92025年12月02日 12:00:00 GMTUsing multi-staged actions with AI Agents to reduce costs & time<p>When building AI Agents, one of the challenges you have to deal with is the sheer amount of data that the agent may need to go through. A natural way to deal with that is <em>not</em> to hand the information directly to the model, but rather allow it to query for the information as it sees fit.</p><p>For example, in the case of a human resource assistant, we may want to expose the employer’s policies to the agent, so it can answer questions such as “What is the required holiday request time?”.</p><p>We can do that easily enough using the following agent-query mechanism:</p><p><img src="/blog/Images/cCCT52Mq4ifxZuCvxXcHfg.png"/></p><p>If the agent needs to answer a question about a policy, it can use this tool to get the policies and find out what the answer is.</p><p>That works if you are a mom & pop shop, but what happens if you happen to be a big organization, with policies on everything from requesting time off to bringing your own device to modern slavery prohibition? Calling this tool is going to give <em>all </em>those policies to the model? </p><p>That is going to be <em>incredibly</em> expensive, since you have to burn through a lot of tokens that are simply not relevant to the problem at hand.</p><p>The next step is <em>not</em> to return all of the policies and filter them. We can do that using vector search and utilize the model’s understanding of the data to help us find exactly what we want.</p><p><img src="/blog/Images/bmTBXHayEHFLnzmm1E8qmA.png"/></p><p>That is much better, but a search for “confidentiality contract” will get you the Non-Disclosure Agreement as well as the processes for hiring a new employee when their current employer isn’t aware they are looking, etc.</p><p>That can <em>still</em> be a lot of text to go through. It isn’t as much as <em>everything</em>, but still a pretty heavy weight. </p><p>A nice alternative to this is to break it into two separate operations, as you can see below:</p><p><img src="/blog/Images/d4G2hpZReWWKxjY-hGu05g.png"/></p><p>The model will first run the <code>FindPolicies</code> query to get the list of potential policies. It can then decide, based on their titles, which ones it is actually interested in reading the full text of. </p><p>You need to perform two tool calls in this case, but it actually ends up being both faster and cheaper in the end. </p><p>This is a surprisingly elegant solution, because it matches roughly how people think. No one is going to read a dozen books cover to cover to answer a question. We continuously narrow our scope until we find enough information to answer. </p><p>This approach gives your AI model the same capability to narrowly target the information it needs to answer the user’s query efficiently and quickly.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203430-C/using-multi-staged-actions-with-ai-agents-to-reduce-costs-time?Key=38244b26-20b7-4900-9b80-4b105cea21b2https://ayende.com/blog/203430-C/using-multi-staged-actions-with-ai-agents-to-reduce-costs-time?Key=38244b26-20b7-4900-9b80-4b105cea21b22025年11月17日 12:00:00 GMTReducing AI context load using actions<p>When using an AI model, one of the things that you need to pay attention to is the number of tokens you send to the model. They <em>literally</em> cost you money, so you have to balance the amount of data you send to the model against how much of it is <em>relevant</em> to what you want it to do.</p><p>That is especially important when you are building generic agents, which may be assigned a <em>bunch</em> of different tasks. The classic example is the human resources assistant, which may be tasked with checking your vacation days balance or called upon to get the current number of overtime hours that an employee has worked this month.</p><p>Let’s assume that we want to provide the model with a bit of context. We want to give the model all the recent HR tickets by the current employee. These can range from onboarding tasks to filling out the yearly evaluation, etc. </p><p>That sounds like it can give the model a big hand in understanding the state of the employee and what they want. Of course, that assumes the user is going to ask a question related to those issues.</p><p>What if they ask about the date of the next bank holiday? If we just unconditionally fed all the data to the model preemptively, that would be:</p><ul><li>Quite confusing to the model, since it will have to sift through a lot of irrelevant data.</li><li>Pretty expensive, since we’re going to send a <em>lot</em> of data (and pay for it) to the model, which then has to ignore it.</li><li>Compounding effect as the user & the model keep the conversation going, with all this unneeded information weighing everything down.</li></ul><p>A nice trick that can really help is to <em>not </em>expose the data directly, but rather provide it to the model as a set of actions it can invoke. In other words, when defining the agent, I don’t bother providing it with all the data it needs.</p><p>Rather, I provide the model a <em>way to access the data</em>. Here is what this looks like in RavenDB:</p><p><img src="/blog/Images/IAtwhrjTjb9youuoOByI3Q.png"/></p><p>The agent is provided with a bunch of queries that it can call to find out various interesting details about the current employee. The end result is that the model will invoke those queries to get <em>just</em> the information it wants.</p><p>The overall number of tokens that we are going to consume will be <em>greatly</em> reduced, while the ability of the model to actually access relevant information is enhanced. We don’t need to go through stuff we don’t care about, after all. </p><p>This approach gives you a very focused model for the task at hand, and it is easy to extend the agent with additional information-retrieval capabilities.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203429-C/reducing-ai-context-load-using-actions?Key=34b3dc56-9e87-445e-972d-348f105e80aahttps://ayende.com/blog/203429-C/reducing-ai-context-load-using-actions?Key=34b3dc56-9e87-445e-972d-348f105e80aa2025年11月14日 12:00:00 GMTUsing AI Agents parameters outside of the model's scope<p>Building an AI Agent in RavenDB is very much like defining a class, you define all the things that it can do, the initial prompt to the AI model, and you specify which parameters the agent requires. Like a class, you can create an instance of an AI agent by starting a new conversation with it. Each conversation is a separate instance of the agent, with different parameters, an initial user prompt, and its own history.</p><p>Here is a simple example of a non-trivial agent. For the purpose of this post, I want to focus on the parameters that we pass to the model. </p><p><hr/><pre class='line-numbers language-bash'><code class='line-numbers language-bash'>var agent <span class="token operator">=</span> new AiAgentConfiguration<span class="token punctuation">(</span>
<span class="token string">"shopping assistant"</span>,
config.ConnectionStringName,
<span class="token string">"You are an AI agent of an online shop..."</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
Parameters <span class="token operator">=</span>
<span class="token punctuation">[</span>
new AiAgentParameter<span class="token punctuation">(</span><span class="token string">"lang"</span>,
<span class="token string">"The language the model should respond with."</span><span class="token punctuation">)</span>,
new AiAgentParameter<span class="token punctuation">(</span><span class="token string">"currency"</span>, <span class="token string">"Preferred currency for the user"</span><span class="token punctuation">)</span>,
new AiAgentParameter<span class="token punctuation">(</span><span class="token string">"customerId"</span>, null, sendToModel: <span class="token boolean">false</span><span class="token punctuation">)</span>,
<span class="token punctuation">]</span>,
Queries <span class="token operator">=</span> <span class="token punctuation">[</span> /* redacted<span class="token punctuation">..</span>. */ <span class="token punctuation">]</span>,
Actions <span class="token operator">=</span> <span class="token punctuation">[</span> /* redacted<span class="token punctuation">..</span>. */ <span class="token punctuation">]</span>,
<span class="token punctuation">}</span><span class="token punctuation">;</span></code></pre><hr/></p><p>As you can see in the configuration, we define the <code>lang</code> and <code>currency</code> parameters as standard agent parameters. These are defined with a description for the model and are passed to the model when we create a new conversation. </p><p>But what about the <code>customerId</code> parameter? It is marked as <code>sendToModel: false</code>. What is the <em>point</em> of that? To understand this, you need to know a bit more about how RavenDB deals with the model, conversations, and memory.</p><p>Each conversation with the model is recorded using a conversation document, and part of this includes the parameters you pass to the conversation when you create it. In this case, we don’t need to pass the <code>customerId</code> parameter to the model; it doesn’t hold any meaning for the model and would just waste tokens.</p><p>The key is that you can <em>query based </em>on those parameters. For example, if you want to get all the conversations for a particular customer (to show them their conversation history), you can use the following query:</p><p><hr/><pre class='line-numbers language-php'><code class='line-numbers language-php'>from <span class="token string double-quoted-string">"@conversations"</span>
where Parameters<span class="token operator">.</span>customerId <span class="token operator">=</span> <span class="token variable">$customerId</span></code></pre><hr/></p><p>This is also <em>very</em> useful when you have data that you genuinely don’t want to expose to the model but still want to attach to the conversation. You can set up a query that the model may call to get the most recent orders for a customer, and RavenDB will do that (using <code>customerId</code>) without letting the model actually see that value. </p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203428-C/using-ai-agents-parameters-outside-of-the-models-scope?Key=9fb498ed-4de8-473a-a3e6-78221253f0c6https://ayende.com/blog/203428-C/using-ai-agents-parameters-outside-of-the-models-scope?Key=9fb498ed-4de8-473a-a3e6-78221253f0c62025年11月12日 12:00:00 GMTJoin RavenDB at Microsoft Ignite Next Week<p><a href="https://ignite.microsoft.com/en-US/sponsors/79e19247-5db8-4153-b5d5-95735931ce16?">The RavenDB team will be at Microsoft Ignite in San Francisco</a> next week, as will be yours truly in person 🙂. We are going to show off RavenDB and its features both new and old. </p><p><a href="https://ignite.microsoft.com/en-US/sessions/ODSP1462">I'll be hosting a session demonstrating how to build powerful AI Agents using RavenDB</a>.I’ll show practical examples and the features that make RavenDB suitable for AI-driven applications.</p><p>If you're at Microsoft Ignite or in the San Francisco area next week, I'd like to meet up.Feel free to reach out to discuss RavenDB, AI, architecture or anything else.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203427-C/join-ravendb-at-microsoft-ignite-next-week?Key=d706c934-a477-4509-8b7b-c5430ec0d7f5https://ayende.com/blog/203427-C/join-ravendb-at-microsoft-ignite-next-week?Key=d706c934-a477-4509-8b7b-c5430ec0d7f52025年11月10日 12:00:00 GMTRavenDB's new offices<p>RavenDB Ltd (formerly Hibernating Rhinos) has been around for quite some time!In its current form, we've been building the RavenDB database for over 15 years now.In late 2010, we officially moved into our first real offices.</p>
<p>Our first place was a small second-story office space deep in the industrial section, a bit out of the way, but it served us incredibly well until we grew and needed more space.Then we grew again, and again, and again!Last month, we moved offices yet again.</p>
<p>This new location represents our fifth office, with each relocation necessitated by our growth exceeding the capacity of the previous premises.</p>
<p>If you ever pass by Hadera, where our offices now proudly reside, you'll spot a big sign as you enter the city!</p>
<p><img src="/blog/Images/trLAGZy5Ge7pyfGBQDomtA.png" alt="" /></p>
<p>You can also see how it looks like from the inside:</p>
<p><iframe title="Welcome to Our New Office Space at the RavenDB Headquarters in Hadera, Israel | RavenDB" src="https://www.youtube.com/embed/XVsz6hQdpOA" width="640" height="480" frameborder="0" allowfullscreen="allowfullscreen"></iframe></p>https://ayende.com/blog/203395-C/ravendbs-new-offices?Key=9ecab156-ca92-4011-9eac-86218105d966https://ayende.com/blog/203395-C/ravendbs-new-offices?Key=9ecab156-ca92-4011-9eac-86218105d9662025年10月23日 12:00:00 GMTThe cost of design iteration in software engineering<p>I ran into this tweet from about <a href="https://x.com/thdxr/status/1964481163575321081">a month ago</a>:</p><blockquote><p><strong><a href="https://x.com/thdxr">dax </a></strong><a href="https://x.com/thdxr">@thdxr</a></p></blockquote><blockquote><p>programmers have a dumb chip on their shoulder that makes them try and emulate traditional engineering there is zero physical cost to iteration in software - can delete and start over, can live patch our approach should look a lot different than people who build bridges</p></blockquote><p>I have to say that I would <em>strongly </em>disagree with this statement. Using the building example, it is obvious that moving a window in an already built house is expensive. <em>Obviously,</em> it is going to be cheaper to move this window during the planning phase. </p><p>The answer is that it may be cheap<em>er</em>, but it won’t necessarily be <em>cheap</em>. Let’s say that I want to move the window by 50 cm to the right. Would it be up to code? Is there any wiring that needs to be moved? Do I need to consider the placement of the air conditioning unit? What about the emergency escape? Any structural impact?</p><p>This is when we are at the blueprint stage - the equivalent of editing code on screen. And it is obvious that such changes can be <em>really</em> expensive. Similarly, in software, every modification demands a careful assessment of the existing system, long-term maintenance, compatibility with other components, and user expectations.This intricate balancing act is at the core of the engineering discipline.</p><p>A civil engineer designing a bridge faces tangible constraints: the physical world, regulations, budget limitations, and environmental factors like wind, weather, and earthquakes.While software designers might not grapple with physical forces, they contend with equally critical elements such as disk usage, data distribution, rules & regulations, system usability, operational procedures, and the impact of expected future changes.</p><p>Evolving an existing software system presents a substantial engineering challenge.Making significant modifications without causing the system to collapse requires careful planning and execution.The notion that one can simply "start over" or "live deploy" changes is incredibly risky.History is replete with examples of major worldwide outages stemming from seemingly simple configuration changes.A notable instance is <a href="https://status.cloud.google.com/incidents/ow5i3PPK96RduMcb1SsW">the Google outage of June 2025</a>, where a simple missing null check brought down significant portions of GCP. Even small alterations can have cascading and catastrophic effects.</p><p>I’m currently working on a codebase whose age is near the legal drinking age. It also has close to 1.5 million lines of code and a big team operating on it. Being able to successfully run, maintain, and extend that over time requires discipline.</p><p>In such a project, you face issues such as different versions of the software deployed in the field, backward compatibility concerns, etc. For example, I may have a better idea of how to structure the data to make a particular scenario more efficient. That would require updating the on-disk data, which is a 100% engineering challenge. We have to take into consideration physical constraints (updating a multi-TB dataset without downtime is a tough challenge).</p><p>The moment you are actually deployed, you have <em>so many </em>additional concerns to deal with. A good example of this may be that users are <em>used</em> to <a href="https://xkcd.com/1172/">stuff working in a certain way</a>. But even for software that hasn’t been deployed to production yet, the cost of change is <em>high</em>.</p><p>Consider the effort associated with this update to a <code>JobApplication</code> class:</p><p><img src="/blog/Images/Q6aV54vBOui5OWp4Pb1ABA.png"/></p><p>This <em>looks</em> like a simple change, right? It just requires that you (partial list):</p><ul><li>Set up database migration for the new shape of the data.</li><li>Migrate the <em>existing </em>data to the new format.</li><li>Update any indexes and queries on the position.</li><li>Update any endpoints and decide how to deal with backward compatibility.</li><li>Create a new user interface to match this whenever we create/edit/view the job application.</li><li>Consider any existing workflows that inherently assume that a job application is for a <em>single</em> position. </li><li>Can you be <em>partially</em> rejected? What is your status if you interviewed for one position but received an offer for another?</li><li>How does this affect the reports & dashboard? </li></ul><p>This is a <em>simple</em> change, no? Just a few characters on the screen. No physical cost. But it is also a full-blown Epic Task for the project - even if we aren’t in production, have no data to migrate, or integrations to deal with.</p><p>Software engineersoperate under constraints similar to other engineers, including severe consequences for mistakes (global system failure because of a missing null check). Making changes to large, established codebases presents a significant hurdle.</p><p>The moment that you need to consider more than a single factor, whether in your code or in a bridge blueprint, there <em>is</em> a pretty high cost to iterations. Going back to the bridge example, the architect may have a rough idea (is it going to be a Roman-style arch bridge or a suspension bridge) and have a lot of freedom to play with various options at the start. But the moment you begin to nail things down and fill in the details, the cost of change escalates quickly.</p><p>Finally, just to be clear, I don’t think that the cost of changing software is equivalent to changing a bridge after it was built. I simply very strongly disagree that there is zero cost (or indeed, even <em>low</em> cost) to changing software once you are past the “rough draft” stage.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203364-C/the-cost-of-design-iteration-in-software-engineering?Key=619cc0e6-43da-46ff-9cb8-e1ef309e61e1https://ayende.com/blog/203364-C/the-cost-of-design-iteration-in-software-engineering?Key=619cc0e6-43da-46ff-9cb8-e1ef309e61e12025年10月13日 12:00:00 GMTUsing AI for candidate ranking with RavenDB<p>Hiring the right people is notoriously difficult.I have been personally involved in hiring decisions for about two decades, and it is an unpleasant process. You deal with an utterly overwhelming influx of applications, often from candidates using the “spray and pray” approach of applying to <em>all</em> jobs.</p><p>At one point, I got the resume of a <em>divorce lawyer</em> in response to a job posting for a backend engineer role. I was curious enough to follow up on that, and no, that lawyer didn’t want to change careers. He was interested in <em>being</em> a divorce lawyer. What kind of clients would want their divorce handled by a database company, I refrained from asking.</p><p>Companies often resort to <em>expensive </em>external agencies to sift through countless candidates.</p><p>In the age of AI and LLMs, is that still the case? This post will demonstrate how to build an intelligent candidate screening process using RavenDB and modern AI, enabling you to efficiently accept applications, match them to appropriate job postings, and make an initial go/no-go decision for your recruitment pipeline.</p><p>We’ll start our process by defining a couple of open positions:</p><ul><li>Staff Engineer, Backend & DevOps</li><li>Senior Frontend Engineer (React/TypeScript/SaaS)</li></ul><p>Here is what this looks like at the database level:</p><p><img src="/blog/Images/fx1KoGukCHzVhmX1K0tVIg.png"/></p><p>Now, let’s create a couple of applicants for those positions. We have James & Michael, and they look like this:</p><p><img src="/blog/Images/WfowzB467778q8-oXKFP1Q.png"/></p><p>Note that we are not actually doing a lot here in terms of the data we ask the applicant to provide. We mostly gather the contact information and ask them to attach their resume. You can see the resume attachment in RavenDB Studio. In the above screenshot, it is in the right-hand Attachments pane of the document view.</p><p>Now we can use RavenDB’s new <a href="https://github.com/ravendb/ravendb/discussions/21526">Gen AI attachments feature</a>. I defined an OpenAI connection with <code>gpt-4.1-mini</code> and created a Gen AI task to read & understand the resume. I’m assuming that you’ve read my post about <a href="https://ayende.com/blog/202851-C/ravendb-7-1-the-gen-ai-release">Gen AI in RavenDB</a>, so I’ll skip going over the actual setup.</p><p>The key is that I’m applying the following context extraction script to the <code>Applicants </code>collection:</p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'><span class="token keyword">const</span> resumePdf <span class="token operator">=</span> <span class="token function">loadAttachment</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"resume.pdf"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>resumePdf<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
ai<span class="token punctuation">.</span><span class="token function">genContext</span><span class="token punctuation">(</span><span class="token punctuation">{</span>name<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>applicantName<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">withPdf</span><span class="token punctuation">(</span>resumePdf<span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><hr/></p><p>When I test this script on James’s document, I get:</p><p><img src="/blog/Images/urBuojUpKDTA2Zjz1MA5_g.png"/></p><p>Note that we have the attachment in the bottom right - that will <em>also</em> be provided to the model. So we can now write the following prompt for the model:</p><p><hr/><pre class='line-numbers language-bash'><code class='line-numbers language-bash'>You are an HR data parsing specialist. Your task is to analyze the provided CV/resume content <span class="token punctuation">(</span>from the PDF<span class="token punctuation">)</span>
and extract the candidate's professional profile into the provided JSON schema.
In the requiredTechnologies object, every value within the arrays <span class="token punctuation">(</span>languages, frameworks_libraries, etc.<span class="token punctuation">)</span> must be a single,
distinct technology or concept. Do not use slashes <span class="token punctuation">(</span>/<span class="token punctuation">)</span>, commas, semicolons, or parentheses <span class="token punctuation">(</span><span class="token punctuation">)</span> to combine items within a single string. Separate combined concepts into individual strings <span class="token punctuation">(</span>e.g., <span class="token string">"Ruby/Rails"</span> becomes <span class="token string">"Ruby"</span>, <span class="token string">"Rails"</span><span class="token punctuation">)</span>.</code></pre><hr/></p><p>We also ask the model to respond with an object matching the following sample:</p><p><hr/><pre class='line-numbers language-json'><code class='line-numbers language-json'><span class="token punctuation">{</span>
<span class="token property">"location"</span><span class="token operator">:</span> <span class="token string">"The primary location or if interested in remote option (e.g., Pasadena, CA or Remote)"</span><span class="token punctuation">,</span>
<span class="token property">"summary"</span><span class="token operator">:</span> <span class="token string">"A concise overview of the candidate's history and key focus areas (e.g., Lead development of data-driven SaaS applications focusing on React, TypeScript, and Usability)."</span><span class="token punctuation">,</span>
<span class="token property">"coreResponsibilities"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token string">"A list of the primary duties and contributions in previous roles."</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">"requiredTechnologies"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">"languages"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token string">"Key programming and markup languages that the candidate has experience with."</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">"frameworks_libraries"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token string">"Essential UI, state management, testing, and styling libraries."</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">"tools_platforms"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token string">"Version control, cloud platforms, build tools, and project management systems."</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token property">"data_storage"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
<span class="token string">"The database technologies the candidate is expected to work with."</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><hr/></p><p>Testing this on James’s applicant document results in the following output:</p><p><img src="/blog/Images/oUGu2IC2HbXp0Uc7LO0bwQ.png"/></p><p>I actually had to check where the model got the “LA Canada” issue. That <em>shows up</em> in the real resume PDF, and it is a real place. I triple-checked, because I was sure this was a hallucination at first ☺️.</p><p>The last thing we need to do is actually deal with the model’s output. We use an update script to apply the model’s output to the document. In this case, it is as simple as just storing it in the source document:</p><p><hr/><pre class='line-numbers language-php'><code class='line-numbers language-php'>this<span class="token operator">.</span>resume <span class="token operator">=</span> <span class="token variable">$output</span><span class="token punctuation">;</span></code></pre><hr/></p><p>And here is what the output looks like:</p><p><img src="/blog/Images/Nff9I0NkT-SZnbFzYEMuWg.png"/></p><p>Reminder: Gen AI tasks in RavenDB use a three-stage approach:</p><ul><li>Context extraction script - gets data (and attachment) from the source document to provide to the model.</li><li>Prompt & Schema - instructions for the model, telling it what it should <em>do</em> with the provided context and how it should format the output.</li><li>Update script - takes the structured output from the model and applies it back to the source document.</li></ul><p>In our case, this process starts with the applicant uploading their CV, and then we have the <code>Read Resume</code> task running. This parses the PDF and puts the result in the document, which is great, but it is only part of the process.</p><p>We now have the resume contents in a structured format, but we need to <em>evaluate</em> the candidate’s suitability for all the positions they applied for. We are going to do that using the model <em>again</em>, with a new Gen AI task.</p><p>We start by defining the following context extraction script:</p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'><span class="token comment">// wait until the resume (parsed CV) has been added to the document</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span><span class="token keyword">this</span><span class="token punctuation">.</span>resume<span class="token punctuation">)</span> <span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">const</span> positionId of <span class="token keyword">this</span><span class="token punctuation">.</span>targetPosition<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">const</span> position <span class="token operator">=</span> <span class="token function">load</span><span class="token punctuation">(</span>positionId<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>position<span class="token punctuation">)</span> <span class="token keyword">continue</span><span class="token punctuation">;</span>
ai<span class="token punctuation">.</span><span class="token function">genContext</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
position<span class="token punctuation">,</span>
positionId<span class="token punctuation">,</span>
resume<span class="token punctuation">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>resume
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre><hr/></p><p>Note that this relies on the <code>resume </code>field that we created in the previous task. In other words, we set things up in such a way that we run this task <em>after</em> the <code>Read Resume</code> task, but without needing to put them in an explicit pipeline or manage their execution order.</p><p>Next, note that we output <em>multiple </em>contexts for the same document. Here is what this looks like for James, we have two <em>separate</em> contexts, one for each position James applied for:</p><p><img src="/blog/Images/pMhfqVPKDiUQtdWoNCDpSg.png"/></p><p>This is important because we want to process each position and resume <em>independently</em>. This avoids context leakage from one position to another. It also lets us process multiple positions for the same applicant <em>concurrently</em>.</p><p>Now, we need to tell the model what it is supposed to do:</p><p><hr/><pre class='line-numbers language-lua'><code class='line-numbers language-lua'>You are a specialized HR Matching AI<span class="token punctuation">.</span> Your task is to receive two structured JSON objects — one describing a Job Position <span class="token keyword">and</span> one
summarizing a Candidate Resume — <span class="token keyword">and</span> evaluate the suitability of the resume <span class="token keyword">for</span> the position<span class="token punctuation">.</span>
Assess the overlap <span class="token keyword">in</span> jobTitle<span class="token punctuation">,</span> summary<span class="token punctuation">,</span> <span class="token keyword">and</span> coreResponsibilities<span class="token punctuation">.</span> Does the candidate<span class="token string">'s career trajectory align with the role'</span>s <span class="token function">needs</span> <span class="token punctuation">(</span>e<span class="token punctuation">.</span>g<span class="token punctuation">.</span><span class="token punctuation">,</span> has matching experience required <span class="token keyword">for</span> a Senior Frontend role<span class="token punctuation">)</span>?
Technical Match<span class="token punctuation">:</span> Compare the technologies listed <span class="token keyword">in</span> the requiredTechnologies sections<span class="token punctuation">.</span> Identify both direct <span class="token function">matches</span> <span class="token punctuation">(</span>must<span class="token operator">-</span>haves<span class="token punctuation">)</span> <span class="token keyword">and</span> <span class="token function">gaps</span> <span class="token punctuation">(</span>missing <span class="token keyword">or</span> weak areas<span class="token punctuation">)</span><span class="token punctuation">.</span> Consider substitutions such as js <span class="token keyword">or</span> ecmascript to javascript <span class="token keyword">or</span> node<span class="token punctuation">.</span>js<span class="token punctuation">.</span>
Evaluate <span class="token keyword">if</span> the candidate's experience level <span class="token keyword">and</span> domain <span class="token function">expertise</span> <span class="token punctuation">(</span>e<span class="token punctuation">.</span>g<span class="token punctuation">.</span><span class="token punctuation">,</span> SaaS<span class="token punctuation">,</span> Data Analytics<span class="token punctuation">,</span> Mapping Solutions<span class="token punctuation">)</span> meet <span class="token keyword">or</span> exceed the requirements<span class="token punctuation">.</span></code></pre><hr/></p><p>And the output schema that we want to get from the model is:</p><p><hr/><pre class='line-numbers language-json'><code class='line-numbers language-json'><span class="token punctuation">{</span>
<span class="token property">"explanation"</span><span class="token operator">:</span> <span class="token string">"Provide a detailed analysis here. Start by confirming the high-level match (e.g., 'The candidate is an excellent match because...'). Detail the strongest technical overlaps (e.g., React, TypeScript, Redux, experience with BI/SaaS). Note any minor mismatches or significant overqualifications (e.g., candidate's deep experience in older technologies like ASP.NET classic is not required but demonstrates full-stack versatility)."</span><span class="token punctuation">,</span> <span class="token property">"isSuitable"</span><span class="token operator">:</span> <span class="token boolean">false</span>
<span class="token punctuation">}</span></code></pre><hr/></p><p>Here I want to stop for a moment and talk about what exactly we are doing here. We <em>could</em> ask the model just to judge whether an applicant is suitable for a position and save a bit on the number of tokens we spend. However, getting just a yes/no response from the model is not something I recommend.</p><p>There are two primary reasons why we want the explanation field as well. First, it serves as a good check on the model itself. The <em>order</em> of properties matters in the output schema. We first ask the model to explain itself, then to render the verdict. That means it is going to be more focused. </p><p>The other reason is a bit more delicate. You may be <em>required</em> to provide an explanation to the applicant if you reject them. I won’t necessarily put this exact justification in the rejection letter to the applicant, but it is something that is quite important to retain in case you need to provide it later.</p><p>Going back to the task itself, we have the following update script:</p><p><hr/><pre class='line-numbers language-php'><code class='line-numbers language-php'>this<span class="token operator">.</span>suitability <span class="token operator">=</span> this<span class="token operator">.</span><span class="token class-name">suitability</span> <span class="token operator">||</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
this<span class="token operator">.</span>suitability<span class="token punctuation">[</span><span class="token variable">$input</span><span class="token operator">.</span>positionId<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token variable">$output</span><span class="token punctuation">;</span></code></pre><hr/></p><p>Here we are doing something quite interesting. We extracted the <code>positionId</code> at the start of this process, and we are using it to associate the output from the model with the specific position we are currently evaluating.</p><p>Note that we are actually evaluating multiple positions for the same applicant at the same time, and we need to execute this update script for <em>each</em> of them. So we need to ensure that we don’t overwrite previous work. </p><blockquote><p>I’m not mentioning this in detail because I covered it in <a href="https://ayende.com/blog/202851-C/ravendb-7-1-the-gen-ai-release">my previous Gen AI post</a>, but it is important to note that we have <em>two</em> tasks sourced from the same document. RavenDB knows how to handle the data being modified by both tasks without triggering an infinite loop. It seems like a small thing, but it is the sort of thing that <em>not</em> having to worry about really simplifies the whole process.</p></blockquote><p>With these two tasks, we have now set up a complete pipeline for the initial processing of applicants to open positions. As you can see here:</p><p><img src="/blog/Images/UJye4HLxVnussV8N7hMA_g.png"/></p><p>This sort of process allows you to integrate into your system stuff that, until recently, looked like science fiction. A pipeline like the one above is not something you could just build before, but now you can spend a few hours and have this capability ready to deploy.</p><p>Here is what the tasks look like inside RavenDB:</p><p><img src="/blog/Images/q73Fk5q3NfPuM4rkEVYhyg.png"/></p><p>And the final applicant document after all of them have run is:</p><p><img src="/blog/Images/I5ERv44KePhzPaVoGsdwLw.png"/></p><p>You can see the metadata for the two tasks (which we use to avoid going to the model again when we don’t have to), as well as the actual outputs of the model (<code>resume</code>, <code>suitability</code> fields).</p><p>A few more notes before we close this post. I chose to use two GenAI tasks here, one to read the resume and generate the structured output, and the second to actually evaluate the applicant’s suitability.</p><p>From a modeling perspective, it is easier to split this into distinct steps. You <em>can</em> ask the model to both read the resume and evaluate suitability in a single shot, but I find that it makes it harder to extend the system down the line.</p><p>Another reason you want to have different tasks for this is that you can use <em>different</em> models for each one. For example, reading the resume and extracting the structured output is something you can run on <code>gpt-4.1-mini</code> or <code>gpt-5-nano,</code> while evaluating applicant suitability can make use of a smarter model.</p><p>I’m really happy with the new RavenDB AI integration features. We got some early feedback that is <em>really</em> exciting, and I’m looking forward to seeing what you can do with them.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203363-C/using-ai-for-candidate-ranking-with-ravendb?Key=e63b9a8f-6547-4b36-ab7a-f634938a8e09https://ayende.com/blog/203363-C/using-ai-for-candidate-ranking-with-ravendb?Key=e63b9a8f-6547-4b36-ab7a-f634938a8e092025年10月10日 12:00:00 GMTWhen perf optimization breaks tests in a GOOD way<p>You might have noticed a theme going on in RavenDB. We <em>care</em> a lot about performance. The problem with optimizing performance is that sometimes you have a great idea, you implement it, the performance gains are <em>there</em> to be had - and then a test fails… and you realize that your great idea now needs to be 10 times more complex to handle a niche edge case.</p><p>We did a <em>lot</em> of work around optimizing the performance of RavenDB at the lowest levels for the next major release (8.0), and we got a persistently failing test that we started to look at.</p><p>Here is the failing message:</p><blockquote><p>Restore with MaxReadOpsPerSecond = 1 should take more than '11' seconds, but it took '00:00:09.9628728'</p></blockquote><p>The test in question is <code>ShouldRespect_Option_MaxReadOpsPerSec_OnRestore</code>, part of the <code>MaxReadOpsPerSecOptionTests </code>suite of tests. What it tests is that we can <em>limit </em>how fast RavenDB can restore a database. </p><p>The reason you want to do that is to avoid consuming too many system resources when performing a big operation. For example, I may want to restore a <em>big</em> database, but I don’t want to consume all the IOPS on the server, because there are additional databases running on it.</p><p>At any rate, we started to get test failures on this test. And a deeper investigation revealed something quite amusing. We made the entire system more efficient. In particular, we managed to reduce the size of the buffers used significantly, so we can push more data faster. It turns out that this is enough to break the test.</p><p>The fix was to reduce the actual time that we budget as the minimum viable time. And I have to say that this is one of those pull requests that lights a warm fire in my heart.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203331-C/when-perf-optimization-breaks-tests-in-a-good-way?Key=593060c0-2e46-43d4-bb16-f6cb89abd738https://ayende.com/blog/203331-C/when-perf-optimization-breaks-tests-in-a-good-way?Key=593060c0-2e46-43d4-bb16-f6cb89abd7382025年10月07日 12:00:00 GMTRecording: How To Run AI Agents Natively In Your Database<p><iframe width="1840" height="1035" title="How To Run AI Agents Natively In Your Database, LIVE Webinar with RavenDB's CEO, Oren Eini | RavenDB" src="https://www.youtube.com/embed/A17GSLGN-cQ" frameborder="0" allowfullscreen="" referrerpolicy="strict-origin-when-cross-origin" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"></iframe></p><p><br></p><p>AI agents are only as powerful as their connection to data. In this session, Oren Eini, CEO and Co-Founder of RavenDB, demonstrates why the best place for AI agents to live is inside your database. Moderated by Ariel, Director of Product Marketing at RavenDB, the webinar explores how to eliminate orchestration complexity, keep agents safe, and unlock production-ready AI with minimal code. </p><p>You’ll see how RavenDB integrates embeddings and vector search directly into the database, runs generative AI tasks such as translation and summarization on your documents, and defines AI agents that can query and act on your data safely. Learn how to scope access, prevent hallucinations, and use AI agents to handle HR queries, payroll checks, and issue escalations. </p><p>Discover how RavenDB supports any LLM provider (OpenAI, DeepSeek, Ollama, and more), works seamlessly on the edge or in the cloud, and gives developers a fast path from prototype to production without a tangle of external services. This session shows how to move beyond chatbots into real, action-driven agents that are reliable, predictable, and simple to extend. If you’re exploring AI-driven applications, this is where to start.</p>https://ayende.com/blog/203299-B/recording-how-to-run-ai-agents-natively-in-your-database?Key=e92a1e04-0ea1-4ffa-9f1a-ada54c3e0612https://ayende.com/blog/203299-B/recording-how-to-run-ai-agents-natively-in-your-database?Key=e92a1e04-0ea1-4ffa-9f1a-ada54c3e06122025年9月29日 12:00:00 GMTCryptographic documents in RavenDB<p>We got an interesting use case from a customer - they need to <em>verify</em> that documents in RavenDB have not been modified by any external party, including users with administrator credentials for the database.</p><p>This is known as the Rogue Root problem, where you have to protect yourself from potentially malicious root users. That is <em>not</em> an easy problem - in theory, you can safeguard yourself <a href="https://techcommunity.microsoft.com/blog/exchange/protecting-against-rogue-administrators/585155">using various means</a>, for example the whole premise of SELinux is based on that. </p><p>I don’t really like that approach, since I assume that if a user has (valid) root access, they also likely have <em>physical access</em>. In other words, they can change the operating system to bypass any hurdles in the way.</p><p>Luckily, the scenario we were presented with involved detecting changes made by an administrator, which is significantly easier. And we can also use some cryptography tools to help us handle even the case of detecting <em>malicious</em> tampering.</p><p>First, I’m going to show how to make this work with RavenDB, then we’ll discuss the implications of this approach for the overall security of the system.</p><h1>The implementation</h1><p>The RavenDB client API allows you to hook into the saving process of documents, as you can see in the code below. In this example, I’m using a user-specific <code>ECDsa </code>key (by calling the <code>GetSigningKeyForUser()</code> method).</p><p><hr/><pre class='line-numbers language-javascript'><code class='line-numbers language-javascript'>store<span class="token punctuation">.</span>OnBeforeStore <span class="token operator">+=</span> <span class="token punctuation">(</span><span class="token parameter">sender<span class="token punctuation">,</span> e</span><span class="token punctuation">)</span> <span class="token operator">=></span>
<span class="token punctuation">{</span>
using <span class="token keyword">var</span> obj <span class="token operator">=</span> e<span class="token punctuation">.</span>Session<span class="token punctuation">.</span>JsonConverter<span class="token punctuation">.</span><span class="token function">ToBlittable</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>Entity<span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> date <span class="token operator">=</span> DateTime<span class="token punctuation">.</span>UtcNow<span class="token punctuation">.</span><span class="token function">ToString</span><span class="token punctuation">(</span><span class="token string">"O"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> data <span class="token operator">=</span> Encoding<span class="token punctuation">.</span><span class="token constant">UTF8</span><span class="token punctuation">.</span><span class="token function">GetBytes</span><span class="token punctuation">(</span> e<span class="token punctuation">.</span>DocumentId <span class="token operator">+</span> date <span class="token operator">+</span> obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
using ECDsa key <span class="token operator">=</span> <span class="token function">GetSigningKeyForUser</span><span class="token punctuation">(</span>CurrentUser<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> signData <span class="token operator">=</span> key<span class="token punctuation">.</span><span class="token function">SignData</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> HashAlgorithmName<span class="token punctuation">.</span><span class="token constant">SHA256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
e<span class="token punctuation">.</span>DocumentMetadata<span class="token punctuation">[</span><span class="token string">"DigitalSignature"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Dictionary</span><span class="token operator"><</span>string<span class="token punctuation">,</span> string<span class="token operator">></span>
<span class="token punctuation">{</span>
<span class="token punctuation">[</span><span class="token string">"User"</span><span class="token punctuation">]</span> <span class="token operator">=</span> CurrentUser<span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">"Signature"</span><span class="token punctuation">]</span> <span class="token operator">=</span> Convert<span class="token punctuation">.</span><span class="token function">ToBase64String</span><span class="token punctuation">(</span>signData<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">"Date"</span><span class="token punctuation">]</span> <span class="token operator">=</span> date<span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">"PublicKey"</span><span class="token punctuation">]</span> <span class="token operator">=</span> key<span class="token punctuation">.</span><span class="token function">ExportSubjectPublicKeyInfoPem</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span></code></pre><hr/></p><p>What you can see here is that we are using the user’s key to generate a signature that is composed of:</p><ul><li>The document’s ID.</li><li>The current signature time.</li><li>The JSON content of the entity.</li></ul><p>After we generate the signature, we add it to the document’s metadata. This allows us to verify that the entity is indeed valid and was signed by the proper user. </p><p>To validate this afterward, we use the following code:</p><p><hr/><pre class='line-numbers language-javascript'><code class='line-numbers language-javascript'>bool ValidateEntity<span class="token operator"><</span><span class="token constant">T</span><span class="token operator">></span><span class="token punctuation">(</span>IAsyncDocumentSession session<span class="token punctuation">,</span><span class="token constant">T</span> entity<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">var</span> metadata <span class="token operator">=</span> session<span class="token punctuation">.</span>Advanced<span class="token punctuation">.</span><span class="token function">GetMetadataFor</span><span class="token punctuation">(</span>entity<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> documentId <span class="token operator">=</span> session<span class="token punctuation">.</span>Advanced<span class="token punctuation">.</span><span class="token function">GetDocumentId</span><span class="token punctuation">(</span>entity<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> digitalSignature <span class="token operator">=</span> metadata<span class="token punctuation">.</span><span class="token function">GetObject</span><span class="token punctuation">(</span><span class="token string">"DigitalSignature"</span><span class="token punctuation">)</span> <span class="token operator">??</span>
<span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">IOException</span><span class="token punctuation">(</span><span class="token string">"Signature is missing for "</span> <span class="token operator">+</span> documentId<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> date <span class="token operator">=</span> digitalSignature<span class="token punctuation">.</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">"Date"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> user <span class="token operator">=</span> digitalSignature<span class="token punctuation">.</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">"User"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> signature <span class="token operator">=</span> digitalSignature<span class="token punctuation">.</span><span class="token function">GetString</span><span class="token punctuation">(</span><span class="token string">"Signature"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
using <span class="token keyword">var</span> key <span class="token operator">=</span> <span class="token function">GetPublicKeyForUser</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">;</span>
using <span class="token keyword">var</span> obj <span class="token operator">=</span> session<span class="token punctuation">.</span>Advanced<span class="token punctuation">.</span>JsonConverter<span class="token punctuation">.</span><span class="token function">ToBlittable</span><span class="token punctuation">(</span>entity<span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> data <span class="token operator">=</span> Encoding<span class="token punctuation">.</span><span class="token constant">UTF8</span><span class="token punctuation">.</span><span class="token function">GetBytes</span><span class="token punctuation">(</span>documentId <span class="token operator">+</span> date <span class="token operator">+</span> obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> bytes <span class="token operator">=</span> Convert<span class="token punctuation">.</span><span class="token function">FromBase64String</span><span class="token punctuation">(</span>signature<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> key<span class="token punctuation">.</span><span class="token function">VerifyData</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> bytes<span class="token punctuation">,</span> HashAlgorithmName<span class="token punctuation">.</span><span class="token constant">SHA256</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre><hr/></p><p>Note that here, too, we are using the <code>GetPublicKeyForUser</code><code>()</code> to get the proper public key to validate the signature. We use the specified user from the metadata to get the key, and we verify the signature against the document ID, the date in the metadata, and the JSON of the entity.</p><p>We are also saving the <em>public </em>key of the signing user in the metadata. But we haven’t used it so far, why are we doing this? </p><p>The reason we use <code>GetPublicKeyForUser()</code> in the <code>ValidateEntity()</code> call is pretty simple: we want to get the user’s key from the same source. This assumes that the user’s key is stored in a safe location (a secure vault or a hardware key like YubiKey, etc.). </p><p>The reason we want to store the public key in the metadata is so we can verify the data on the <em>server</em> side. I created the following index:</p><p><hr/><pre class='line-numbers language-javascript'><code class='line-numbers language-javascript'>from c <span class="token keyword">in</span> docs<span class="token punctuation">.</span>Companies
<span class="token keyword">let</span> unverified <span class="token operator">=</span> Crypto<span class="token punctuation">.</span><span class="token function">Verify</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span>
where unverified is not <span class="token keyword">null</span>
select <span class="token keyword">new</span>
<span class="token punctuation">{</span>
Problem <span class="token operator">=</span> unverified
<span class="token punctuation">}</span></code></pre><hr/></p><p>I’m using <a href="https://docs.ravendb.net/7.1/indexes/extending-indexes/">RavenDB’s additional sources</a> feature to add <a href="https://gist.github.com/ayende/050c1a9dd6a484a53354f3d41efa9464">the following code</a> to the index. This exposes the <code>Crypto.Verify()</code> call to the index, and the code uses the public key in the metadata (as well as the other information there) to verify that the document signature is valid.</p><p>The index code above will filter all the documents whose signature is valid, so you can easily get all the problematic documents. In other words, it is a quick way of saying: “Find me all the documents whose verification failed”. For compliance, that is quite important and usually requires going over the entire dataset to answer it.</p><h1>The implications</h1><p>Let’s consider the impact of such a system. We now have cryptographic verification that the document was modified by a specific user. Any tampering with the document will invalidate the digital signature (or require signing it with your key).</p><p>Combine that with <a href="https://docs.ravendb.net/7.0/document-extensions/revisions/overview/">RavenDB’s revisions</a>, and you have an immutable log that you can verify using modern cryptography. No, it isn’t a blockchain, but it will put a significant roadblock in the path of anyone trying to just modify the data. </p><p>The fact that we do the signing on the client side, rather than the server, means that the server never actually has <em>access</em> to the signing keys (only the public keys). The server’s administrator, in the same manner, doesn’t have a way to get those signing keys and forge a document.</p><p>In other words, we solved the Rogue Root problem, and we ensured that a user cannot repudiate a document they signed. It is easy to audit the system for invalid documents (and, combined with revisions, go back to a valid one).</p><h3>Escape hatch design</h3><p>If you need this sort of feature for compliance only, you may want to skip the <code>ValidateEntity()</code> call. That would allow an administrator to manually change a document (thus invalidating the digital signature) and still have the rest of the system work. That goes against what we are trying to do, yes, but it is sometimes desirable.</p><p>That isn’t required for the normal course of operations, but it <em>can</em> be required for troubleshooting, for example. I’m sure you can think of a number of reasons why it would make things a lot easier to fix if you could just modify the database’s data.</p><p>For example, an <code>Order</code> contains a <code>ZipCode</code> with the value <code>"02116"</code> (note the leading zero), which a downstream system turns into the integer <code>02116</code>. An administrator can change the value to be <code>" 02116"</code>, with a leading space, preventing this problem (the downstream system will not convert this to a number, thus keeping the leading 0). Silly, yes - but it happens all the time.</p><p>Even though we are invalidating the digital signature, we may want to do that anyway. The index we defined would alert on this, but we can proceed with processing the order, then fix it up later. Or just make a note of this for compliance purposes. </p><h1>Summary</h1><p>This post walks you through building a cryptographic solution to protect document integrity within a RavenDB environment, addressing the Rogue Root problem. The core mechanism is a client-side <code>OnBeforeStore </code>hook that generates an <code>ECDsa</code> digital signature for each document. This design ensures that the private keys are never exposed on the server, preventing a database administrator from forging signatures and providing true non-repudiation.</p><p>A RavenDB index is used to automatically and asynchronously verify every document's signature against its current content. This index filters for any documents where the digital signature is valid, providing an efficient server-side audit mechanism to find all the documents with invalid signatures.</p><p>The really fun part here is that there isn’t really a lot of code or complexity involved, and you get strong cryptographic proof that your data has not been tampered with.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203300-B/cryptographic-documents-in-ravendb?Key=913acc58-fb7d-4f8f-bccd-298ff20858cahttps://ayende.com/blog/203300-B/cryptographic-documents-in-ravendb?Key=913acc58-fb7d-4f8f-bccd-298ff20858ca2025年9月26日 12:00:00 GMTRecording: How To Create Powerful and Secure AI Agents with RavenDB<p><iframe width="1840" height="1035" title="[LIVE] COD#6 How To Create Powerful and Secure AI Agents with RavenDB CEO, Oren Eini | RavenDB" src="https://www.youtube.com/embed/jzUxL9P17G4" frameborder="0" allowfullscreen="" referrerpolicy="strict-origin-when-cross-origin" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"></iframe></p><p><br></p><p>Unlock practical AI agents inside your database. In this live demo and deep dive, Oren Eini shows how to build real, production-ready AI agents directly in RavenDB that query your data, take actions, remember context, and stay inside strict security guardrails. You will see an agent defined in a few lines of code, connected to OpenAI or any LLM you choose, running vector search and RAG over your catalog, and safely executing business actions like "add to cart," "find policies," or "sign document," all with parameters that are enforced by the database rather than trusted to the model. You will learn how RavenDB agents eliminate fragile glue code by giving the model explicit tools: data queries that return typed results and server-side actions you validate in your code. </p><p>Conversations are stored as documents, with automatic token-aware summarization to control latency and cost. The demo streams responses token by token for responsive UX, switches models without rewrites, and shows how scope parameters prevent data leaks even if the prompt is manipulated. You will also see a multi-tool HR assistant that chains tools, coordinates front end and back end, and persists state. The session closes with a look at the roadmap, including multi-agent orchestration and AI assist inside Studio.</p>https://ayende.com/blog/203267-A/recording-how-to-create-powerful-and-secure-ai-agents-with-ravendb?Key=4f5a9a1c-96bc-4607-aced-64991f6d0927https://ayende.com/blog/203267-A/recording-how-to-create-powerful-and-secure-ai-agents-with-ravendb?Key=4f5a9a1c-96bc-4607-aced-64991f6d09272025年9月22日 12:00:00 GMTScheduling with RavenDB<p>I got a question from one of our users about how they can use RavenDB to manage scheduled tasks. Stuff like: “Send this email next Thursday” or “Cancel this reservation if the user didn’t pay within 30 minutes.”</p><p>As you can tell from the context, this is both more straightforward and more complex than the “run this every 2nd Wednesday" you’ll typically encounter when talking about scheduled jobs.</p><p>The answer for how to do that in RavenDB is pretty simple, you use <a href="https://docs.ravendb.net/7.1/server/extensions/refresh">the Document Refresh feature</a>. This is a really <em>tiny </em>feature when you consider what it does. Given this document:</p><p><hr/><pre class='line-numbers language-json'><code class='line-numbers language-json'><span class="token punctuation">{</span>
<span class="token property">"Redacted"</span><span class="token operator">:</span> <span class="token string">"Details"</span><span class="token punctuation">,</span>
<span class="token property">"@metdata"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
<span class="token property">"@collection"</span><span class="token operator">:</span> <span class="token string">"RoomAvailabilities"</span><span class="token punctuation">,</span>
<span class="token property">"@refresh"</span><span class="token operator">:</span> <span class="token string">"2025-09-14T10:00:00.0000000Z"</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre><hr/></p><p>RavenDB will remove the <code>@refresh </code>metadata field at the specified time. That is <em>all</em> this does, nothing else. That looks like a pretty useless feature, I admit, but there is a point to it.</p><p>The act of removing the <code>@refresh</code> field from the document will also (obviously) update the document, which means that everything that reacts to a document update will also react to this.</p><p><a href="https://www.ayende.com/blog/195393-A/ravendb-subscriptions-messaging-patterns">I wrote about this in the past</a>, but it turns out there are a <em>lot</em> of interesting things you can do with this. For example, consider the following index definition:</p><p><hr/><pre class='line-numbers language-go'><code class='line-numbers language-go'>from RoomAvailabilitiesas r
where <span class="token boolean">true</span> and not <span class="token function">exists</span><span class="token punctuation">(</span>r<span class="token punctuation">.</span><span class="token string">"@metadata"</span><span class="token punctuation">.</span><span class="token string">"@refresh"</span><span class="token punctuation">)</span>
<span class="token keyword">select</span> <span class="token builtin">new</span> <span class="token punctuation">{</span>
r<span class="token punctuation">.</span>RoomId<span class="token punctuation">,</span>
r<span class="token punctuation">.</span>Date<span class="token punctuation">,</span>
<span class="token comment">// etc...</span>
<span class="token punctuation">}</span></code></pre><hr/></p><p>What you see here is an index that lets me “hide” documents (that were reserved) until that reservation expires. </p><p>I can do quite a lot with this feature. For example, use this in RabbitMQ ETL to build automatic delayed sending of documents. Let’s implement a “dead-man switch”, a document will be automatically sent to a RabbitMQ channel if a server doesn’t contact us often enough:</p><p><hr/><pre class='line-numbers language-javascript'><code class='line-numbers language-javascript'><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">[</span><span class="token string">'@metadata'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">"@refresh"</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">return</span><span class="token punctuation">;</span> <span class="token comment">// no need to send if refresh didn't expire</span>
<span class="token keyword">var</span> alertData <span class="token operator">=</span> <span class="token punctuation">{</span>
<span class="token literal-property property">Id</span><span class="token operator">:</span> <span class="token function">id</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token literal-property property">ServerId</span><span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>ServerId<span class="token punctuation">,</span>
<span class="token literal-property property">LastUpdate</span><span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>Timestamp<span class="token punctuation">,</span>
<span class="token literal-property property">LastStatus</span><span class="token operator">:</span> <span class="token keyword">this</span><span class="token punctuation">.</span>Status <span class="token operator">||</span> <span class="token string">'ACTIVE'</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token function">loadToAlertExchange</span><span class="token punctuation">(</span>alertData<span class="token punctuation">,</span> <span class="token string">'alert.operations'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
<span class="token literal-property property">Id</span><span class="token operator">:</span> <span class="token function">id</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token literal-property property">Type</span><span class="token operator">:</span> <span class="token string">'operations.alerts.missing_heartbeat'</span><span class="token punctuation">,</span>
<span class="token literal-property property">Source</span><span class="token operator">:</span> <span class="token string">'/operations/server-down/no-heartbeat'</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><hr/></p><p>The idea is that whenever a server contacts us, we’ll update the <code>@refresh</code> field to the maximum duration we are willing to miss updates from the server. If that time expires, RavenDB will remove the <code>@refresh</code> field, and the RabbitMQ ETL script will send an alert to the RabbitMQ exchange. You’ll note that this is actually reacting to <em>inaction</em>, which is a surprisingly hard thing to actually do, usually.</p><blockquote><p>You’ll notice that, like many things in RavenDB, most features tend to be small and focused. The idea is that they compose well together and let you build the behavior you need with a very low complexity threshold.</p></blockquote><p>The common use case for <code>@refresh</code> is when you use <a href="https://docs.ravendb.net/7.1/client-api/data-subscriptions/what-are-data-subscriptions/">RavenDB Data Subscriptions</a> to process documents. For example, you want to send an email in a week. This is done by writing an EmailToSend document with a <code>@refresh</code> of a week from now and defining a subscription with the following query:</p><p><hr/><pre class='line-numbers language-julia'><code class='line-numbers language-julia'>from EmailToSend as e
where <span class="token boolean">true</span> and not exists<span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token operator">'</span>@metadata<span class="token operator">'</span><span class="token punctuation">.</span><span class="token operator">'</span>@refresh<span class="token operator">'</span><span class="token punctuation">)</span></code></pre><hr/></p><p>In other words, we simply filter out those that have a <code>@refresh </code>field, it’s that simple. Then, in your code, you can ignore the scheduling aspect entirely. Here is what this looks like:</p><p><hr/><pre class='line-numbers language-dart'><code class='line-numbers language-dart'><span class="token keyword">var</span> subscription <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">store<span class="token punctuation">.</span></span>Subscriptions
.GetSubscriptionWorker</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">EmailToSend</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"EmailToSendSubscription"</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">await</span> <span class="token class-name"><span class="token namespace">subscription<span class="token punctuation">.</span></span>Run</span><span class="token punctuation">(</span><span class="token keyword">async</span> batch <span class="token operator">=</span><span class="token operator">></span>
<span class="token punctuation">{</span>
using <span class="token keyword">var</span> session <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">batch<span class="token punctuation">.</span></span>OpenAsyncSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
foreach <span class="token punctuation">(</span><span class="token keyword">var</span> item <span class="token keyword">in</span> <span class="token class-name"><span class="token namespace">batch<span class="token punctuation">.</span></span>Items</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">var</span> email <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">item<span class="token punctuation">.</span></span>Result</span><span class="token punctuation">;</span>
<span class="token keyword">await</span> <span class="token class-name">EmailProvider.SendEmailAsync</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">EmailMessage</span>
<span class="token punctuation">{</span>
<span class="token class-name">To</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">email<span class="token punctuation">.</span></span>To</span><span class="token punctuation">,</span>
<span class="token class-name">Subject</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">email<span class="token punctuation">.</span></span>Subject</span><span class="token punctuation">,</span>
<span class="token class-name">Body</span> <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">email<span class="token punctuation">.</span></span>Body</span><span class="token punctuation">,</span>
<span class="token class-name">From</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"no-reply@example.com"</span></span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name"><span class="token namespace">email<span class="token punctuation">.</span></span>Status</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Sent"</span></span><span class="token punctuation">;</span>
<span class="token class-name"><span class="token namespace">email<span class="token punctuation">.</span></span>SentAt</span> <span class="token operator">=</span> <span class="token class-name">DateTime.UtcNow</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">await</span> <span class="token class-name"><span class="token namespace">session<span class="token punctuation">.</span></span>SaveChangesAsync</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><hr/></p><p>Note that nothing in this code handles scheduling. RavenDB is in charge of sending the documents to the subscription when the time expires.</p><p>Using <code>@refresh</code> + Subscriptions in this manner provides us with a number of interesting advantages:</p><ul><li><strong>Missed Triggers</strong>: Handles missed schedules seamlessly, resuming on the next subscription run.</li><li><strong>Reliability</strong>: Automatically retries subscription processing on errors.</li><li><strong>Rescheduling</strong>: When <code>@refresh </code>expires, your subscription worker will get the document and can decide to act or reschedule a check by updating the <code>@refresh</code> field again.</li><li><strong>Robustness</strong>: You can rely on RavenDB to keep serving subscriptions even if nodes (both clients & servers) fail.</li><li><strong>Scaleout</strong>: You can use concurrent subscriptions to have multiple workers read from the same subscription.</li></ul><p>You can take this approach really far, in terms of load, throughput, and complexity. The nice thing about this setup is that you don’t need to glue together cron, a message queue, and worker management. You can let RavenDB handle it all for you.</p>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css" integrity="sha512-/mZ1FHPkg6EKcxo0fKXF51ak6Cr2ocgDi5ytaTBjsQZIH/RNs6GF6+oId/vPe3eJB836T36nXwVh/WBl/cWT4w==" crossorigin="anonymous" referrerpolicy="no-referrer" />https://ayende.com/blog/203203-B/scheduling-with-ravendb?Key=bec80bdd-3afc-4a81-97ab-c83f0c0e4955https://ayende.com/blog/203203-B/scheduling-with-ravendb?Key=bec80bdd-3afc-4a81-97ab-c83f0c0e49552025年9月18日 12:00:00 GMT