Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created March 27, 2023 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/fd757860a97543c228865a02ee9a9aed to your computer and use it in GitHub Desktop.
Save bjoerntx/fd757860a97543c228865a02ee9a9aed to your computer and use it in GitHub Desktop.
[HttpPost]
public string RequestAPI([FromBody] ChatGPTRequest request) {
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", Models.Constants.OPENAI_API_KEY);
var openAIPrompt = new {
model = "text-davinci-003",
prompt = Models.Constants.Prompts[request.Type] + request.Text.Trim(),
temperature = 0.2,
max_tokens = 2048,
top_p = 1
};
var stringContent = new StringContent(JsonConvert.SerializeObject(openAIPrompt), Encoding.UTF8, "application/json");
var result = client.PostAsync("https://api.openai.com/v1/completions", stringContent).Result;
var jsonContent = result.Content.ReadAsStringAsync().Result;
ChatGPTResponse chatGPTResponse = JsonConvert.DeserializeObject<ChatGPTResponse>(jsonContent);
return chatGPTResponse.Choices[0].Text.Trim();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment