c#整合文言一心聊天代码

更新时间:2024/6/2 18:23:02点击: 技术文章

ERNIE-Bot 4.0是百度最新发布的自研?语?模型,实现了基础模型的全面升级,在理解、生成、逻辑和记忆能力上都有着显著提升,支持5K输入+2K输出。本文介绍了千帆相关API及应用。 
using System;
using System.IO;
using RestSharp;//依赖版本106.15.0 https://www.nuget.org/packages/RestSharp/106.15.0
using Newtonsoft.Json; //https://www.nuget.org/packages/Newtonsoft.Json
namespace SampleApplication {
    public class Sample {

        const string API_KEY = "01111111113`123`18hC";
        const string SECRET_KEY = "lPOCLlprasdfgsdfgsdNVJnvk64Y1";

        public static void Main(string[] args) {
            var client = new RestClient($"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token={GetAccessToken()}");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            var body = @"{""messages"":[{""role"":""user"",""content"":""网站建设写一句广告语""}],""temperature"":0.95,""top_p"":0.8,""penalty_score"":1,""disable_search"":false,""enable_citation"":false}";
            request.AddParameter("application/json", body,  ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);

        }
        
        
        /**
        * 使用 AK,SK 生成鉴权签名(Access Token)
        * @return 鉴权签名信息(Access Token)
        */
        static string GetAccessToken() {
            var client = new RestClient($"https://aip.baidubce.com/oauth/2.0/token");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddParameter("grant_type", "client_credentials");
            request.AddParameter("client_id", API_KEY);
            request.AddParameter("client_secret", SECRET_KEY);
            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);
            var result = JsonConvert.DeserializeObject<dynamic>(response.Content);
            return result.access_token.ToString();
        }
        
    }
}