LinkToolAddin/host/llm/Bailian.cs

61 lines
2.5 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 LinkToolAddin.host.llm.entity.stream;
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<LlmStreamChat> SendChatStreamAsync(LlmJsonContent jsonContent)
{
jsonContent.Stream = true;
StringBuilder contentBuilder = new StringBuilder();
StringBuilder reasonBuilder = new StringBuilder();
await foreach (LlmStreamChat chunk in HttpRequest.PostWithStreamingResponseAsync(
"https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
JsonConvert.SerializeObject(jsonContent),
api_key))
{
contentBuilder.Append(chunk.Choices[0].Delta.Content);
reasonBuilder.Append(chunk.Choices[0].Delta.ResoningContent);
LlmStreamChat fullChunk = chunk;
fullChunk.Choices[0].Delta.Content = contentBuilder.ToString();
fullChunk.Choices[0].Delta.ResoningContent = reasonBuilder.ToString();
yield return fullChunk;
}
}
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;
}
}