From 05dac97920febbcdd4bbc34d3b1c1adaf586f169 Mon Sep 17 00:00:00 2001 From: reebhub Date: Wed, 16 Jul 2025 16:54:49 +0300 Subject: [PATCH] GenAI documentation - fix by review comments --- .../gen-ai-integration/gen-ai-api.markdown | 8 +- .../AiIntegration/GenAI/GenAI.cs | 80 ++++++++++++++++++- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/Documentation/7.1/Raven.Documentation.Pages/ai-integration/gen-ai-integration/gen-ai-api.markdown b/Documentation/7.1/Raven.Documentation.Pages/ai-integration/gen-ai-integration/gen-ai-api.markdown index 863e208f8c..e00b8334a1 100644 --- a/Documentation/7.1/Raven.Documentation.Pages/ai-integration/gen-ai-integration/gen-ai-api.markdown +++ b/Documentation/7.1/Raven.Documentation.Pages/ai-integration/gen-ai-integration/gen-ai-api.markdown @@ -64,7 +64,10 @@ * Define a GenAI task using a `GenAiConfiguration` object. * Run the task using `AddGenAiOperation`. -{CODE:csharp gen-ai_define-gen-ai-task@AiIntegration\GenAI\GenAI.cs /} +{CODE-TABS} +{CODE-TAB:csharp:use-sample-object gen-ai_define-gen-ai-task_use-sample-object@AiIntegration\GenAI\GenAI.cs /} +{CODE-TAB:csharp:use-json-schema gen-ai_define-gen-ai-task_use-json-schema@AiIntegration\GenAI\GenAI.cs /} +{CODE-TABS/} --- @@ -79,7 +82,8 @@ | **Collection** | `string` | Name of the document collection associated with the task | | **GenAiTransformation** | `GenAiTransformation` | Context generation script - format for objects to be sent to the AI model | | **Prompt** | `string` | AI model Prompt - the instructions sent to the AI model | -| **SampleObject** | `string` | JSON schema - a sample response object to format AI model replies by | +| **SampleObject** | `string` | A [sample response object](../../ai-integration/gen-ai-integration/gen-ai-overview#the-elements_json-schema) to format the AI model's replies by
If both a `SampleObject` and a `JsonSchema` are provided the schema takes precedence | +| **JsonSchema** | `string` | A [JSON schema](../../ai-integration/gen-ai-integration/gen-ai-overview#the-elements_json-schema) to format the AI model's replies by
If both a `SampleObject` and a `JsonSchema` are provided the schema takes precedence | | **UpdateScript** | `string` | Update script - specifies what to do with AI model replies | | **MaxConcurrency** | `int` | Max concurrent connections to the AI model (each connection serving a single context object | diff --git a/Documentation/7.1/Samples/csharp/Raven.Documentation.Samples/AiIntegration/GenAI/GenAI.cs b/Documentation/7.1/Samples/csharp/Raven.Documentation.Samples/AiIntegration/GenAI/GenAI.cs index 6d69d1f43a..69ea16347a 100644 --- a/Documentation/7.1/Samples/csharp/Raven.Documentation.Samples/AiIntegration/GenAI/GenAI.cs +++ b/Documentation/7.1/Samples/csharp/Raven.Documentation.Samples/AiIntegration/GenAI/GenAI.cs @@ -7,6 +7,7 @@ using Raven.Client.Documents.Operations.ConnectionStrings; using Raven.Client.Documents.Operations.ETL; using Sparrow.Json; +using static Akka.Streams.Attributes; using static Akka.Streams.Implementation.Fusing.GraphInterpreter; namespace Raven.Documentation.Samples.AiIntegration.ConnectionStrings; @@ -65,7 +66,7 @@ public async Task Examples() using (var store = new DocumentStore()) { - #region gen-ai_define-gen-ai-task + #region gen-ai_define-gen-ai-task_use-sample-object GenAiConfiguration config = new GenAiConfiguration { // Task name @@ -94,7 +95,7 @@ public async Task Examples() // AI model Prompt - the instructions sent to the AI model Prompt = "Check if the following blog post comment is spam or not", - // JSON schema - a sample response object to format AI model replies by + // Sample object - a sample response object to format the AI model's replies by SampleObject = JsonConvert.SerializeObject( new { @@ -122,6 +123,81 @@ public async Task Examples() var addAiIntegrationTaskResult = store.Maintenance.Send(GenAiOperation); #endregion } + + using (var store = new DocumentStore()) + { + #region gen-ai_define-gen-ai-task_use-json-schema + GenAiConfiguration config = new GenAiConfiguration + { + // Task name + Name = "FilterSpam", + + // Unique task identifier + Identifier = "FilterSpam", + + // Connection string to AI model + ConnectionStringName = "ollama-cs", + + // Task is enabled + Disabled = false, + + // Collection associated with the task + Collection = "Posts", + + // Context generation script - format for objects to be sentto the AI model + GenAiTransformation = new GenAiTransformation { + Script = @" + for(const comment of this.Comments) + { + ai.genContext({Text: comment.Text, Author: comment.Author, Id: comment.Id});}" + }, + + // AI model Prompt - the instructions sent to the AI model + Prompt = "Check if the following blog post comment is spam or not", + + // JSON schema - a schema to format the AI model's replies by + JsonSchema = @"{ + ""name"": """ + "some-name" + @""", + ""strict"": true, + ""schema"": { + ""type"": ""object"", + ""properties"": { + ""Blocked"": { + ""type"": ""boolean"" + }, + ""Reason"": { + ""type"": ""string"", + ""description"": ""Concise reason for why this comment was marked as spam or ham"" + } + }, + ""required"": [ + ""Blocked"", + ""Reason"" + ], + ""additionalProperties"": false + } + }", + + // Update script - specifies what to do with AI model replies + UpdateScript = @" + // Find the comment + const idx = this.Comments.findIndex(c => c.Id == $input.Id); + // Was detected as spam + if($output.Blocked) + { + // Remove this comment + this.Comments.splice(idx, 1); + }", + + // Max concurrent connections to AI model + MaxConcurrency = 4 + }; + + // Run the task + var GenAiOperation = new AddGenAiOperation(config); + var addAiIntegrationTaskResult = store.Maintenance.Send(GenAiOperation); + #endregion + } } }