Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting an error when I try to run the chat() method #91

Closed
amidonsolutions opened this issue Mar 27, 2023 · 9 comments
Closed

Getting an error when I try to run the chat() method #91

amidonsolutions opened this issue Mar 27, 2023 · 9 comments

Comments

@amidonsolutions
Copy link

Thank you for providing this code. When I try ..., I get this error:

Fatal error: Uncaught Error: Call to undefined method OpenAI\Client::chat()

I've been able to run the code snippets fine as I work down the page. I get this error when I try the chat method. Any suggestions? Thanks!

@gehrisandro
Copy link
Collaborator

Hi @amidonsolutions

Please provide more context otherwise I don't know how to help.

@amidonsolutions
Copy link
Author

Thank you. Here is my code. I've added all libraries. I have other code snippets working fine like embeddings and completions. For some reason, chat is throwing that error:

chat()->create([ 'model' => 'gpt-3.5-turbo', 'messages' => [ ['role' => 'user', 'content' => 'Hello!'], ], ]); $response->id; // 'chatcmpl-6pMyfj1HF4QXnfvjtfzvufZSQq6Eq' $response->object; // 'chat.completion' $response->created; // 1677701073 $response->model; // 'gpt-3.5-turbo-0301' foreach ($response->choices as $result) { $result->index; // 0 $result->message->role; // 'assistant' $result->message->content; // '\n\nHello there! How can I assist you today?' $result->finishReason; // 'stop' } $response->usage->promptTokens; // 9, $response->usage->completionTokens; // 12, $response->usage->totalTokens; // 21 $response->toArray(); // ['id' => 'chatcmpl-6pMyfj1HF4QXnfvjtfzvufZSQq6Eq', ...]

@amidonsolutions
Copy link
Author

amidonsolutions commented Mar 27, 2023

`<?php

require 'vendor/autoload.php';

$client = OpenAI::client('MY_API_KEY');

$response = $client->chat()->create([
'model' => 'gpt-3.5-turbo',
'messages' => [
['role' => 'user', 'content' => 'Hello!'],
],
]);

$response->id; // 'chatcmpl-6pMyfj1HF4QXnfvjtfzvufZSQq6Eq'
$response->object; // 'chat.completion'
$response->created; // 1677701073
$response->model; // 'gpt-3.5-turbo-0301'

foreach ($response->choices as $result) {
$result->index; // 0
$result->message->role; // 'assistant'
$result->message->content; // '\n\nHello there! How can I assist you today?'
$result->finishReason; // 'stop'
}

$response->usage->promptTokens; // 9,
$response->usage->completionTokens; // 12,
$response->usage->totalTokens; // 21

$response->toArray(); // ['id' => 'chatcmpl-6pMyfj1HF4QXnfvjtfzvufZSQq6Eq', ...]
`

THrows this error:

Fatal error: Uncaught Error: Call to undefined method OpenAI\Client::chat()

@gehrisandro
Copy link
Collaborator

Most likely you are using a package version below v0.3.3

make sure you are using the latest version v0.4.1

@amidonsolutions
Copy link
Author

Thank you. I ran this in my terminal just now:

composer require openai-php/client
Info from https://repo.packagist.org: #StandWithUkraine
./composer.json has been updated
Running composer update openai-php/client
Loading composer repositories with package information
Updating dependencies
Nothing to modify in lock file
Writing lock file
Installing dependencies from lock file (including require-dev)
Nothing to install, update or remove
Generating autoload files
10 packages you are using are looking for funding.
Use the composer fund command to find out more!
No security vulnerability advisories found
Using version ^0.4.1 for openai-php/client

@gehrisandro
Copy link
Collaborator

Could you please submit the full stacktrace of your exception?

@amidonsolutions
Copy link
Author

Fatal error: Uncaught Error: Call to undefined method OpenAI\Client::chat() in /home/.../php-example.php:7 Stack trace: #0 {main} thrown in /home/.../php-example.php on line 7

@gehrisandro
Copy link
Collaborator

When I copy and paste your code into my application it works fine.

@amidonsolutions
Copy link
Author

Thank you. Figured this out. Had to run composer for latest version of openai and your code in the directory where my code was. Had an older version which was working for other methods but not chat. Now working. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants