How do you implement OpenAI GPT-3 Api Client in PHP?

题意:如何在 PHP 中实现 OpenAI GPT-3 API 客户端?

问题背景:

I need help understanding the vague instructions on https://packagist.org/packages/orhanerday/open-ai

我需要帮助来理解这些模糊的说明...

I downloaded the package from https://github.com/orhanerday/open-ai

我从 https://github.com/orhanerday/open-ai 下载了这个包。

I installed the package by running "composer require orhanerday/open-ai" in my Command Prompt

我通过在命令提示符中运行 composer require orhanerday/open-ai 安装了这个包。

Instructions stop making sense from there.....

从那以后,说明就不太清楚了......

What does the "use Orhanerday\OpenAi\OpenAi;" code mean and where is it applied? Am I to create a php file say index.php with content:

代码 use Orhanerday\OpenAi\OpenAi; 是什么意思,应该在哪里使用?我是要创建一个包含以下内容的 PHP 文件,比如 index.php 吗?

php 复制代码
<?php

use Orhanerday\OpenAi\OpenAi;

$complete = $open_ai->complete([

   'engine' => 'davinci',
   'prompt' => 'Hello',
   'temperature' => 0.9,
   'max_tokens' => 150,
   'frequency_penalty' => 0,
   'presence_penalty' => 0.6,
]
?>

how and where do I add my api key? Do I create a file Orhanerday\OpenAi\OpenAi.php and enter my api key there? i.e. OPENAI_API_KEY=sk-**********************************************

我应该如何以及在哪里添加我的 API 密钥?我需要创建一个 Orhanerday\OpenAi\OpenAi.php 文件并在其中输入我的 API 密钥吗?例如 OPENAI_API_KEY=sk-**********************************************

问题解决:

You should define the $open_ai variable as an OpenAI object by passing your private KEY value. for example; new OpenAi('Your-OPENAI-KEY');

你应该通过传入你的私密密钥值,将 $open_ai 变量定义为一个 OpenAI 对象。例如:new OpenAi('Your-OPENAI-KEY');

Complete code; 完整的代码

php 复制代码
<?php


use Orhanerday\OpenAi\OpenAi;

$open_ai = new OpenAi('OPEN-AI-KEY');// <- define the variable.

$complete = $open_ai->complete([
    'engine' => 'davinci',
    'prompt' => 'Hello',
    'temperature' => 0.9,
    'max_tokens' => 150,
    'frequency_penalty' => 0,
    'presence_penalty' => 0.6,
]);

I also added the Quick Start Part to orhanerday/OpenAI readme.

我还在 orhanerday/OpenAI 的自述文件中添加了快速入门部分。

相关推荐
TrisighT30 分钟前
Agent 的工具调用我从来不用官方推荐那套写法——4 个我实测下来更稳的隐藏用法
aigc·openai·cursor
AINative软件工程40 分钟前
别只会重试:LLM JSON 失败 5 类修复法
openai
仙逆GPT1 小时前
GPT-5.6在ChatGPT中如何显示?模型入口与推理档位说明
chatgpt·openai·sol·gpt-5.6
武子康1 小时前
🔥 GPT-5.6 有限预览全拆解:Sol/Terra/Luna 三档定价 + max/ultra 双推理 + 缓存 1.00×/1.25×/0.10× 三倍
人工智能·chatgpt·openai
武子康2 小时前
OpenAI Workspace Agents 进入信用计费:Coding Agent 从额度体验走向成本治理
人工智能·openai·claude
云卷云舒___________2 小时前
OpenAI 发布 ChatGPT Work,Codex 并入 ChatGPT,Kimi Code 推出 K2.7 高速模式 | 7月10日 AI日报
chatgpt·openai·codex·ai日报·chatgptwork·kimicode·k27
不爱记笔记3 小时前
GPT-5.6 Sol 真实项目实测!Bug修复、UI重构与帆船游戏复刻
人工智能·gpt·ui·chatgpt·bug·openai
weixin_BYSJ198715 小时前
SpringBoot + MySQL 乒乓球运动员信息管理系统项目实战--附源码04954
java·javascript·spring boot·python·django·flask·php
VIP_CQCRE18 小时前
用 Ace Data Cloud 快速接入 OpenAI Chat Completions:对话、流式、多轮和多模态一篇打通
python·ai·openai·api·教程
用户30745969820721 小时前
GatewayWorker 从零到一完整指南
javascript·php