LinkToolAddin/host/llm/Bailian.cs

55 lines
2.1 KiB
C#

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<string> 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<string> SendApplicationStreamAsync(string message)
{
throw new System.NotImplementedException();
}
public async Task<string> 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<LlmChat>(responseBody);
return result.Choices[0].Message.Content;
}
public async Task<string> 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<ApplicationOutput>(responseBody);
return responseBody;
}
}