题意:从 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 密钥设置为完全访问权限后,现在可以正常工作了。