Skip to content

Commit b4c81eb

Browse files
authored
added code to generate sample events
1 parent 8a67a81 commit b4c81eb

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

articles/cognitive-services/personalizer/includes/quickstart-sdk-csharp.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,61 @@ Run the application with the dotnet `run` command from your application director
534534
dotnet run
535535
```
536536

537+
## Generate sample events for analysis
538+
539+
You can easily generate 5,000 events from this quickstart demo scenario, which is sufficient to get experience with using Apprentice mode, Online mode, running offline evaluations, and creating feature evaluations. Simply replace the `Main()` method of the above code in the `Run a Rank and Reward cycle` section with:
540+
541+
```csharp
542+
543+
static void Main(string[] args)
544+
{
545+
int iteration = 1;
546+
int runLoop = 0;
547+
548+
// Get the actions list to choose from personalizer with their features.
549+
IList<RankableAction> actions = GetActions();
550+
551+
// Initialize Personalizer client.
552+
PersonalizerClient client = InitializePersonalizerClient(ServiceEndpoint);
553+
554+
do
555+
{
556+
Console.WriteLine("\nIteration: " + iteration++);
557+
558+
// <rank>
559+
// Get context information.
560+
Context context = GetContext();
561+
562+
// Create current context from user specified data.
563+
IList<object> currentContext = new List<object>() {
564+
context
565+
};
566+
567+
// Generate an ID to associate with the request.
568+
string eventId = Guid.NewGuid().ToString();
569+
570+
// Rank the actions
571+
var request = new RankRequest(actions: actions, contextFeatures: currentContext, eventId: eventId);
572+
RankResponse response = client.Rank(request);
573+
// </rank>
574+
575+
Console.WriteLine($"\nPersonalizer service thinks {context.User.Name} would like to have: {response.RewardActionId}.");
576+
577+
// <reward>
578+
float reward = GetRewardScore(context, response.RewardActionId);
579+
580+
// Send the reward for the action based on user response.
581+
client.Reward(response.EventId, new RewardRequest(reward));
582+
// </reward>
583+
584+
runLoop = runLoop + 1;
585+
586+
} while (runLoop < 1000);
587+
}
588+
```
589+
590+
then run the program.
591+
537592
![The quickstart program asks a couple of questions to gather user preferences, known as features, then provides the top action.](../media/csharp-quickstart-commandline-feedback-loop/quickstart-program-feedback-loop-example.png)
538593

539594
The [source code for this quickstart](https://github.com/Azure-Samples/cognitive-services-quickstart-code/tree/master/dotnet/Personalizer) is available.

0 commit comments

Comments
 (0)