I’m having trouble after upgrading my OpenAI .NET SDK from version 1.11.0 to 2.1.0. The classes I was using before don’t seem to exist anymore.
In my old implementation, I was using these components that are now missing:
CompletionRequestclassCompletions.CreateCompletionAsyncmethod
Here’s my original working code from the older SDK version:
using OpenAI_API;
using OpenAI_API.Completions;
public class AiContentGenerator : IContentGenerator
{
private readonly OpenAIAPI _apiClient;
private readonly IUserValidator _validator;
public AiContentGenerator(string key, IUserValidator validator)
{
_apiClient = new OpenAIAPI(key);
_validator = validator;
}
public async Task<ContentResult> GenerateContentAsync(ContentRequest input)
{
var promptRequest = new CompletionRequest
{
Prompt = input.UserPrompt,
Model = input.SelectedModel,
MaxTokens = input.TokenLimit,
Temperature = input.Creativity
};
var result = await _apiClient.Completions.CreateCompletionAsync(promptRequest);
var cleanedOutput = result.ToString().Replace("\n", "").Replace("\r", "").Trim('"', ' ');
return new ContentResult(cleanedOutput, 0, true);
}
}
After updating to 2.1.0, I tried modifying the code like this:
using System.Threading.Tasks;
using OpenAI;
public class AiContentGenerator : IContentGenerator
{
private readonly OpenAIClient _openAiClient;
private readonly IUserValidator _validator;
public AiContentGenerator(string key, IUserValidator validator)
{
_openAiClient = new OpenAIClient(key);
_validator = validator;
}
public async Task<ContentResult> GenerateContentAsync(ContentRequest input)
{
var promptRequest = new CompletionRequest
{
Prompt = input.UserPrompt,
Model = input.SelectedModel,
MaxTokens = input.TokenLimit,
Temperature = input.Creativity
};
var result = await _openAiClient.Completions.CreateCompletionAsync(promptRequest);
var cleanedOutput = result.ToString().Replace("\n", "").Replace("\r", "").Trim('"', ' ');
return new ContentResult(cleanedOutput, 0, true);
}
}
But this doesn’t work because:
CompletionRequestclass doesn’t existCompletionsproperty is not available onOpenAIClient
My Setup:
- Framework:
.NET 9.0 - Package:
OpenAIversion2.1.0 - Also using:
Newtonsoft.Jsonversion13.0.3andRystem.OpenAiversion4.0.5
What I need help with:
- How do I make completion requests in OpenAI .NET SDK version 2.1.0?
- What classes or methods should I use instead of the old
CompletionRequestandCompletions.CreateCompletionAsync? - Is there a different way to handle text completions in this newer version?
I’d really appreciate any code examples or guidance on how to properly migrate this functionality. Thanks!