首页 > 编程语言 >使用c#调用chatgpt 。以下代码由ai自动生成。

使用c#调用chatgpt 。以下代码由ai自动生成。

时间:2023-04-14 13:11:28浏览次数:45  
标签:byteArray string c# request System ai API using chatgpt

using System;
using System.IO;
using System.Net;
using System.Text;

class Program
{
static void Main()
{
// API的URL
string apiUrl = "https://api.openai.com/v1/chat/completions";

    // 设置API的请求头
    WebRequest request = WebRequest.Create(apiUrl);
    request.Method = "POST";
    request.Headers.Add("Authorization", "Bearer {YOUR_API_KEY}");  // 替换为你的API密钥
    request.ContentType = "application/json";
    
    // 设置API的请求体,包含输入和参数
    string input = "请问天气怎么样?";
    string prompt = "用户:" + input + "\n助理:";
    int maxTokens = 50;
    string requestBody = "{ \"messages\": [{ \"role\": \"system\", \"content\": \"You are a helpful assistant.\" }, { \"role\": \"user\", \"content\": \"" + prompt + "\" }], \"max_tokens\": " + maxTokens + " }";
    byte[] byteArray = Encoding.UTF8.GetBytes(requestBody);
    request.ContentLength = byteArray.Length;
    
    // 发送请求并获取响应
    using (Stream dataStream = request.GetRequestStream())
    {
        dataStream.Write(byteArray, 0, byteArray.Length);
    }
    WebResponse response = request.GetResponse();
    using (Stream responseStream = response.GetResponseStream())
    {
        StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
        string responseJson = reader.ReadToEnd();
        Console.WriteLine(responseJson);
        // 在这里处理API的响应
    }
    response.Close();
}

}

标签:byteArray,string,c#,request,System,ai,API,using,chatgpt
From: https://www.cnblogs.com/wang2650/p/17317982.html

相关文章

  • Codefroces Round #863(div.3)---E
    题目:Problem-E-Codeforces题意:有一个序列a,a中的每个元素的每一位都不为4,问序列中第k个数字是多少。分析:可以用数位dp查询出1-r中有多少个满足条件的数字,通过二分r找到结果。代码:#include<bits/stdc++.h>usingnamespacestd;#definelllonglong#defineendl'\n'u......
  • macos shangflask tradingview python环境安装
    1.Homebrew4.0后更新报错问题原文参考brewupdateFailedtodownloadhttps://formulae.brew.sh/api/formula.jws.json!Homebrew4.0进行了一项最大的改动,组织方式从Git仓库管理改为JSON文件下载。JSON配置文件会从formulae.brew.sh下载,本地的homebrew/core、homebrew/cas......
  • C# 用委托有什么好处? 它起什么作用?
    什么是委托首先要知道什么是委托,用最通俗易懂的话来讲,你就可以把委托看成是用来执行方法(函数)的一个东西。如何使用委托在使用委托的时候,你可以像对待一个类一样对待它。即先声明,再实例化。只是有点不同,类在实例化之后叫对象或实例,但委托在实例化后仍叫委托。如下:1......
  • docker-compose版本升级
    https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-linux-x86_641.下载安装包根据机器的架构选择具体的版本,更多的请参考官网https://github.com/docker/compose/releases架构下载地址X86https://github.com/docker/compose/releases/download/v2.4......
  • .NET7使用HttpClient实现查询天气预报接口
    朋友做网站需要根据城市展示天气预报,找了一圈没有找到靠谱的接口,今天在中央气象台的官网查询某个城市找到了接口,先用postman试了一下居然可以使用,可以查询某个城市7天的天气预报等信息。但是查询编码是气象台自己的编码,在网上搜索了一下居然有这个编码。本文使用HttpClient方法查......
  • 使用css在元素的前面或者后面添加内容
    :before在元素前面添加内容:after在元素的后面添加内容<!DOCTYPEhtml><html><style>h1:after{content:'前面的内容';}h1:before{content:'后面的内容';}</style><body><h1>***h1内容****</h1></body></html......
  • Cmake 学习记录
    https://cmake.org/documentation/官网有相关的文档,可以以官方的文档进行了解。首先我们先了解一下Cmake的大致使用目的,以前使用过qt,qt有Qmake这个文件,让我大致了解了过程,就是有这么一份文件,配置了相关属性,然后利用工具,将目标工程编译打包。引用下官网的简介:CMakeisanopen-sou......
  • 安全参考和书安 NPM、PYPI、DockerHub 备份
    安全参考2013Dockerdockerpullapachecn0/hackcto-2013dockerrun-tid-p<port>:80apachecn0/hackcto-2013#访问http://localhost:{port}查看文档PYPIpipinstallhackcto-2013hackcto-2013<port>#访问http://localhost:{port}查看文档NPMnpminstall-ghackcto-e......
  • mybatis 连接 ORACLE
     application-common-dev.ymlspring:profiles:include:[common]main:allow-bean-definition-overriding:true#Redis服务器IPredis:host:127.0.0.1#Redis服务器连接端口port:6379#Redis服务器连接密码password:testj......
  • CEH 讲义 NPM、PYPI、DockerHub 备份
    CEHv10ModuleAllInOneDockerdockerpullapachecn0/ceh-v10-mod-allinonedockerrun-tid-p<port>:80apachecn0/ceh-v10-mod-allinone#访问http://localhost:{port}查看文档PYPIpipinstallceh-v10-mod-allinoneceh-v10-mod-allinone<port>#访问http://loca......