dirk1983 / chatgpt

演示站现在可以免费使用ChatGPT对话和画图了。全网最易部署,响应速度最快的ChatGPT环境。PHP版调用OpenAI接口进行问答和画图,采用Stream流模式通信,一边生成一边输出。前端采用EventSource,支持Markdown格式解析,支持公式显示,代码有着色处理,支持画图。页面UI简洁,支持上下文连续会话。源码只有几个文件,没用任何框架,支持所有PHP版本,全部开源,极易二开。保姆级教程,账号等周边资源,欢迎进群交流,一切全免费。

Home Page:https://mm1.ltd

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

能不能增加一个余额显示?

yzq313 opened this issue · comments

前台能不能增加一个账户余额显示?

你指的是让用户自行输入key的时候显示余额?

你指的是让用户自行输入key的时候显示余额?

对的

commented

余额调用方式如下

$ curl -s  "https://api.openai.com/dashboard/billing/credit_grants" \
--header 'Content-type: application/json' \
--header 'Authorization: Bearer api_key' | jq    # 这里替换api_key
{
  "object": "credit_summary",
  "total_granted": 18,
  "total_used": 0.178104,  # 已用
  "total_available": 17.821896,  # 剩余可用余额
  "grants": {
    "object": "list",
    "data": [
      {
        "object": "credit_grant",
        "id": "be32c280-ad4a-4e14-bc1a-077a32275f9e",
        "grant_amount": 18,
        "used_amount": 0.178104,
        "effective_at": 1677628800,
        "expires_at": 1688169600
      }
    ]
  }
}

已经有热心网友给了代码,有需要的话自己加上吧

commented

We are testing this in the classroom. Simple example below but does not always show the available balance:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/dashboard/billing/credit_grants');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer sk-YOUR-KEY-HERE';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

$data = json_decode($result,true);

echo "<table>";
foreach ($data as $key => $value) {
    echo "<tr>";
    echo "<td>" . $key . "</td>";
    echo "<td>" . $value . "</td>";
    echo "</tr>";
}
echo "</table>";

?>
commented

We are testing this in the classroom. Simple example below but does not always show the available balance:

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/dashboard/billing/credit_grants');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer sk-YOUR-KEY-HERE';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

$data = json_decode($result,true);

echo "<table>";
foreach ($data as $key => $value) {
    echo "<tr>";
    echo "<td>" . $key . "</td>";
    echo "<td>" . $value . "</td>";
    echo "</tr>";
}
echo "</table>";

?>

Did you try curl command? Did it work?

commented

@baby9 Unfortunately we can only use PHP CURL in the classroom, so cannot try in commandline.

commented

In case anyone following, this info may be of use:

<?php

// Set the start date (e.g., 1-30 days ago)
$start_date = date('Y-m-d', strtotime('-1 days')); //one day
// Set the end date (e.g., today)
$end_date = date('Y-m-d');

$ch = curl_init();

//curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/dashboard/billing/usage');
// Append the start_date and end_date parameters to the URL
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/dashboard/billing/usage?start_date=' . $start_date . '&end_date=' . $end_date);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');

$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer sk-YOUR-KEY-HERE';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
} else {
    // Print out the raw response body
    echo $result;
}
curl_close($ch);

// Decode the JSON response
$response = json_decode($result);

// Access and print the available credits
echo "Available credits: " . $response->available_credits;

?>

Returns lots of information.