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";
     });    
相关推荐
apple_692658856 小时前
DLSS5画质增强 mayflyMods v1.2.5 四合一工具:DLSS5画质增强 + MFG多帧生成 + 游戏库启动 + MOD模组管理
ai·dlss5
云烟成雨TD15 小时前
LlamaIndex 系列【31】检索增强策略:命名实体识别(NER)
ai·agent·rag·llamaindex
2601_9548118216 小时前
AI科学实验室自律系统:闭环自动化与智能实验设计技术路径 — 自动化技术
ai
2601_9548118217 小时前
AI智能教育多智能体协同:备教辅全链路架构实现 — 技术架构
ai
Java后端的Ai之路17 小时前
Python进阶探索17 - Python中的深拷贝与浅拷贝
人工智能·python·ai·浅拷贝·深拷贝
龙智DevSecOps解决方案18 小时前
AI驱动的知识管理指南:基于Atlassian Intelligence、Rovo、Teamwork Collection打造团队知识引擎
ai·atlassian·需求管理·团队协作·知识管理
李燚18 小时前
Agent 要用 API key,但明文一次都不能进模型——Secret Runtime 落地实录(第110篇)
ai·agent·credential·secret·eino·deepflux·secret runtime
奇牙coding18 小时前
gpt-5.6-luna 接入踩坑实录:model_not_found 排查与 reasoning_summary 字段使用说明
ai
霸道流氓气质18 小时前
Prompt 工程最佳实践与企业级模板库建设
ai
海带紫菜菠萝汤19 小时前
本周 AI 观察:嘴上喊着降速,手上全踩油门
人工智能·深度学习·ai·开源·大模型