HunyuanService1.cs 4.0 KB

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