| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- using TencentCloud.Hunyuan.V20230901;
- using TencentCloud.Hunyuan.V20230901.Models;
- namespace OASystem.API.OAMethodLib.HunYuanAPI
- {
- public class HunyuanService : IHunyuanService
- {
- private readonly HunyuanClient _hunyuanClient;
- /// <summary>
- /// 构造函数注入配置好的HunyuanClient
- /// </summary>
- /// <param name="hunyuanClient"></param>
- public HunyuanService(HunyuanClient hunyuanClient)
- {
- _hunyuanClient = hunyuanClient;
- }
- /// <inheritdoc />
- public async Task<string> 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<ChatCompletionsResponse> ChatCompletionsAsync(ChatCompletionsRequest request)
- {
- // 直接调用SDK方法
- return await _hunyuanClient.ChatCompletions(request);
- }
- /// <inheritdoc />
- public async Task<string> 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 "模型未生成有效回答。";
- }
- }
- }
|