admin管理员组文章数量:1632072
# C# API
现在需要Verify your phone number to create an API key
using Newtonsoft.Json;
using System.Text;
class Program
{
static readonly HttpClient client = new HttpClient();
static async Task Main()
{
try
{
// 设置 API 密钥
string apiKey = "your api";
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
// 构建请求体
var requestData = new
{
model = "gpt-3.5-turbo-1106",
messages = new[] {
new { role = "system",content = "解释递归" },
},
// [{ role: "system", content: "You are a helpful assistant." }],
temperature = 0.7,
max_tokens = 60
};
string json = JsonConvert.SerializeObject(requestData);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
// 发送请求
HttpResponseMessage response = await client.PostAsync("https://api.openai//v1/chat/completions", content);
// 获取响应
string responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
}
}
max_tokens = 60 我设的小了,内容就截断了。
版权声明:本文标题:C# chatGPT API调用示例 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1729101803a1186676.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论