当前位置: 首页 > article >正文

OpenAi assistant run always fails when called from PHP

题意:从 PHP 调用时,OpenAI 助理运行总是失败。

问题背景:

The runs I create with the openai-php library fail direct in 100% of cases. What am I doing wrong? I do not have much experience with php but this is the test script.

我使用 openai-php 库创建的运行在 100% 的情况下直接失败。我哪里做错了?我对 PHP 没有太多经验,这是测试脚本。

<?php
require '../../../dev/vendor/autoload.php';

$apiKey = getenv('ASSISTANT_API_KEY');

@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ob_end_clean();
set_time_limit(0);
ob_implicit_flush(1);

$client = OpenAI::client($apiKey);

echo "Creating thread...<br/>";
$response = $client->threads()->create([]);

$array = $response->toArray(); 
$threadId = $response->id;

echo "Thread created, id:" . $response->id . "<br/>";

echo "Adding message Hello World...<br/>";

$response = $client->threads()->messages()->create($threadId, 
    [
        'role' => 'user',
        'content' => 'Hello World!',
    ]
);

echo "Message created, id:" . $response->id . "<br/>";

echo "Creating a run...<br/>";

$response = $client->threads()->runs()->create(
    threadId: $threadId, 
    parameters: [
        'assistant_id' => 'asst_wYzcGyBRwvpcKY3G4O0fEM5A',
    ],
);

echo "Run created, id:" . $response->id . "<br/>";
$runId = $response->id;

echo "Waiting for run result...<br/>";

for ($i=0; $i <= 20; $i++) {
    $response = $client->threads()->runs()->retrieve(
        threadId: $threadId,
        runId: $runId,
    );
    $runStatus = $response->status;
    if(strcmp($runStatus, "in_progress")==0){
        echo "In progres... <br/>";
    }

    if(strcmp($runStatus, "failed")==0){
        echo "Failed... <br/>";
        echo json_encode($response->toArray(), JSON_PRETTY_PRINT);
        echo "<br/>";
        break;
    }
    if(strcmp($runStatus, "completed") ==0){
        echo "In completed... <br/>";
        break;
    }
    sleep(1); 
}

echo "Run complete<br/>";

?>

The result:

Creating thread...
Thread created, id:thread_ZvHJYWZjoYXf04CR6BJbw67C
Adding message Hello World...
Message created, id:msg_Z1Y6Ei4j9b5EKI48BtSzHvot
Creating a run...
Run created, id:run_jR2oc8y3wLqWS8jwfcizK70I
Waiting for run result...
In progres...
Failed...
{
  "id": "run_jR2oc8y3wLqWS8jwfcizK70I",
  "object": "thread.run",
  "created_at": 1710458772,
  "assistant_id": "asst_wYzcGyBRwvpcKY3G4O0fEM5A",
  "thread_id": "thread_ZvHJYWZjoYXf04CR6BJbw67C",
  "status": "failed",
  "started_at": 1710458772,
  "expires_at": null,
  "cancelled_at": null,
  "failed_at": 1710458773,
  "completed_at": null,
  "last_error": {
    "code": "server_error",
    "message": "Sorry, something went wrong."
  },
  "model": "gpt-4-1106-preview",
  "instructions": "You're helping debugging why the api calls to you are not working. Please share everything , all technical details you know about the interaction and the communication aspects. ",
  "tools": [],
  "file_ids": [],
  "metadata": [],
  "usage": {
    "prompt_tokens": 0,
    "completion_tokens": 0,
    "total_tokens": 0
  }
}
Run complete

When I use the api to create the tread and message and use the platform.openai.com website the run completes fine. (I used an online json formatter to format it nicely as above.)

当我使用 API 创建对话和消息,并在 platform.openai.com 网站上运行时,它能够顺利完成。(我使用了在线 JSON 格式化工具,将其格式化得如上所示。)

The error message is: Sorry, something went wrong.

错误信息是:抱歉,出现了一些问题。

Thanks for any help!

问题解决:

The problem was with OpenAI and not with the code. I used an api key restricted to threads write access, thread write access and assistant read access and these did not work.

问题出在 OpenAI 而不是代码。我使用了一个仅限于对话写入访问、对话写入访问和助手读取访问的 API 密钥,但这些都不起作用。

I set the api key to full access and now it works.

我将 API 密钥设置为完全访问权限后,现在可以正常工作了。


http://www.kler.cn/news/318412.html

相关文章:

  • Go unique包:突破字符串局限的通用值Interning技术实现
  • 【M-LOAM学习】
  • 【Java】关键字-abstract【主线学习笔记】
  • 《算法笔记》例题解析 第3章入门模拟--4日期处理(9题)2021-03-03
  • 直流负载箱的主要功能有哪些?
  • 数字化转型中的供应链管理优化
  • 鸭脖变“刺客”,啃不起了
  • Leetcode算法基础篇-贪心算法
  • 输入5个数,求中值,verilog实现
  • more、less 命令:阅读文本
  • 电商效果图渲染神器:轻松高效出图
  • [docker][软件]docker快速安装rabbitmq
  • 【Rust语言】std::collections::HashMap用法
  • Linux环境下安装部署MySQL8.0以上(内置保姆级教程) C语言
  • Oracle数据库expdp与impdp
  • 基于SpringBoot+Vue+MySQL的网上租赁系统
  • CVPR最牛图像评价算法!
  • webview2加载本地页面
  • 「JavaScript深入」一文吃透JS的基本数据类型 Symbol
  • 统信服务器操作系统【Cron定时任务服务】
  • 安装程序不用鼠标,Windows也玩程序包管理存储库
  • 敏感词过滤
  • uni-app 多环境配置
  • 项目实战 (15)--- 代码区块重构及相关技术落地
  • 8月份,AI图像生成领域web端产品排行榜及产品是做什么的
  • UniApp一句话经验: px -> rpx动态转换和动态元素区域的获取
  • 前端-js例子:tab切换
  • 如何使用爬虫挖掘更多长尾关键词
  • HashMap五大核心问题总结
  • SpringMVC后续4