PHP CURL 打开 AI 移除 POST 数据中的引号

huangapple go评论58阅读模式
英文:

PHP CURL Open Ai Remove The Quotes From Post Data

问题

以下是我的PHP Curl脚本,用于向Open AI发布请求,一切都按预期工作。

我想要能够从发布的数据中设置数值,就像这样。

$getOpenAITemperature = $_POST['OpenAITemperature'];
$getmaxtokens = $_POST['OpenAIMaximumLength'];
$getTopp = $_POST['OpenAITopP'];

但是当我这样做时,它会在发布的值周围添加引号,导致它停止工作。

像这样。

$postData = [
  'model' => $getOpenAIModel,
  'prompt' => $getRequest,
  'temperature' => "0.24",
  'max_tokens' => "250",
  'top_p' => "1",
];

但为了使其工作,它需要看起来像这样。

$postData = [
  'model' => $getOpenAIModel,
  'prompt' => $getRequest,
  'temperature' => 0.24,
  'max_tokens' => 250,
  'top_p' => 1,
];

如何去掉数字周围的引号?模型和提示周围的引号是可以的,只是数字不需要引号。

*** 下面的脚本工作正常 ***

$getOpenAITemperature = 0.5;
$getmax_tokens = 250;
$gettop_p = 1;

$OPENAI_API_KEY = "sk-123";
$getOpenAIModel = "text-davinci-003";
$getRequest = "My Question";
$ch = curl_init();
$headers = [
  'Accept: application/json',
  'Content-Type: application/json',
  'Authorization: Bearer ' . $OPENAI_API_KEY,
];
$postData = [
  'model' => $getOpenAIModel,
  'prompt' => $getRequest,
  'temperature' => $getOpenAITemperature,
  'max_tokens' => $getTopp,
  'top_p' => $getmax_tokens,
  'best_of' => 2,
  'frequency_penalty' => 0.0,
  'presence_penalty' => 0.0,
  'stop' => '["\n"]',
];

curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));

$result = curl_exec($ch);

我已经尝试了一些方法,如php的trim()str_replace,但没有什么用。

英文:

Below is my PHP Curl script that works to post to Open AI, this is all working as it should.

I want to be able to set the values from posted data like this.

 $getOpenAITemperature = $_POST[OpenAITemperature]; 
 $getmaxtokens = $_POST[OpenAIMaximumLength]; 
 $getTopp = $_POST[OpenAITopP];`

But when I do it added quotes to the posted values and it stops working.

Like this.

 $postData = [
  'model' => $getOpenAIModel,
  'prompt' => $getRequest,
  'temperature' => "0.24",
  'max_tokens => "250",
  'top_p' => "1",

But it needs to look like this to work.

 $postData = [
   'model' => $getOpenAIModel,
   'prompt' => $getRequest,
   'temperature' => 0.24,
   'max_tokens => 250,
   'top_p' => 1,

How can I remove the quotes around the numbers ? The quotes around the model and prompt are fine its just the numbers.

*** The script below here works fine ***

  $getOpenAITemperature = 0.5;
  $getmax_tokens = 250;
  $gettop_p = 1;


  $OPENAI_API_KEY = "sk-123";
  $getOpenAIModel = "text-davinci-003";
  $getRequest "My Question";
  $ch = curl_init();
  $headers  = [
        'Accept: application/json',
        'Content-Type: application/json',
        'Authorization: Bearer '.$OPENAI_API_KEY.''
    ];
 $postData = [
   'model' => $getOpenAIModel,
   'prompt' => $getRequest,
   'temperature' => $getOpenAITemperature,
   'max_tokens' => $getTopp,
   'top_p' => $getmaxtokens,
   'best_of' => 2,
   'frequency_penalty' => 0.0,
   'presence_penalty' => 0.0,
    'stop' => '["\n"]',
  ];

  curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData)); 

  $result = curl_exec($ch);`

I have tried a number of things like php trim() and str_replace but nothing worked.

答案1

得分: 0

你可以将字符串转换为整数或浮点数,如下所示:

$postData['temperature'] = (float) $postData['temperature'];
$postData['max_tokens'] = (int) $postData['max_tokens'];

查看 PHP 文档了解类型转换
https://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

英文:

You can cast the strings to an int or a float like so:

$postData['temperature'] = (float) $postData['temperature'];
$postData['max_tokens'] = (int) $postData['max_tokens'];

Check the PHP docs for type casting
https://www.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

huangapple
  • 本文由 发表于 2023年1月9日 03:45:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定