赞
踩
今天来写一个简单的ernie-c#的例子,主要参考了百度智能云的例子,然后自己改了改,学习了ERNIE模型的鉴权方式,数据流的格式和简单的数据解析,实现了在C#中调用百度智能云的ernie(其实就是文心一言3.5的语言模型)的例子。
百度智能云
按提示注册即可
点击“应用接入” -> “创建应用”
按下图操作即可
百度会送20元的优惠券,试用的话是免费的,如果要收费的话,请酌情开通
我的代金券
在这里获取AK和SK,后面代码里面要用
// 您的AccessKey ID
const string API_KEY = "qSXXXXXXXXXXXXXXXXQ";
// 您的AccessKey Secret
const string SECRET_KEY = "Kb8XXXXXXXXXXXXXXXXXX24ZH";
public class Message
{
[JsonProperty("role")]
public string Role { get; set; }
[JsonProperty("content")]
public string Content { get; set; }
}
public class ErnieMessage
{
[JsonProperty("messages")]
public Message[] Messages { get; set; }
[JsonProperty("temperature")]
public double Temperature { get; set; }
[JsonProperty("top_p")]
public double TopP { get; set; }
[JsonProperty("penalty_score")]
public double PenaltyScore { get; set; }
[JsonProperty("disable_search")]
public bool DisableSearch { get; set; }
[JsonProperty("enable_citation")]
public bool EnableCitation { get; set; }
}
ErnieMessage ernieMessage = new ErnieMessage();
public class ChatCompletionResult
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("object")]
public string ObjectType { get; set; } // 这里的属性名可以根据实际需求进行命名,例如避免与.NET的内置Object类冲突
[JsonProperty("created")]
public long CreatedTimestamp { get; set; } // 通常时间戳会转换为DateTime,但这里保持为long
[JsonProperty("result")]
public string Result { get; set; }
[JsonProperty("is_truncated")]
public bool IsTruncated { get; set; }
[JsonProperty("need_clear_history")]
public bool NeedClearHistory { get; set; }
[JsonProperty("finish_reason")]
public string FinishReason { get; set; }
[JsonProperty("usage")]
public Usage UsageInfo { get; set; }
// 嵌套类来表示Usage对象
public class Usage
{
[JsonProperty("prompt_tokens")]
public int PromptTokens { get; set; }
[JsonProperty("completion_tokens")]
public int CompletionTokens { get; set; }
[JsonProperty("total_tokens")]
public int TotalTokens { get; set; }
}
}
// 定义消息池
public class MessagePool
{
public List<Message> Messages { get; set; }
public MessagePool()
{
Messages = new List<Message>();
}
public void AddMessage(Message message)
{
Messages.Add(message);
}
public void ClearMessages()
{
Messages.Clear();
}
}
enum eRole { User, Assistant}
string Role(eRole role)
{
return role == eRole.User ? "user" : "assistant";
}
private string Chat(string strSend, eRole role)
{
var client = new RestClient($"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-3.5-4k-0205?access_token={GetAccessTokenMethod()}");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
AddMessageToPool(strSend, role);
var body = JsonConvert.SerializeObject(ernieMessage, Formatting.None);
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
AddMessageToPool(response.Content, eRole.Assistant);
ChatCompletionResult result = JsonConvert.DeserializeObject<ChatCompletionResult>(response.Content);
FillCompletionInfo(result);
if (result.NeedClearHistory) ClearHistoryMethod();
return result.Result;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。