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

题意:使用 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 :

php 复制代码
$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 : 示例

php 复制代码
 $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 :

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

php 复制代码
$Response = "";     
                        
        $client->completion($configuration, function ($curl_info, $data) use (&$Response) {             
        $Response = "something here";
     });    
相关推荐
云微客AI剪辑矩阵系统33 分钟前
矩阵新玩法,云微客AI矩阵系统开启新营销大门
人工智能·ai·矩阵·云微客
ChatGPT研究院3 小时前
Transformer教程之神经网络和深度学习基础
人工智能·深度学习·神经网络·机器学习·ai·chatgpt·transformer
技能咖4 小时前
在企业数字化转型中,设计人应如何拥抱AI?
人工智能·ai·aigc
程序员潘子4 小时前
如何用 php 实现邮件发送功能
vscode·后端·php
flydean4 小时前
福利来了!MoneyPrinterPlus可以自动配置环境和自动运行了
ai·aigc·工具·程序那些事
ChatGPT研究院5 小时前
ChatGPT自媒体创作秘籍:高效生成优质文章和视频
人工智能·ai·chatgpt·音视频·媒体
小宇python5 小时前
PHP远程代码执行漏洞复现及安全防护
开发语言·安全·php
rorg5 小时前
Laravel 中 使用模型作为标志
php·laravel·dreamweaver
罗政6 小时前
个人博客|PHP源码|支持多国语言切换
开发语言·php
kkai人工智能7 小时前
ChatGPT 论文助手:如何用 AI 技术加速学术写作过程
人工智能·学习·ai·chatgpt·媒体