using System; using System.Windows.Forms; using System.Net; using Newtonsoft.Json; // Add the following using statement for the RichTextBox control using System.Drawing; public class Form1 : Form { private RichTextBox codeBox; private Button generateCodeButton; private TextBox inputBox; public Form1() { // Initialize the form controls codeBox = new RichTextBox(); generateCodeButton = new Button(); inputBox = new TextBox(); // Set the properties for the generate code button generateCodeButton.Text = "Generate Code"; generateCodeButton.Click += new EventHandler(this.generateCode); // Add the controls to the form this.Controls.Add(codeBox); this.Controls.Add(generateCodeButton); this.Controls.Add(inputBox); } private void generateCode(object sender, EventArgs e) { // Get the user input string input = inputBox.Text; // Use the OpenAI API to generate code string apiKey = "YOUR_API_KEY"; string endpoint = "https://api.openai.com/v1/engines/davinci-codex/completions"; string json = "{\"prompt\":\"" + input + "\",\"temperature\":0.5}"; string response = sendPost(endpoint, apiKey, json); // Parse the JSON response dynamic jsonData = JsonConvert.DeserializeObject(response); string generatedCode = jsonData.choices[0].text; // Clear the code box codeBox.Clear(); // Color the generated code codeBox.SelectionColor = Color.Blue; codeBox.AppendText(generatedCode); codeBox.SelectionColor = codeBox.ForeColor; } private string sendPost(string endpoint, string apiKey, string json) { // Send a POST request to the specified endpoint var httpWebRequest = (HttpWebRequest)WebRequest.Create(endpoint); httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; httpWebRequest.Headers.Add("Authorization", "Bearer " + apiKey); using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { return streamReader.ReadToEnd(); } } [STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } }