当前位置: 首页> 健康> 科研 > Open AI Stream Completion Set Variable Inside Function PHP With Openai-php SDK

Open AI Stream Completion Set Variable Inside Function PHP With Openai-php SDK

时间:2025/8/29 10:11:01来源:https://blog.csdn.net/suiusoar/article/details/140049699 浏览次数:0次

题意:使用 OpenAI 的 PHP SDK(例如 openai-php)来在函数内部设置和完成一个流(stream)相关的变量

问题背景:

How to set variable inside this openai-php sdk function in stream completion ? I am using this open-ai library

如何使用 openai-php SDK 在流完成(stream completion)的函数内部设置变量?我正在使用这个 open-ai 库。

https://github.com/orhanerday/open-ai

This is the code :

$client->completion($configuration, function ($curl_info, $data) { $response = ""; // This Variable Keep Init Because it is looping in this function                                       $cleanJson = str_replace("data: ", "", $data);                      if ($data != "data: [DONE]\n\n") {$arrayData = json_decode($cleanJson, true);                         $response = $response . $arrayData['choices'][0]['text']; // <= I cannot save all the content data to variable $response because it's keep replaced             } else {echo "ITS DONE";            }ob_flush();flush();            return strlen($data);               });    

I cannot define variable outside $client->completion function. Because it will not detect inside $client->completion function.

我无法在 $client->completion 函数之外定义变量,因为该变量在 $client->completion 函数内部不会被检测到。

What I want is I can pass the variable outside to $client->completion function.

我想要的是我能将变量从外部传递到 $client->completion 函数中。

example :        示例

 $client->completion($configuration, function ($curl_info, $data, $response, $other) { });

That example give an error.        示例报错

How do I pass the $response or $other variable in the $client->Completion ?

如何将 $response 或 $other 变量传递给 $client->Completion

问题解决:

Found the correct way. This is the correct one to pass the variable :

找到了正确的方法。这是传递变量的正确方式:

$Response = "";     $client->completion($configuration, function ($curl_info, $data) use (&$Response) {             $Response = "something here";});    

关键字:Open AI Stream Completion Set Variable Inside Function PHP With Openai-php SDK

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: