Created
March 27, 2023 21:36
-
-
Save bjoerntx/fd757860a97543c228865a02ee9a9aed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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