Skip to content

Commit 716e24e

Browse files
authored
Merge pull request #2061 from reebhub/RDoc-3296_GenAI_3
GenAI documentation - fix by review comments
2 parents 015048e + 05dac97 commit 716e24e

File tree

2 files changed

+84
-4
lines changed
  • Documentation/7.1
    • Raven.Documentation.Pages/ai-integration/gen-ai-integration
    • Samples/csharp/Raven.Documentation.Samples/AiIntegration/GenAI

2 files changed

+84
-4
lines changed

Documentation/7.1/Raven.Documentation.Pages/ai-integration/gen-ai-integration/gen-ai-api.markdown

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@
6464
* Define a GenAI task using a `GenAiConfiguration` object.
6565
* Run the task using `AddGenAiOperation`.
6666

67-
{CODE:csharp gen-ai_define-gen-ai-task@AiIntegration\GenAI\GenAI.cs /}
67+
{CODE-TABS}
68+
{CODE-TAB:csharp:use-sample-object gen-ai_define-gen-ai-task_use-sample-object@AiIntegration\GenAI\GenAI.cs /}
69+
{CODE-TAB:csharp:use-json-schema gen-ai_define-gen-ai-task_use-json-schema@AiIntegration\GenAI\GenAI.cs /}
70+
{CODE-TABS/}
6871

6972
---
7073

@@ -79,7 +82,8 @@
7982
| **Collection** | `string` | Name of the document collection associated with the task |
8083
| **GenAiTransformation** | `GenAiTransformation` | Context generation script - format for objects to be sent to the AI model |
8184
| **Prompt** | `string` | AI model Prompt - the instructions sent to the AI model |
82-
| **SampleObject** | `string` | JSON schema - a sample response object to format AI model replies by |
85+
| **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 <br> If both a `SampleObject` and a `JsonSchema` are provided the schema takes precedence |
86+
| **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 <br> If both a `SampleObject` and a `JsonSchema` are provided the schema takes precedence |
8387
| **UpdateScript** | `string` | Update script - specifies what to do with AI model replies |
8488
| **MaxConcurrency** | `int` | Max concurrent connections to the AI model (each connection serving a single context object |
8589

Documentation/7.1/Samples/csharp/Raven.Documentation.Samples/AiIntegration/GenAI/GenAI.cs

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Raven.Client.Documents.Operations.ConnectionStrings;
88
using Raven.Client.Documents.Operations.ETL;
99
using Sparrow.Json;
10+
using static Akka.Streams.Attributes;
1011
using static Akka.Streams.Implementation.Fusing.GraphInterpreter;
1112

1213
namespace Raven.Documentation.Samples.AiIntegration.ConnectionStrings;
@@ -65,7 +66,7 @@ public async Task Examples()
6566

6667
using (var store = new DocumentStore())
6768
{
68-
#region gen-ai_define-gen-ai-task
69+
#region gen-ai_define-gen-ai-task_use-sample-object
6970
GenAiConfiguration config = new GenAiConfiguration
7071
{
7172
// Task name
@@ -94,7 +95,7 @@ public async Task Examples()
9495
// AI model Prompt - the instructions sent to the AI model
9596
Prompt = "Check if the following blog post comment is spam or not",
9697

97-
// JSON schema - a sample response object to format AI model replies by
98+
// Sample object - a sample response object to format the AI model's replies by
9899
SampleObject = JsonConvert.SerializeObject(
99100
new
100101
{
@@ -122,6 +123,81 @@ public async Task Examples()
122123
var addAiIntegrationTaskResult = store.Maintenance.Send(GenAiOperation);
123124
#endregion
124125
}
126+
127+
using (var store = new DocumentStore())
128+
{
129+
#region gen-ai_define-gen-ai-task_use-json-schema
130+
GenAiConfiguration config = new GenAiConfiguration
131+
{
132+
// Task name
133+
Name = "FilterSpam",
134+
135+
// Unique task identifier
136+
Identifier = "FilterSpam",
137+
138+
// Connection string to AI model
139+
ConnectionStringName = "ollama-cs",
140+
141+
// Task is enabled
142+
Disabled = false,
143+
144+
// Collection associated with the task
145+
Collection = "Posts",
146+
147+
// Context generation script - format for objects to be sentto the AI model
148+
GenAiTransformation = new GenAiTransformation {
149+
Script = @"
150+
for(const comment of this.Comments)
151+
{
152+
ai.genContext({Text: comment.Text, Author: comment.Author, Id: comment.Id});}"
153+
},
154+
155+
// AI model Prompt - the instructions sent to the AI model
156+
Prompt = "Check if the following blog post comment is spam or not",
157+
158+
// JSON schema - a schema to format the AI model's replies by
159+
JsonSchema = @"{
160+
""name"": """ + "some-name" + @""",
161+
""strict"": true,
162+
""schema"": {
163+
""type"": ""object"",
164+
""properties"": {
165+
""Blocked"": {
166+
""type"": ""boolean""
167+
},
168+
""Reason"": {
169+
""type"": ""string"",
170+
""description"": ""Concise reason for why this comment was marked as spam or ham""
171+
}
172+
},
173+
""required"": [
174+
""Blocked"",
175+
""Reason""
176+
],
177+
""additionalProperties"": false
178+
}
179+
}",
180+
181+
// Update script - specifies what to do with AI model replies
182+
UpdateScript = @"
183+
// Find the comment
184+
const idx = this.Comments.findIndex(c => c.Id == $input.Id);
185+
// Was detected as spam
186+
if($output.Blocked)
187+
{
188+
// Remove this comment
189+
this.Comments.splice(idx, 1);
190+
}",
191+
192+
// Max concurrent connections to AI model
193+
MaxConcurrency = 4
194+
};
195+
196+
// Run the task
197+
var GenAiOperation = new AddGenAiOperation(config);
198+
var addAiIntegrationTaskResult = store.Maintenance.Send(GenAiOperation);
199+
#endregion
200+
}
125201

126202
}
127203
}

0 commit comments

Comments
 (0)