HunyuanService.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using TencentCloud.Hunyuan.V20230901;
  2. using TencentCloud.Hunyuan.V20230901.Models;
  3. namespace OASystem.API.OAMethodLib.HunYuanAPI
  4. {
  5. public class HunyuanService : IHunyuanService
  6. {
  7. private readonly HunyuanClient _hunyuanClient;
  8. /// <summary>
  9. /// 构造函数注入配置好的HunyuanClient
  10. /// </summary>
  11. /// <param name="hunyuanClient"></param>
  12. public HunyuanService(HunyuanClient hunyuanClient)
  13. {
  14. _hunyuanClient = hunyuanClient;
  15. }
  16. /// <inheritdoc />
  17. public async Task<string> ChatCompletionsHunyuan_t1_latestAsync(string question)
  18. {
  19. var request = new ChatCompletionsRequest
  20. {
  21. Model = "hunyuan-t1-latest",
  22. Messages = new Message[]
  23. {
  24. new Message
  25. {
  26. Role = "user",
  27. Content = question
  28. }
  29. },
  30. Stream = false,
  31. Temperature = 0.5f,
  32. TopP = 1.0f,
  33. // 其他参数根据需要设置
  34. };
  35. // 直接调用SDK方法
  36. var response = await _hunyuanClient.ChatCompletions(request);
  37. // 提取并返回模型生成的回答
  38. // 注意:响应结构可能包含多个Choice,这里取第一个。
  39. if (response.Choices != null && response.Choices.Length > 0)
  40. {
  41. return response.Choices[0].Message.Content?.Trim() ?? "模型未返回内容。";
  42. }
  43. return "模型未生成有效回答。";
  44. }
  45. public async Task<ChatCompletionsResponse> ChatCompletionsAsync(ChatCompletionsRequest request)
  46. {
  47. // 直接调用SDK方法
  48. return await _hunyuanClient.ChatCompletions(request);
  49. }
  50. /// <inheritdoc />
  51. public async Task<string> AskWithFileContextAsync(string fileContent, string question, string model = "hunyuan-lite")
  52. {
  53. // 1. 构建提示词:将文件内容作为上下文,与用户问题结合。
  54. // 这是一个简单示例,实际可根据需求设计更复杂的Prompt。
  55. string prompt = $"请根据以下文本内容回答问题。\n文本内容:{fileContent}\n问题:{question}";
  56. // 2. 使用SDK自带实体构建请求
  57. var request = new ChatCompletionsRequest
  58. {
  59. Model = model,
  60. Messages = new Message[]
  61. {
  62. new Message
  63. {
  64. Role = "user",
  65. Content = prompt
  66. }
  67. },
  68. // 可根据需要设置其他参数,如Stream, Temperature, TopP等
  69. // Stream = false,
  70. // Temperature = 0.5f,
  71. // TopP = 1.0f,
  72. };
  73. // 3. 调用SDK方法
  74. var response = await ChatCompletionsAsync(request);
  75. // 4. 提取并返回模型生成的回答
  76. // 注意:响应结构可能包含多个Choice,这里取第一个。
  77. if (response.Choices != null && response.Choices.Length > 0)
  78. {
  79. return response.Choices[0].Message.Content?.Trim() ?? "模型未返回内容。";
  80. }
  81. return "模型未生成有效回答。";
  82. }
  83. }
  84. }