注册账号
1.注册(百度智能云)

2.选择应用接入

3.创建应用接口
4.获取 API_KEY与 SECRET_KEY

代码实现

实现前先要在NuGet程序包下载两个库
Newtonsoft.Json;(我目前使用版本13.0.3)
RestSharp;(我目前使用版本106.2.0)
已下是代码的实现
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
您的AccessKey ID
//const string API_KEY = “OA6CUefHaF7CkTLc9k8KdWwu”;
您的AccessKey Secret
//const string SECRET_KEY = “zlYO2QF641jVGqIlT0Hv31stYISvh6nj”;
// 您的AccessKey ID
const string API_KEY = “AFJFrctHEskWpwGJ2jcGTA0s”;
// 您的AccessKey Secret
const string SECRET_KEY = “Y59b4iZMBTjfWtSHXR01JQfG0dX2rOif”;
//文心一言会根据前面的问答回复信息
List messages = new List();
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
var token = GetAccessToken();
List messages = new List();
string sayWord = tssl_show.Text;
messages.Add(new ChatVO { role = “user”, content = sayWord });
if (string.IsNullOrEmpty(sayWord))
{
MessageBox.Show(“问题不能为空”);
tssl_show.Focus();
return;
}
tb_answer.AppendText(“用户: ” + sayWord + “rn”);
//var chatMsg = GetChat(token, “key117258248”, messages);
var chatMsg = GetChat(token, “117261818”, messages);
ChatCompletionResponse response = JsonConvert.DeserializeObject(chatMsg);
tb_answer.AppendText(“文心一言: ” + response.result + “rn”);
}
static string GetChat(string accessToken, string userId, List messages)
{
WxyyChatReq wxyyChatReq = new WxyyChatReq
{
user_id = userId,
messages = messages,
temperature = 0.95,
top_p = 0.8,
penalty_score = 1,
disable_search = false,
enable_citation = false,
stream = false
};
//var url = $”https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-128k?access_token={accessToken}”;
var url = $”https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-4.0-8k-latest?access_token={accessToken}”;
var client = new RestClient(url);
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader(“Content-Type”, “application/json”);
var body = JsonConvert.SerializeObject(wxyyChatReq);
request.AddParameter(“application/json”, body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return response.Content;
}
/**
* 使用 AK,SK 生成鉴权签名(Access Token)
* @return 鉴权签名信息(Access Token)
*/
static string GetAccessToken()
{
var url = “https://aip.baidubce.com/oauth/2.0/token”;
var client = new RestClient(url);
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);
var result = JsonConvert.DeserializeObject(response.Content);
return result.access_token.ToString();
}
}
public class WxyyChatReq
{
public string user_id { get; set; }
public double temperature { get; set; }
public double top_p { get; set; }
public double penalty_score { get; set; }
public bool disable_search { get; set; }
public bool enable_citation { get; set; }
//是否流式处理
public bool stream { get; set; }
//最大输出token数(token:字数一般为1:2)
//public int max_output_tokens { get; set; }
public List messages { get; set; }
}
///
///
public class ChatVO
{
public string role { get; set; }
public string content { get; set; }
}
///
///
public class ChatCompletionResponse
{
public string id { get; set; }
public int created { get; set; }
public string result { get; set; }
}
}
文章来源于互联网:c# 调用文心一言大模型
9月4日,百度文心一言官网显示,在向全社会开放一周年之际,文心一言进行了功能最新全面升级,同时在周年期间为新老会员增加1个月专业版免费使用体验。 据了解,针对网页版用户需求,文心一言实现了创作内容更加深度专业、问答效果更加全面丰富、支持同时处理上百个多种格…
5bei.cn大模型教程网










