Skip to content

Instantly share code, notes, and snippets.

@kkdai
Created December 7, 2022 02:13
Show Gist options
  • Save kkdai/49bb4816f137295240beb3c34ba814a8 to your computer and use it in GitHub Desktop.
Save kkdai/49bb4816f137295240beb3c34ba814a8 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Web;
namespace isRock.Template
{
public class ChatGPT
{
private static HttpClient client = new HttpClient();
private static string JSONString = @"
{{
""model"": ""text-davinci-003"",
""prompt"": ""{0}"",
""max_tokens"": 4000,
""temperature"": 0
}}
";
private static Newtonsoft.Json.JsonSerializerSettings serializerSettings = new Newtonsoft.Json.JsonSerializerSettings()
{
NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
};
public static Result CallChatGPT(string msg)
{
string uri = "https://api.openai.com/v1/completions";
// Request headers.
client.DefaultRequestHeaders.Add(
"Authorization", "Bearer ________chatGPT_Token_______________");
var JSON = String.Format(JSONString, msg);
var content = new StringContent(JSON, Encoding.UTF8, "application/json");
var response = client.PostAsync(uri, content).Result;
return Newtonsoft.Json.JsonConvert.DeserializeObject<Result>(response.Content.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment