using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Text; using System.Threading.Tasks; using LinkToolAddin.common; using LinkToolAddin.host.llm.entity; using Newtonsoft.Json; namespace LinkToolAddin.host.llm; public class Bailian : Llm { public string model { get; set; } = "qwen-max"; public string temperature { get; set; } public string top_p { get; set; } public string max_tokens { get; set; } public string app_id { get; set; } public string api_key { get; set; } public async IAsyncEnumerable SendChatStreamAsync(LlmJsonContent jsonContent) { jsonContent.Stream = true; StringBuilder builder = new StringBuilder(); await foreach (var chunk in HttpRequest.PostWithStreamingResponseAsync( "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions", JsonConvert.SerializeObject(jsonContent), api_key)) { builder.Append(chunk); yield return builder.ToString(); } } public IAsyncEnumerable SendApplicationStreamAsync(string message) { throw new System.NotImplementedException(); } public async Task SendChatAsync(LlmJsonContent jsonContent) { string url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"; string responseBody = await HttpRequest.SendPostRequestAsync(url, JsonConvert.SerializeObject(jsonContent), api_key); LlmChat result = JsonConvert.DeserializeObject(responseBody); return result.Choices[0].Message.Content; } public async Task SendApplicationAsync(CommonInput commonInput) { string url = $"https://dashscope.aliyuncs.com/api/v1/apps/{app_id}/completion"; string responseBody = await HttpRequest.SendPostRequestAsync(url, JsonConvert.SerializeObject(commonInput), api_key); ApplicationOutput result = JsonConvert.DeserializeObject(responseBody); return responseBody; } }