using System.Threading.Tasks;
private const string ApiKey = "your_openai_api_key_here";
private const string Endpoint = "https://api.openai.com/v1/chat/completions";
public static async Task Main(string[] args)
Console.WriteLine("Chatbot: Hello! Ask me anything. Type 'exit' to end the conversation.");
string userInput = Console.ReadLine();
if (userInput?.ToLower() == "exit")
Console.WriteLine("Chatbot: Goodbye!");
string response = await GetChatGPTResponse(userInput);
Console.WriteLine($"Chatbot: {response}");
private static async Task<string> GetChatGPTResponse(string userInput)
var client = new RestClient(Endpoint);
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", $"Bearer {ApiKey}");
request.AddHeader("Content-Type", "application/json");
new { role = "system", content = "You are a helpful assistant." },
new { role = "user", content = userInput }
request.AddJsonBody(requestBody);
var response = await client.ExecuteAsync(request);
if (response.IsSuccessful)
var responseObject = System.Text.Json.JsonDocument.Parse(response.Content);
return responseObject.RootElement.GetProperty("choices")[0].GetProperty("message").GetProperty("content").GetString();
return "I'm sorry, I couldn't process your request.";