c#如何引入chatgpt开发ai智能化应用?
网友回复
以下是使用C#调用ChatGPT API的示例代码:
using System; using System.Net.Http; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { var endpoint = "https://api.openai.com/v1/engines/davinci-codex/completions"; var apiKey = "YOUR_API_KEY_HERE"; var prompt = "Hello, how are you?"; var maxTokens = 50; var temperature = 0.7; using var client = new HttpClient(); client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}"); var requestBody = new { prompt = prompt, max_tokens = maxTokens, temperature = temperature }; var response = await client.PostAsJsonAsync(endpoint, requestBody); var responseBody = await response.Content.ReadAsAsync<dynamic>(); var completions = responseBody.choices; foreach (var completion in completions) { Console.WriteLine(completion.text); } } }请注意,您需要将`YOUR_API_KEY_HERE`替换为您的实际API密钥。此外,您还可以根据需要更改提示、最大令牌数和温度。