Hi, I can’t get past this error in connecting via the API. The posted content clearly gets throught to the openai server, which returns an “invalid API key” error. The message shows the inital and final api key characters, which indicate it did get my API key correctly: I also tried a second key with the same result. I see in this forum that this is a common problem, but the solution is not clear.
{
“error”: {
“message”: “Incorrect API key provided: sk - kqT*****************************************Flt0. You can find your API key at OpenAI API”,
“type”: “invalid_request_error”,
“param”: null,
“code”: “invalid_api_key”
}
}
Here is the C# code:
namespace SystematicReview {
public static class GPTClient {
public static async Task Testing123() {
string apiUrl = “https://api.openai.com/v1/chat/completions”;
string apiKey1 = “sk - kqTi…Flt0”;
//string apiKey2 = “sk - kPi3…zXW”;
Request request = new Request();
request.Messages = new RequestMessage[] {
new RequestMessage() {
Role = "system",
Content = "You are a helpful assistant."
},
new RequestMessage() {
Role = "user",
Content = "Who won the world series in 2020?"
}
};
string requestData = JsonSerializer.Serialize(request);
StringContent content = new StringContent(requestData, Encoding.UTF8, "application/json");
using (HttpClient httpClient = new HttpClient()) {
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey1);
HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(apiUrl, content);
string responseString = await httpResponseMessage.Content.ReadAsStringAsync();
if (httpResponseMessage.IsSuccessStatusCode) {
Response response = JsonSerializer.Deserialize<Response>(responseString);
string FinalString = response.Choices[0].Message.Content;
}
else {
string ErrorString = string.Format("{0}:\n{1}", httpResponseMessage.StatusCode.ToString(), responseString);
int wowsa = 0;
}
}
return true;
}
public class Request {
[JsonPropertyName("model")]
//public string Model { get; set; } = "gpt-3.5-turbo";
public string Model { get; set; } = "text-davinci-003";
[JsonPropertyName("max_tokens")]
//public int MaxTokens { get; set; } = 4000;
public int MaxTokens { get; set; } = 4097;
//public int MaxTokens { get; set; } = 8192;
[JsonPropertyName("messages")]
public RequestMessage[] Messages { get; set; }
}
public class RequestMessage {
[JsonPropertyName("role")]
public string Role { get; set; }
[JsonPropertyName("content")]
public string Content { get; set; }
}
public class Response {
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("created")]
public int Created { get; set; }
[JsonPropertyName("model")]
public string Model { get; set; }
[JsonPropertyName("usage")]
public ResponseUsage Usage { get; set; }
[JsonPropertyName("choices")]
public ResponseChoice[] Choices { get; set; }
}
public class ResponseUsage {
[JsonPropertyName("prompt_tokens")]
public int PromptTokens { get; set; }
[JsonPropertyName("completion_tokens")]
public int CompletionTokens { get; set; }
[JsonPropertyName("total_tokens")]
public int TotalTokens { get; set; }
}
public class ResponseChoice {
[JsonPropertyName("message")]
public ResponseMessage Message { get; set; }
[JsonPropertyName("finish_reason")]
public string FinishReason { get; set; }
[JsonPropertyName("index")]
public int Index { get; set; }
}
public class ResponseMessage {
[JsonPropertyName("role")]
public string Role { get; set; }
[JsonPropertyName("content")]
public string Content { get; set; }
}
}
}