Call chatgpt api in C#
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
public class OpenAIApiClient
{
??? private readonly HttpClient _httpClient;
??? private readonly string _apiKey;
??? public OpenAIApiClient(string apiKey)
??? {
??????? _apiKey = apiKey;
??????? _httpClient = new HttpClient();
??????? _httpClient.BaseAddress = new Uri("https://api.openai.com/v1/");
??????? _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
??? }?
??? public async Task<string> SendPrompt(string prompt, string model)
??? {
??????? var requestBody = new
??????? {
??????????? prompt = prompt,
??????????? model = model,
??????????? max_tokens = 150,
??????????? temperature = 0.5
??????? };?
??????? var response = await _httpClient.PostAsJsonAsync("completions", requestBody);
??????? response.EnsureSuccessStatusCode();
??????? var responseBody = await response.Content.ReadAsStringAsync();
??????? return responseBody;
??? }
}
Once you have an API client, you can use it to send prompts to ChatGPT and get responses.
var openAIApiClient = new OpenAIApiClient("your-api-key-here");
var response = await openAIApiClient.SendPrompt("Hello, ChatGPT!", "davinci");
Console.WriteLine(response);