using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Diagnostics; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Text.Json.Serialization; using NPOI.Util; using HeyRed.MarkdownSharp; namespace FineUIPro.Web.ZHGL.FirstArticalInspaction { public partial class LLM : PageBase { protected void Page_Load(object sender, EventArgs e) { } protected void TextBox2_TextChanged(object sender, EventArgs e) { var result = GetResult(TextBox2.Text); var response = System.Text.Json.JsonSerializer.Deserialize(result); var finalResult = response.Output.Text; Markdown mark = new Markdown(); string markdownResult = mark.Transform(finalResult); Label1.Text = markdownResult; } private static readonly string API_KEY = "sk-Sbcnxh0YXz"; //不返回资料来源接口 private static readonly string URL = "https://dashscope.aliyuncs.com/api/v1/apps/7e0c1e47a6364f8e914982c99a1e00c5/completion"; //返回原始资料来源 private static readonly string URL2 = "https://dashscope.aliyuncs.com/api/v1/apps/d9461732646e49a38d7832d5c3eb8332/completion"; private static readonly string URL3 = "https://dashscope.aliyuncs.com/api/v1/apps/7e0c1e47a6364f8e914982c99a1e00c5/completion"; static String GetResult(string question) { var client = new LLM(); try { string result = client.GetCompletion(question); Debug.WriteLine(result); return result; } catch (Exception ex) { Debug.WriteLine($"发生错误: {ex.Message}"); return $"发生错误: {ex.Message}"; } } public string GetCompletion(string question) { using (var client = new HttpClient()) { // 设置请求头 client.DefaultRequestHeaders.Add("Authorization", $"Bearer {API_KEY}"); // 构建请求体 var requestBody = new { input = new { prompt = question }, parameters = new { }, debug = new { } }; // 将对象序列化为 JSON var jsonContent = System.Text.Json.JsonSerializer.Serialize(requestBody); var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); try { // 发送 POST 请求 var response = client.PostAsync(URL, content).Result; // 确保请求成功 response.EnsureSuccessStatusCode(); // 读取响应内容 return response.Content.ReadAsStringAsync().Result; } catch (HttpRequestException ex) { Console.WriteLine($"请求失败: {ex.Message}"); throw; } } } } public class ResponseModel { [JsonPropertyName("output")] public Output Output { get; set; } [JsonPropertyName("usage")] public Usage Usage { get; set; } [JsonPropertyName("request_id")] public string RequestId { get; set; } } public class Output { [JsonPropertyName("finish_reason")] public string FinishReason { get; set; } [JsonPropertyName("session_id")] public string SessionId { get; set; } [JsonPropertyName("text")] public string Text { get; set; } } public class Usage { [JsonPropertyName("models")] public List Models { get; set; } } public class Model { [JsonPropertyName("output_tokens")] public int OutputTokens { get; set; } [JsonPropertyName("model_id")] public string ModelId { get; set; } [JsonPropertyName("input_tokens")] public int InputTokens { get; set; } } }