Coder Social home page Coder Social logo

Comments (9)

gehrisandro avatar gehrisandro commented on August 20, 2024

Hi @amidonsolutions

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

from client.

amidonsolutions avatar amidonsolutions commented on August 20, 2024

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', ...]

from client.

amidonsolutions avatar amidonsolutions commented on August 20, 2024

`<?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()

from client.

gehrisandro avatar gehrisandro commented on August 20, 2024

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

make sure you are using the latest version v0.4.1

from client.

amidonsolutions avatar amidonsolutions commented on August 20, 2024

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

from client.

gehrisandro avatar gehrisandro commented on August 20, 2024

Could you please submit the full stacktrace of your exception?

from client.

amidonsolutions avatar amidonsolutions commented on August 20, 2024

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

from client.

gehrisandro avatar gehrisandro commented on August 20, 2024

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

from client.

amidonsolutions avatar amidonsolutions commented on August 20, 2024

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.

from client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.