-
Notifications
You must be signed in to change notification settings - Fork 14
Text completion 1. (Legacy)
Zoltan Juhasz edited this page Dec 10, 2023
·
1 revision
WARNING! - This is a legacy feature of OpenAI, it will be shut down on January 4th, 2024. Check more information: https://platform.openai.com/docs/api-reference/completions
The next code demonstrates how to give a simple instruction (prompt). The whole answer generated on the OpenAI side remotelly, than the answer will send in a response.
public static async Task Main(string[] args)
{
using var host = Host.CreateDefaultBuilder(args)
.ConfigureServices((builder, services) =>
{
services.AddForgeOpenAI(options => {
options.AuthenticationInfo = builder
.Configuration["OpenAI:ApiKey"]!;
});
})
.Build();
IOpenAIService openAi = host.Services.GetService<IOpenAIService>()!;
// in this scenario the answer generated on server side,
// than the whole text will be sent in one pass.
// this method is useful for small conversatons and for short answers
TextCompletionRequest request = new TextCompletionRequest();
request.Prompt = "Say this is a test";
HttpOperationResult<TextCompletionResponse> response =
await openAi.TextCompletionService
.GetAsync(request, CancellationToken.None)
.ConfigureAwait(false);
if (response.IsSuccess)
{
response.Result!.Completions.ForEach(c => Console.WriteLine(c.Text));
request.Prompt = "Are you sure?";
response = await openAi.TextCompletionService
.GetAsync(request, CancellationToken.None).ConfigureAwait(false);
if (response.IsSuccess)
{
response.Result!.Completions.ForEach(c => Console.WriteLine(c.Text));
}
else
{
Console.WriteLine(response);
}
}
else
{
Console.WriteLine(response);
}
}