@spark leaf here it is
imported other libs too....
using OpenAI_API;
using OpenAI_API.Models;
namespace GPTIntegration
{
public partial class _default : System.Web.UI.Page
{
protected async void Button1_Click(object sender, EventArgs e)
{
// Define your API endpoint URL here
string url = "https://api.openai.com/v1/chat/completions";
string apiKey = "sk-XXX";
// Create an instance of HttpClient
using (var client = new HttpClient())
{
// Create a new prompt to send to the API
string prompt = promptTextBox.Text;
// Define parameters to specify for the API call
var parameters = new Dictionary<string, string>() {
{ "prompt", prompt }, // Text prompt to generate a response for
{ "max_tokens", "50" }, // Maximum number of tokens to generate in the response
{ "temperature", "0.5" }, // Sampling temperature to control the randomness and creativity of the response
{ "n", "1" }, // Number of responses to generate
{ "stop", "" }, // Stop sequence(s) used to stop generation when reached
{ "echo", "false" }, // Whether or not to include the prompt in the generated response
{ "presence_penalty", "0.0" }, // Penalty added to generate lower probability of repeating words present in the text prompt
{ "frequency_penalty", "0.0" }, // Penalty added to generate lower probability of repeating words generated so far
{ "model", "text-davinci-003" } // Model used to generate the response
// Other models that can be used:
};
// Serialize the parameters into a URL-encoded string
string paramString = await new FormUrlEncodedContent(parameters).ReadAsStringAsync();
// Create a new HttpRequestMessage with the headers and data we want to send to the API
var request = new HttpRequestMessage
{
Method = HttpMethod.Post, // Use HTTP POST method for the request
RequestUri = new Uri(url), // Set the request URL to the OpenAI API endpoint
Headers =
{
{ "Authorization", $"Bearer {apiKey}" }, // Set the authorization header with our API key
//{ "Content-Type", "application/json" } // Set the Content-Type header to application/json
},
Content = new StringContent(paramString, System.Text.Encoding.UTF8, "application/json")
};
// Send the HTTP request to the API endpoint
HttpResponseMessage response = await client.SendAsync(request);
// Read the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();
// Deserialize the response JSON into a GptResponse object and extract the generated text
GptResponse gptResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<GptResponse>(responseBody);
if (gptResponse != null && gptResponse.Choices != null && gptResponse.Choices.Count > 0)
{
string generatedText = gptResponse.Choices[0].Text;
// Display the generated text in the label control
generatedTextLabel.Text = generatedText;
}
else
{
generatedTextLabel.Text = "No response received from OpenAI API";
}
}
}
}
public class GptResponse
{
[System.Text.Json.Serialization.JsonPropertyName("choices")]
public List<GptChoice> Choices { get; set; }
}
public class GptChoice
{
[System.Text.Json.Serialization.JsonPropertyName("text")]
public string Text { get; set; }
}
}