openai-php / laravel

⚡️ OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with OpenAI API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can you change the fake chat response text when testing?

davidyell opened this issue · comments

It doesn't seem possible to fake a Chat response with custom output from what I can understand.

For example if we follow the docs to create a fake json response.

use OpenAI\Laravel\Facades\OpenAI;
use OpenAI\Responses\Chat\CreateResponse;
use OpenAI\Responses\Chat\CreateResponseChoice;

OpenAI::fake([
    CreateResponse::fake([
        'choices' => [
            [
                CreateResponseChoice::from([
                    'index' => 0,
                    'message' => [
                        'role' => 'assistant',
                        'content' => json_encode([
                            'Fruits' => [
                                'banana',
                                'guava',
                                'tangerine',
                                'dragon fruit'
                            ]
                        ]),
                    ],
                ]),
            ],
        ],
    ]),
]);

For example this will always return a OpenAI\Responses\Chat\CreateResponseMessage with content of Hello there, this is a fake chat response.

Screenshot 2024-04-17 at 15 34 05

I have managed to sidestep this issue by loading all the attributes from the fixture that the fakeable uses and changing the bit I wanted.

It seems you need to match exactly, so this seems to work, using the base client fixture https://github.com/openai-php/client/blob/main/src/Testing/Responses/Fixtures/Chat/CreateResponseFixture.php

use OpenAI\Responses\Chat\CreateResponse;
use OpenAI\Testing\Responses\Fixtures\Chat\CreateResponseFixture;

$fakeOverrides = CreateResponseFixture::ATTRIBUTES;
$fakeOverrides['choices'][0]['message']['content'] = 'My fake ai chat response text for my test';

$response = CreateResponse::fake($fakeOverrides);