Comments (5)
When using the example above
use OpenAI\Laravel\Facades\OpenAI;
OpenAI::shouldReceive('completions')->andReturn([]);
The following error is returned
The class \OpenAI\Client is marked final and its methods cannot be replaced. Classes marked final can be passed in to \Mockery::mock() as instantiated objects to create a partial mock, but only if the mock is not subject to type hinting checks.
I also tried mocking OpenAI\Client
replacing the instance in the service container, but that class is also marked final so I get the same issue.
$this->instance(
OpenAI::class,
Mockery::mock(OpenAI::class, function ($mock) {
$mock
->shouldReceive('completions')
->once()
->andReturn(['...']);
})
);
Presumably I'm missing something. As it stands I'm just having to work around it using my own method as a wrapper, then mocking my own method to remove the call to OpenAI,
use OpenAI\Laravel\Facades\OpenAI;
class MockableOpenAI
{
public function completions($prompt)
{
return OpenAI::completions()->create([
'model' => 'text-davinci-003',
'prompt' => $prompt,
]);
}
}
...
$this->instance(
MockableOpenAI::class,
Mockery::mock(MockableOpenAI::class, function ($mock) {
$mock
->shouldReceive('completions')
->once()
->andReturn(['OpenAI Output']);
})
);
from laravel.
@adrianpaiva1 @benswinburne
Try this
$this->swap(Client::class, new \Mockery());
OpenAI::shouldReceive('completions')->andReturnSelf();
OpenAI::shouldReceive('create')->andReturn([
'choices' => [['text' => 'test']],
'usage' => []
]);
from laravel.
The example you provided should work.
from laravel.
It didn't work for me neither, I will follow the same approach as @benswinburne. In my case if I could mock ThreadRunResponse
it will be good enough I think.
from laravel.
@benswinburne I just found a solution that worked perfectly for me.
https://tomasvotruba.com/blog/2019/03/28/how-to-mock-final-classes-in-phpunit
https://github.com/dg/bypass-finals
Regards
from laravel.
Related Issues (20)
- How can I use factory for Azure HOT 1
- Not working even after installing. HOT 7
- Getting error by using model " gpt-3.5-turbo " HOT 3
- How to use HTTP Proxy? HOT 1
- Undefined array key "permission" HOT 1
- Bug with GPT Vision ? HOT 4
- Fluent method to specify API key (multi tenancy) HOT 3
- Streaming with GPT4 Turbo Vision Preview in Laravel HOT 3
- OPEN AI GPT turbo 3.5 (text-davinci-003) HOT 5
- Switch to Laravel HTTP client for compatibility with Pulse, Telescope, Sentry etc HOT 7
- OpenAI \ Exceptions \ UnserializableResponse Syntax error HOT 1
- Readme is ahead current version
- change Endpoint to Azure HOT 1
- Question: JSON mode support? HOT 4
- ERROR There are no commands defined in the "openai" namespace. HOT 2
- Assistant API not supported in facade
- Send images to the api with model gpt-4-vision-preview to analyze the image HOT 1
- Install command HOT 2
- Supply and Delivery
- Upgrade openai-php/client package to latest version HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel.