0

I am attempting to replicate a call I can make within Postman within my .net Core API. Within Postman I am performing a POST call to:

https://api.openai.com/v1/completions

In the body I pass the following:

{
    "model":"ada:dummy-2022-11-08-18-19-04",
    "prompt":"This is my test data on causes for illness ->",
    "max_tokens":6,
    "temperature":0
}

as raw JSON.

This returns my result with no problem. Obviously I am setting my Authorization as well I am just not showing that. In my .net core api I have the following code:

async Task<string> getOpenAIData(OpenAISettings pSettings, string promptValue)
{
  var targetUrl = $"https://api.openai.com/V1/completions";

  OpenAIRequest reqBody = new OpenAIRequest();
  reqBody.model = pSettings.Model;
  reqBody.prompt = promptValue;
  reqBody.temprature = pSettings.Temprature;
  reqBody.max_tokens = pSettings.MaxTokens;

  using(HttpClient client = new HttpClient())
  {
    using(var request = new HttpRequestMessage(HttpMethod.Post, targetUrl))
    {
      request.Headers.TryAddWithoutValidation("Authorization", "Bearer my-secret-code");
      request.Content = JsonContent.Create(reqBody, new MediaTypeHeaderValue("application/json"));
      request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

      var response = client.SendAsync(request).Result;
      var json = response.Content.ReadAsStringAsync().Result;
    }
  }
  return "";
} 

I know there is more to complete this function but right now my response status is always a 404. My reqBody object looks identical to what I have passed from Postman.

A reader may tell me to just use one of the wrapper libraries but none of them allow you to specify the model (that I can tell) and I have tried to reverse engineer them and can't get anything to work. Is there something obvious I am just overlooking?

halfer
  • 19,824
  • 17
  • 99
  • 186
pachyderm94
  • 431
  • 1
  • 5
  • 15

1 Answers1

0

add try catch like:

        catch (WebException ex)
        {

            using (var stream = ex.Response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                string Fail404 = reader.ReadToEnd();
               
            }

            
        }

And then debug the Fail404 (respone body of any call)