1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
| using Microsoft.Extensions.Options; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks;
namespace AiChatAPI01x02 { class WxyyAiModels { string API_KEY02; string SECRET_KEY02; private readonly HttpClient _httpClient;
public WxyyAiModels(string aPI_KEY02, string sECRET_KEY02) { _httpClient = new HttpClient(); API_KEY02 = aPI_KEY02; SECRET_KEY02 = sECRET_KEY02; }
public async Task<string> DiaoYongAiModelAsync(string question) { var token = await GetAccessTokenAsync(); List<ChatVO> messages = new List<ChatVO>(); ChatVO chat01 = new ChatVO { role = "user", content = $"{question}" }; messages.Add(chat01); var chatMsg = await GetChatAsync(token, "13900000011", messages); return chatMsg; }
public async Task<string> DiaoYongAiModelAsync(string userId,string question) { var token = await GetAccessTokenAsync(); List<ChatVO> messages = new List<ChatVO>(); ChatVO chat01 = new ChatVO { role = "user", content = $"{question}" }; messages.Add(chat01); var chatMsg = await GetChatAsync(token, userId, messages); return chatMsg; } public async Task<string> DiaoYongAiModelAsync(string userId, List<ChatVO> messages) { var token = await GetAccessTokenAsync(); var chatMsg = await GetChatAsync(token, "123453323", messages); return chatMsg; }
public async Task<string> DiaoYongAiTranslationAsync(string question, string from, string to) { var token = await GetAccessTokenAsync(); List<ChatVO> messages = new List<ChatVO>(); ChatVO chat01 = new ChatVO { role = "user", content = $"你是一个ai翻译助手的api,把-{question}-,从-{from}-翻译成-{to}-,注意仅返回翻译后的内容。因为我的后端系统只能处理翻译后的内容,否则会报错。" }; messages.Add(chat01); var chatMsg = await GetChatAsync(token, "123453323", messages); return chatMsg; } public async Task<string> DiaoYongAiAnswerAsync(string question) { var token = await GetAccessTokenAsync(); List<ChatVO> messages = new List<ChatVO>(); ChatVO chat01 = new ChatVO { role = "user", content = $"你是一个ai答题助手的api,把-{question}-,答题,注意仅返回答案,不要返回题目。因为我的后端系统只能处理答案,否则会报错。" }; messages.Add(chat01); var chatMsg = await GetChatAsync(token, "123453323", messages); return chatMsg; }
public async Task<string> DiaoYongAiModelAsync(string token,string userId, string question) { List<ChatVO> messages = new List<ChatVO>(); ChatVO chat01 = new ChatVO { role = "user", content = $"{question}" }; messages.Add(chat01); var chatMsg = await GetChatAsync(token, userId, messages); return chatMsg; }
public async Task<string> GetChatAsync(string accessToken, string userId, List<ChatVO> 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/completions_pro?access_token={accessToken}"; var json = JsonConvert.SerializeObject(wxyyChatReq);
var response = await _httpClient.PostAsync(url, new StringContent(json, System.Text.Encoding.UTF8, "application/json")); var content = await response.Content.ReadAsStringAsync();
return content; } public async Task<string> GetAccessTokenAsync() { var url = "https://aip.baidubce.com/oauth/2.0/token"; var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("grant_type", "client_credentials"), new KeyValuePair<string, string>("client_id", API_KEY02), new KeyValuePair<string, string>("client_secret", SECRET_KEY02) });
var response = await _httpClient.PostAsync(url, content); var result = await response.Content.ReadAsStringAsync(); var json = JsonConvert.DeserializeObject<dynamic>(result); return json.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; } public List<ChatVO> messages { get; set; } }
public class ChatVO { public string role { get; set; } public string content { get; set; } } }
|