ChatStd.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. require_once __DIR__.'/../../../vendor/autoload.php';
  3. // 导入对应产品模块的client
  4. use TencentCloud\Hunyuan\V20230901\HunyuanClient;
  5. // 导入要请求接口对应的Request类
  6. use TencentCloud\Hunyuan\V20230901\Models\ChatStdRequest;
  7. use TencentCloud\Common\Exception\TencentCloudSDKException;
  8. use TencentCloud\Common\Credential;
  9. // 导入可选配置类
  10. use TencentCloud\Common\Profile\ClientProfile;
  11. use TencentCloud\Common\Profile\HttpProfile;
  12. function streamProcessResult($result)
  13. {
  14. // 处理每个事件流
  15. echo $result;
  16. }
  17. try {
  18. // 实例化一个证书对象,入参需要传入腾讯云账户secretId,secretKey
  19. // $cred = new Credential("【secretId】", "【secretKey】");
  20. $cred = new Credential(getenv("TENCENTCLOUD_SECRET_ID"),
  21. getenv("TENCENTCLOUD_SECRET_KEY"));
  22. // 实例化一个http选项,可选的,没有特殊需求可以跳过
  23. $httpProfile = new HttpProfile();
  24. $httpProfile->setReqMethod("POST"); // post请求(默认为post请求)
  25. $httpProfile->setReqTimeout(60); // 请求超时时间,单位为秒(默认60秒)
  26. $httpProfile->setEndpoint("hunyuan.tencentcloudapi.com"); // 指定接入地域域名(默认就近接入)
  27. // 实例化一个client选项,可选的,没有特殊需求可以跳过
  28. $clientProfile = new ClientProfile();
  29. $clientProfile->setSignMethod("TC3-HMAC-SHA256"); // 指定签名算法(默认为HmacSHA256)
  30. $clientProfile->setHttpProfile($httpProfile);
  31. // 实例化要请求产品的client对象,clientProfile是可选的
  32. $client = new HunyuanClient($cred, "ap-guangzhou", $clientProfile);
  33. // 如果响应是SSE-event流,可以设置回调函数逐个处理事件流,否则一次返回所有响应内容
  34. $client->setSseResponseCallbackFunc('streamProcessResult');
  35. // 实例化一个hunyuan实例信息查询请求对象,每个接口都会对应一个request对象。
  36. $req = new ChatStdRequest();
  37. $req->TopP = 0;
  38. $req->Temperature = 0;
  39. $reqMessage = new Message();
  40. $reqMessage->Role = "user";
  41. $reqMessage->Content = "计算1+1";
  42. $req->Messages = [$reqMessage];
  43. // 通过client对象调用ChatStd方法发起请求。注意请求方法名与请求对象是对应的
  44. $client->ChatStd($req);
  45. }
  46. catch(TencentCloudSDKException $e) {
  47. echo $e;
  48. }