using TencentCloud.Hunyuan.V20230901;
using TencentCloud.Hunyuan.V20230901.Models;
namespace OASystem.API.OAMethodLib.HunYuanAPI
{
public class HunyuanService : IHunyuanService
{
private readonly HunyuanClient _hunyuanClient;
///
/// 构造函数注入配置好的HunyuanClient
///
///
public HunyuanService(HunyuanClient hunyuanClient)
{
_hunyuanClient = hunyuanClient;
}
///
public async Task ChatCompletionsHunyuan_t1_latestAsync(string question)
{
var request = new ChatCompletionsRequest
{
Model = "hunyuan-t1-latest",
Messages = new Message[]
{
new Message
{
Role = "user",
Content = question
}
},
Stream = false,
Temperature = 0.5f,
TopP = 1.0f,
// 其他参数根据需要设置
};
// 直接调用SDK方法
var response = await _hunyuanClient.ChatCompletions(request);
// 提取并返回模型生成的回答
// 注意:响应结构可能包含多个Choice,这里取第一个。
if (response.Choices != null && response.Choices.Length > 0)
{
return response.Choices[0].Message.Content?.Trim() ?? "模型未返回内容。";
}
return "模型未生成有效回答。";
}
public async Task ChatCompletionsAsync(ChatCompletionsRequest request)
{
// 直接调用SDK方法
return await _hunyuanClient.ChatCompletions(request);
}
///
public async Task AskWithFileContextAsync(string fileContent, string question, string model = "hunyuan-lite")
{
// 1. 构建提示词:将文件内容作为上下文,与用户问题结合。
// 这是一个简单示例,实际可根据需求设计更复杂的Prompt。
string prompt = $"请根据以下文本内容回答问题。\n文本内容:{fileContent}\n问题:{question}";
// 2. 使用SDK自带实体构建请求
var request = new ChatCompletionsRequest
{
Model = model,
Messages = new Message[]
{
new Message
{
Role = "user",
Content = prompt
}
},
// 可根据需要设置其他参数,如Stream, Temperature, TopP等
// Stream = false,
// Temperature = 0.5f,
// TopP = 1.0f,
};
// 3. 调用SDK方法
var response = await ChatCompletionsAsync(request);
// 4. 提取并返回模型生成的回答
// 注意:响应结构可能包含多个Choice,这里取第一个。
if (response.Choices != null && response.Choices.Length > 0)
{
return response.Choices[0].Message.Content?.Trim() ?? "模型未返回内容。";
}
return "模型未生成有效回答。";
}
}
}