orhanerday / open-ai

OpenAI PHP SDK : Most downloaded, forked, contributed, huge community supported, and used PHP (Laravel , Symfony, Yii, Cake PHP or any PHP framework) SDK for OpenAI GPT-3 and DALL-E. It also supports chatGPT-like streaming. (ChatGPT AI is supported)
https://orhanerday.gitbook.io/openai-php-api-1/
MIT License
2.26k stars 285 forks source link

The data returned is confusing when use stream #109

Closed muzilong closed 1 year ago

muzilong commented 1 year ago

Describe the bug

when i Log::info $data like this

$this->openAi->chat([
                'model' => 'gpt-3.5-turbo-16k',
                'messages' => $temp,
                'temperature' => 1.0,
                'max_tokens' => 3000,
                'frequency_penalty' => 0,
                'presence_penalty' => 0,
                'stream' => true
            ], function ($curl_info, $data) use ($gptArticle) {
                Log::info($data);

Sometimes it is correct:

[2023-10-12 10:05:02] local.INFO: data: {"id":"chatcmpl-88fLfcuOH33GOKQ49iq2vXAuzCHZQ","object":"chat.completion.chunk","created":1697076259,"model":"gpt-3.5-turbo-16k-0613","choices":[{"index":0,"delta":{"content":" air"},"finish_reason":null}]}

[2023-10-12 10:05:02] local.INFO: data: {"id":"chatcmpl-88fLfcuOH33GOKQ49iq2vXAuzCHZQ","object":"chat.completion.chunk","created":1697076259,"model":"gpt-3.5-turbo-16k-0613","choices":[{"index":0,"delta":{"content":" compressor"},"finish_reason":null}]}

[2023-10-12 10:05:02] local.INFO: data: {"id":"chatcmpl-88fLfcuOH33GOKQ49iq2vXAuzCHZQ","object":"chat.completion.chunk","created":1697076259,"model":"gpt-3.5-turbo-16k-0613","choices":[{"index":0,"delta":{"content":" guarantees"},"finish_reason":null}]}

[2023-10-12 10:05:02] local.INFO: data: {"id":"chatcmpl-88fLfcuOH33GOKQ49iq2vXAuzCHZQ","object":"chat.completion.chunk","created":1697076259,"model":"gpt-3.5-turbo-16k-0613","choices":[{"index":0,"delta":{"content":" the"},"finish_reason":null}]}

[2023-10-12 10:05:02] local.INFO: data: {"id":"chatcmpl-88fLfcuOH33GOKQ49iq2vXAuzCHZQ","object":"chat.completion.chunk","created":1697076259,"model":"gpt-3.5-turbo-16k-0613","choices":[{"index":0,"delta":{"content":" highest"},"finish_reason":null}]}

[2023-10-12 10:05:02] local.INFO: data: {"id":"chatcmpl-88fLfcuOH33GOKQ49iq2vXAuzCHZQ","object":"chat.completion.chunk","created":1697076259,"model":"gpt-3.5-turbo-16k-0613","choices":[{"index":0,"delta":{"content":" standard"},"finish_reason":null}]}

Sometimes it is trouble:

[2023-10-12 09:45:10] local.INFO: data: {"id":"chatcmpl-88f382uaVhRbGxjRK8xW2MsNW4832","object":"chat.completion.chunk","created":1697075110,"model":"gpt-3.5-turbo-16k-0613","c  
[2023-10-12 09:45:11] local.INFO: hoices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-88f382uaVhRbGxjRK8xW2MsNW4832","object":"chat.completion.chunk","created":1697075110,"model":"gpt-3.5-turbo
[2023-10-12 09:45:11] local.INFO: -16k-0613","choices":[{"index":0,"delta":{"content":"#"},"finish_reason":null}]}

data: {"id":"chatcmpl-88f382uaVhRbGxjRK8xW2MsNW4832","object":"chat.completion.chunk","created":1697075110,"model":"gpt-3.5-turbo-16k
[2023-10-12 09:45:11] local.INFO: -0613","choices":[{"index":0,"delta":{"content":" The"},"finish_reason":null}]}

data: {"id":"chatcmpl-88f382uaVhRbGxjRK8xW2MsNW4832","object":"chat.completion.chunk","created":1697075110,"model":"gpt-3.5-turbo-16  
[2023-10-12 09:45:11] local.INFO: k-0613","choices":[{"index":0,"delta":{"content":" Power"},"finish_reason":null}]}

data: {"id":"chatcmpl-88f382uaVhRbGxjRK8xW2MsNW4832","object":"chat.completion.chunk","created":1697075110,"model":"gpt-3.5-turbo-16k-061
[2023-10-12 09:45:11] local.INFO: 3","choices":[{"index":0,"delta":{"content":"house"},"finish_reason":null}]}

data: {"id":"chatcmpl-88f382uaVhRbGxjRK8xW2MsNW4832","object":"chat.completion.chunk","created":1697075110,"model":"gpt-3.5-turbo-16k  
[2023-10-12 09:45:11] local.INFO: -0613","choices":[{"index":0,"delta":{"content":" of"},"finish_reason":null}]}

data: {"id":"chatcmpl-88f382uaVhRbGxjRK8xW2MsNW4832","object":"chat.completion.chunk","created":1697075110,"model":"gpt-3.5-turbo-16k
[2023-10-12 09:45:11] local.INFO: -0613","choices":[{"index":0,"delta":{"content":" Efficiency"},"finish_reason":null}]}

data: {"id":"chatcmpl-88f382uaVhRbGxjRK8xW2MsNW4832","object":"chat.completion.chunk","created":1697075110,"model":"gpt-3.5-  
[2023-10-12 09:45:11] local.INFO: turbo-16k-0613","choices":[{"index":0,"delta":{"content":":"},"finish_reason":null}]}

data: {"id":"chatcmpl-88f382uaVhRbGxjRK8xW2MsNW4832","object":"chat.completion.chunk","created":1697075110,"model":"gpt-3.5-turbo
[2023-10-12 09:45:11] local.INFO: -16k-0613","choices":[{"index":0,"delta":{"content":" Un"},"finish_reason":null}]}

Very confused

To Reproduce

null

Code snippets

No response

OS

Ubuntu 20.04.5 LTS x86_64

PHP version

8.1.19

Library version

4.8

xing61 commented 1 year ago

I found this too,any solution??

orhanerday commented 1 year ago

Could you please try the same request using the official OpenAI Python library? If there are no issues with Python and an issue exists with the PHP library, please reopen the ticket.

Py code should be like this:

response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo-16k",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)
pingfuwei commented 11 months ago

@orhanerday Hello, has this problem been resolved,

orhanerday commented 11 months ago

@pingfuwei did you try on Python package with the same request?

pingfuwei commented 11 months ago

The project is for PHP development, and I need to find a solution for PHP

------------------ 原始邮件 ------------------ 发件人: "orhanerday/open-ai" @.>; 发送时间: 2023年11月9日(星期四) 下午4:14 @.>; @.**@.>; 主题: Re: [orhanerday/open-ai] The data returned is confusing when use stream (Issue #109)

@pingfuwei did you try on Python package with the same request?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

orhanerday commented 11 months ago

If you got the same issue on python package, it is OpenAI related issue.

muzilong commented 11 months ago

I'm sorry for not giving timely feedback.

Yeah, it is OpenAI related issue.

When I reported the problem, openai was in trouble.

When openai fixed the problem everything was resolved.

thank you all the same

iamaz007 commented 11 months ago

This bug still not resolved yet by OpenAi

For now, I have resolved it using regex, I'm extracting content directly from respone.

// Define the pattern to match
$pattern = '/"choices":\[\{"index":0,"delta":\{"content":"(.*?)"\}/';

// Use preg_match_all to find all matches
preg_match_all($pattern, $inputData, $matches);

// Extract content from the matches
$contents = $matches[1];
orhanerday commented 11 months ago

Hi @iamaz007 Did you contact OpenAI?

iamaz007 commented 11 months ago

Nope, they don't even reply for hours.

I tested libraries, in Flutter, that issue is happening there too. so, for now we have to solve it on our own.

orhanerday commented 11 months ago

Could you please ask this question on the OpenAI form? Maybe we can get better help from the community.

Please drop the post link over here. Thank you!