Search code examples
openai-apichatgpt-api

Invalid URL when using ChatGPT Sdk (Betalgo.OpenAI)


I'm playing around with the ChatGPT sdk (Betalgo.OpenAI) in c# and using one of their samples:

var openAiService = new OpenAIService(new OpenAiOptions()
{
    ApiKey = "abc",
    Organization = "org-def"
});

var completionResult = await openAiService.Completions
  .CreateCompletion(new CompletionCreateRequest()
  {
    Prompt = "Once upon a time",
    MaxTokens = 20,
    Model = Models.Gpt_4
  }); 

I've change my APIKey and Organization here but they are correct in my code but I'm getting the following error:

This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?

But I'm not specifying the URL so clearly this is determined by the SDK.

I've also tried it with ChatGPT 3.5 by setting the Model to Models.Gpt_3_5_Turbo but I'm getting the very same error.

Any ideas how to circumvent this problem?

Thanks.

UPDATE-1

As per @ShaharShokrani recommendation, I changed from

var completionResult = await openAiService.Completion

to

var completionResult = await openAiService.ChatCompletion

but it also meant changing the provided request. Here is the full code:

var openAiService = new OpenAIService(new OpenAiOptions()
{
    ApiKey = "abc",
    Organization = "org-def"
});

var completionResult = await openAiService.ChatCompletion
    .CreateCompletion(new ChatCompletionCreateRequest()
    {
       Messages = new List<ChatMessage>()
        {
            new ChatMessage("user", "Once upon a time")
        },
       MaxTokens = 20,
       Model = Models.Gpt_3_5_Turbo
    });

As you can see, the CompletionCreateRequest has been changed to ChatCompletionCreateRequest and prompt has been replaced by Messages which is a List<ChatMessages>.


Solution

  • Change it to var completionResult = await openAiService.ChatCompletion instead of var completionResult = await openAiService.Completion