当前位置:   article > 正文

c# 调用文心一言API_c# 文心一言

c# 文心一言

c# 调用文心一言API

  1. using Newtonsoft.Json;
  2. using RestSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace 文心一言
  6. {
  7. class Program
  8. {
  9. const string API_KEY = "";
  10. const string SECRET_KEY = "";
  11. static void Main(string[] args)
  12. {
  13. var token = GetAccessToken();
  14. //文心一言会根据前面的问答回复信息
  15. List<ChatVO> messages = new List<ChatVO>();
  16. //用户问1
  17. messages.Add(new ChatVO { role = "user", content = "父亲的英文是什么?" });
  18. //温馨一言答1
  19. messages.Add(new ChatVO { role = "assistant", content = "父亲是father" });
  20. //用户问2
  21. messages.Add(new ChatVO { role = "user", content = "那母亲呢?" });
  22. var chatMsg = GetChat(token, "13900000011", messages);
  23. Console.WriteLine(chatMsg);
  24. Console.ReadLine();
  25. }
  26. static string GetChat(string accessToken, string userId, List<ChatVO> messages)
  27. {
  28. WxyyChatReq wxyyChatReq = new WxyyChatReq
  29. {
  30. user_id = userId,
  31. messages = messages,
  32. temperature = 0.95,
  33. top_p = 0.8,
  34. penalty_score = 1,
  35. disable_search = false,
  36. enable_citation = false,
  37. stream = false
  38. };
  39. var url = $"https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token={accessToken}";
  40. var client = new RestClient(url);
  41. client.Timeout = -1;
  42. var request = new RestRequest(Method.POST);
  43. request.AddHeader("Content-Type", "application/json");
  44. var body = JsonConvert.SerializeObject(wxyyChatReq);
  45. request.AddParameter("application/json", body, ParameterType.RequestBody);
  46. IRestResponse response = client.Execute(request);
  47. return response.Content;
  48. }
  49. /**
  50. * 使用 AK,SK 生成鉴权签名(Access Token)
  51. * @return 鉴权签名信息(Access Token)
  52. */
  53. static string GetAccessToken()
  54. {
  55. var url = "https://aip.baidubce.com/oauth/2.0/token";
  56. var client = new RestClient(url);
  57. client.Timeout = -1;
  58. var request = new RestRequest(Method.POST);
  59. request.AddParameter("grant_type", "client_credentials");
  60. request.AddParameter("client_id", API_KEY);
  61. request.AddParameter("client_secret", SECRET_KEY);
  62. IRestResponse response = client.Execute(request);
  63. Console.WriteLine(response.Content);
  64. var result = JsonConvert.DeserializeObject<dynamic>(response.Content);
  65. return result.access_token.ToString();
  66. }
  67. }
  68. public class WxyyChatReq
  69. {
  70. public string user_id { get; set; }
  71. public double temperature { get; set; }
  72. public double top_p { get; set; }
  73. public double penalty_score { get; set; }
  74. public bool disable_search { get; set; }
  75. public bool enable_citation { get; set; }
  76. //是否流式处理
  77. public bool stream { get; set; }
  78. //最大输出token数(token:字数一般为1:2)
  79. //public int max_output_tokens { get; set; }
  80. public List<ChatVO> messages { get; set; }
  81. }
  82. public class ChatVO
  83. {
  84. public string role { get; set; }
  85. public string content { get; set; }
  86. }
  87. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/980543
推荐阅读
相关标签
  

闽ICP备14008679号