105 lines
3.4 KiB
C#
105 lines
3.4 KiB
C#
using System;
|
|
using System.ClientModel;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text.Json;
|
|
using System.Threading.Tasks;
|
|
using AutoSW.Agent;
|
|
using Microsoft.Extensions.AI;
|
|
using OpenAI;
|
|
using OpenAI.Chat;
|
|
|
|
namespace AutoSW.UI.Util
|
|
{
|
|
internal class ChatClient
|
|
{
|
|
/// <summary>
|
|
/// 火山方舟API Key
|
|
/// </summary>
|
|
private readonly string API_KEY = "58e5c6fd-a5da-400e-b889-715febce478f";
|
|
private readonly string OLLAMA_ENDPOINT = "http://localhost:11434/";
|
|
private readonly string API_ENDPOINT = "https://ark.cn-beijing.volces.com/api/v3/";
|
|
private string MODEL;
|
|
private readonly IChatClient _client;
|
|
|
|
private bool useOpenAIClient;
|
|
private OpenAIClient _openAICLient;
|
|
public ChatClient(string modelName = "deepseek-r1:1.5b", bool useOllama = true)
|
|
{
|
|
//MODEL = modelName;
|
|
//var apiKeyCredential = new ApiKeyCredential(API_KEY);
|
|
//var openAIClientOptions = new OpenAIClientOptions
|
|
//{
|
|
// Endpoint = new Uri(ENDPOINT)
|
|
//};
|
|
|
|
//_client = new OpenAIClient(apiKeyCredential, openAIClientOptions)
|
|
// .AsChatClient(MODEL);
|
|
|
|
if (useOllama)
|
|
{
|
|
var client = new OllamaChatClient(new Uri(OLLAMA_ENDPOINT), modelName);
|
|
_client = new ChatClientBuilder(client)
|
|
.UseFunctionInvocation()
|
|
.Build();
|
|
}
|
|
else
|
|
{
|
|
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
|
var client = new OpenAIClient(new ApiKeyCredential(API_KEY), new OpenAIClientOptions
|
|
{
|
|
Endpoint = new Uri(API_ENDPOINT),
|
|
|
|
});
|
|
_client = client.AsChatClient(modelName);
|
|
}
|
|
}
|
|
|
|
public IChatClient Client => _client;
|
|
|
|
public async Task<string> GetChatResponseAsync(string question)
|
|
{
|
|
var response = await _client.GetResponseAsync(question);
|
|
return response.Message.ToString();
|
|
}
|
|
|
|
public async IAsyncEnumerable<string> GetStreamingResponseAsync(string question)
|
|
{
|
|
// 获取所有的工具函数
|
|
var toolsList = AgentExecutor.GetDefaultTools();
|
|
var chatOptions = new ChatOptions
|
|
{
|
|
Tools = toolsList,
|
|
ToolMode = ChatToolMode.Auto,
|
|
|
|
};
|
|
if (useOpenAIClient)
|
|
{
|
|
var chatClient = _openAICLient.AsChatClient("doubao-1-5-pro-32k-250115")
|
|
.AsBuilder()
|
|
.UseFunctionInvocation()
|
|
.Build();
|
|
var response = chatClient.GetResponseAsync(question, chatOptions);
|
|
yield return response.Result.Choices[0].Text;
|
|
}
|
|
else
|
|
{
|
|
await foreach (var response in _client.GetStreamingResponseAsync(question, chatOptions))
|
|
{
|
|
yield return response.Text;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UseOpenAI()
|
|
{
|
|
useOpenAIClient = true;
|
|
_openAICLient = new OpenAIClient(new ApiKeyCredential(API_KEY), new OpenAIClientOptions
|
|
{
|
|
Endpoint = new Uri(API_ENDPOINT)
|
|
});
|
|
}
|
|
}
|
|
}
|