|
7 | 7 | using Raven.Client.Documents.Operations.ConnectionStrings;
|
8 | 8 | using Raven.Client.Documents.Operations.ETL;
|
9 | 9 | using Sparrow.Json;
|
| 10 | +using static Akka.Streams.Attributes; |
10 | 11 | using static Akka.Streams.Implementation.Fusing.GraphInterpreter;
|
11 | 12 |
|
12 | 13 | namespace Raven.Documentation.Samples.AiIntegration.ConnectionStrings;
|
@@ -65,7 +66,7 @@ public async Task Examples()
|
65 | 66 |
|
66 | 67 | using (var store = new DocumentStore())
|
67 | 68 | {
|
68 |
| - #region gen-ai_define-gen-ai-task |
| 69 | + #region gen-ai_define-gen-ai-task_use-sample-object |
69 | 70 | GenAiConfiguration config = new GenAiConfiguration
|
70 | 71 | {
|
71 | 72 | // Task name
|
@@ -94,7 +95,7 @@ public async Task Examples()
|
94 | 95 | // AI model Prompt - the instructions sent to the AI model
|
95 | 96 | Prompt = "Check if the following blog post comment is spam or not",
|
96 | 97 |
|
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 |
98 | 99 | SampleObject = JsonConvert.SerializeObject(
|
99 | 100 | new
|
100 | 101 | {
|
@@ -122,6 +123,81 @@ public async Task Examples()
|
122 | 123 | var addAiIntegrationTaskResult = store.Maintenance.Send(GenAiOperation);
|
123 | 124 | #endregion
|
124 | 125 | }
|
| 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 | + } |
125 | 201 |
|
126 | 202 | }
|
127 | 203 | }
|
|
0 commit comments