using Microsoft.Extensions.AI;
using OpenAI.Chat;

namespace DFS.AI.API.Helper
{
    public class ChatHandle
    {
        public ChatHandle()
        {
            
        }
        // 创建快捷调用方法
        public async Task<ChatModelResponse> Chat(string userPrompt,ChatOptions chatOptions,string provider,string model)
        {
            // 配置生成参数
            //var chatOptions = new ChatOptions
            //{
            //    Temperature = 0.1f,      // 低随机性，保证稳定输出
            //    TopP = 0.9f,             // 核采样
            //    MaxOutputTokens = 120    // 限制输出长度
            //};
            var chatClient = AIClientHelper.GetDefaultChatClient(provider,model);
            try
            {
                Console.WriteLine("⏱️ 开始请求...\n");

                var response = await chatClient.GetResponseAsync(userPrompt, chatOptions);

                Console.WriteLine("✅ 请求成功");
               return response.ToModel();
            }
            catch (Exception ex)
            {
                Console.WriteLine("❌ 请求失败：");
               
            }
            return null;
        }


        // 创建快捷调用方法
        public async Task<ChatModelResponse> ChatOllamaModel(string userPrompt, ChatOptions chatOptions, string provider= "Ollama", string model="qwen2.5")
        {
            // 配置生成参数
            //var chatOptions = new ChatOptions
            //{
            //    Temperature = 0.1f,      // 低随机性，保证稳定输出
            //    TopP = 0.9f,             // 核采样
            //    MaxOutputTokens = 120    // 限制输出长度
            //};
            var chatClient = AIClientHelper.GetDefaultChatClient(provider, model);
            try
            {
                Console.WriteLine("⏱️ 开始请求...\n");

                var response = await chatClient.GetResponseAsync(userPrompt, chatOptions);

                Console.WriteLine("✅ 请求成功");
                return response.ToModel();
            }
            catch (Exception ex)
            {
                Console.WriteLine("❌ 请求失败：");

            }
            return null;
        }
    }
}
