Files
semantic-kernel/dotnet/samples/KernelSyntaxExamples/Example49_LogitBias.cs
T
Gil LaHayeandShawn Callegari 0d0b1e4b8c .Net: Update examples and notebooks to stop using text-davinci-003 (#2307)
### Motivation and Context
text-davinci-003 is being retired by Azure OpenAI and is no longer
supported for new deployments

### Description
Update examples and notebooks to use gpt-35-turbo / gpt-3.5-turbo
instead

### Contribution Checklist
- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄

---------

Co-authored-by: Shawn Callegari <[email protected]>
2023-08-04 20:54:35 +00:00

85 lines
3.9 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.AI.OpenAI.ChatCompletion;
/**
* Logit_bias is an optional parameter that modifies the likelihood of specified tokens appearing in a Completion.
* When using the Token Selection Biases parameter, the bias is added to the logits generated by the model prior to sampling.
*/
// ReSharper disable once InconsistentNaming
public static class Example49_LogitBias
{
public static async Task RunAsync()
{
OpenAIChatCompletion chatCompletion = new(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey);
// To use Logit Bias you will need to know the token ids of the words you want to use.
// Getting the token ids using the GPT Tokenizer: https://platform.openai.com/tokenizer
// The following text is the tokenized version of the book related tokens
// "novel literature reading author library story chapter paperback hardcover ebook publishing fiction nonfiction manuscript textbook bestseller bookstore reading list bookworm"
var keys = new[] { 3919, 626, 17201, 1300, 25782, 9800, 32016, 13571, 43582, 20189, 1891, 10424, 9631, 16497, 12984, 20020, 24046, 13159, 805, 15817, 5239, 2070, 13466, 32932, 8095, 1351, 25323 };
var settings = new ChatRequestSettings();
// This will make the model try its best to avoid any of the above related words.
foreach (var key in keys)
{
//This parameter maps tokens to an associated bias value from -100 (a potential ban) to 100 (exclusive selection of the token).
//-100 to potentially ban all the tokens from the list.
settings.TokenSelectionBiases.Add(key, -100);
}
Console.WriteLine("Chat content:");
Console.WriteLine("------------------------");
var chatHistory = chatCompletion.CreateNewChat("You are a librarian expert");
// First user message
chatHistory.AddUserMessage("Hi, I'm looking some suggestions");
await MessageOutputAsync(chatHistory);
string reply = await chatCompletion.GenerateMessageAsync(chatHistory, settings);
chatHistory.AddAssistantMessage(reply);
await MessageOutputAsync(chatHistory);
chatHistory.AddUserMessage("I love history and philosophy, I'd like to learn something new about Greece, any suggestion");
await MessageOutputAsync(chatHistory);
reply = await chatCompletion.GenerateMessageAsync(chatHistory, settings);
chatHistory.AddAssistantMessage(reply);
await MessageOutputAsync(chatHistory);
/* Output:
Chat content:
------------------------
User: Hi, I'm looking some suggestions
------------------------
Assistant: Sure, what kind of suggestions are you looking for?
------------------------
User: I love history and philosophy, I'd like to learn something new about Greece, any suggestion?
------------------------
Assistant: If you're interested in learning about ancient Greece, I would recommend the book "The Histories" by Herodotus. It's a fascinating account of the Persian Wars and provides a lot of insight into ancient Greek culture and society. For philosophy, you might enjoy reading the works of Plato, particularly "The Republic" and "The Symposium." These texts explore ideas about justice, morality, and the nature of love.
------------------------
*/
}
/// <summary>
/// Outputs the last message of the chat history
/// </summary>
private static Task MessageOutputAsync(ChatHistory chatHistory)
{
var message = chatHistory.Messages.Last();
Console.WriteLine($"{message.Role}: {message.Content}");
Console.WriteLine("------------------------");
return Task.CompletedTask;
}
}