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

embedded/2024/9/25 8:20:53/
aidu_pl">

题意:使用 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";});    


http://www.ppmy.cn/embedded/55895.html

相关文章

自动编码器简单理解及简单使用描述

1. 什么是自动编码器&#xff1f; 自动编码器分为编码器和解码器&#xff0c;其中解码器只在训练阶段用到。具体过程就是&#xff1a; 首先&#xff0c;输入训练样本&#xff0c;编码器对输入样本进行编码&#xff0c;对其进行降维&#xff0c;直到到达某个瓶颈层&#xff1b…

工具提示框(Tooltip): 设计、应用与最佳实践

工具提示框&#xff08;Tooltip&#xff09;: 设计、应用与最佳实践 引言 工具提示框&#xff08;Tooltip&#xff09;是用户界面&#xff08;UI&#xff09;设计中的一种常见元素&#xff0c;它为用户提供关于界面元素或操作的额外信息。当用户将鼠标悬停在某个元素上时&…

你了解人工智能吗?

前言 人工智能&#xff08;Artificial Intelligence&#xff0c;AI&#xff09;是计算机科学的一个重要分支&#xff0c;致力于开发能够执行通常需要人类智能的任务的系统。本文将从历史发展、关键技术、应用领域以及未来挑战等方面&#xff0c;深入探讨人工智能的相关内容。 …

MyBatis学习笔记-数据脱敏

如果项目需要对一些特殊、敏感的数据进行脱敏处理。根据实际的需求可以考虑在读写的过程中分别做脱敏操作。 一、写过程参数脱敏 主要是使用mybatis框架提供的Interceptor实现。需要考虑不同类型的参数解析处理方式不同。 @Slf4j @AllArgsConstructor @Intercepts({@Signatu…

小米大模型提效新框架:训练最高提速34%,推理最高提速52%!Kaldi之父合作出品

大模型推理速度提升50%以上&#xff0c;还能保证少样本学习性能&#xff01; 小米大模型团队提出SUBLLM&#xff08;Subsampling-Upsampling-Bypass Large Language Model&#xff09;&#xff0c;国际AI语音大牛、开源语音识别工具Kaldi之父Daniel Povey也参与指导。 与Llam…

简单爬虫案例——爬取快手视频

网址&#xff1a;aHR0cHM6Ly93d3cua3VhaXNob3UuY29tL3NlYXJjaC92aWRlbz9zZWFyY2hLZXk9JUU2JThCJTg5JUU5JTlEJUEy 找到视频接口&#xff1a; 视频链接在photourl中 完整代码&#xff1a; import requestsimport re url https://www.kuaishou.com/graphql cookies {did: web_…

spring mvc实现一个自定义Converter转换器

介绍 自定义转换器输入Spring MVC框架范畴&#xff0c;总体上输入Spring生态的一个特性&#xff0c;对Web开发起作用。 使用场景 在Spring Boot应用中&#xff0c;自定义转换器主要用于处理HTTP请求参数到Java对象的自动转换&#xff0c;或者Java对象到HTTP响应的序列化过程…

重磅发布|WAIC 2024最新活动日程安排完整发布!

WAIC 2024 将于 7 月在上海世博中心和世博展览馆举行&#xff0c;论坛时间为 7 月 4 日至 6 日&#xff0c;展览时间为 7 月 4 日至 7 日。会议涵盖 AI 伦理治理、大模型、具身智能、投融资、教育人才等重点话题&#xff0c;体现 AI 向善等价值导向&#xff0c;9 位大奖得主和 …