Steps to reproduce

1: Installed the module using Composer with the command: composer require 'drupal/openai_client:^1.1'.

2: when we try to open "AI assistants" /admin/structure/assistants, it shows an error

TypeError: OpenAI::client(): Argument #1 ($apiKey) must be of type string, null given, called in /modules/openai_client/src/OpenAIClientWrapper.php on line 70 in OpenAI::client() (line 13 of /vendor/openai-php/client/src/OpenAI.php).

I've attached an image illustrating my actions—installing the module and searching within the extend panel.

CommentFileSizeAuthor
openai.png84.61 KBaman_lnwebworks
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

aman_lnwebworks created an issue. See original summary.

aman_lnwebworks’s picture

Assigned: aman_lnwebworks » Unassigned

Hi @psf_

I have created MR!3 for the above issue, Please review it once.

Thank you !!

psf_’s picture

Status: Active » Needs work

Hi, thx by the patch.

I have some notes:

- In "src/OpenAIClientWrapper.php" you were erased all comments, it's are required by code styling that we have comments.
- In the protected method getApiToken() we can't send any status message, because that will spawn the user with messages. This method is called so many times.

ankitv18 made their first commit to this issue’s fork.

ankitv18 changed the visibility of the branch 1.1.x to hidden.

ankitv18’s picture

Version: 1.1.3 » 1.1.x-dev
Status: Needs work » Needs review

@psf_ please review the MR!5 I guess we could use the ternary operator to handle the null value passed as token.

psf_’s picture

In this line:
$token = empty($token) ? $this->getApiToken() : '';

"$this->getApiToken()" can return a NULL value if the user don't configure the module.

I change your commit with:

public function getClient(string $token = ''): Client {
    if (empty($token)) {
      $token = $this->getApiToken();
    }
    $token = !$token ? '' : $token;
    return \OpenAI::client($token);
  }

But I don't tested it.

ankitv18’s picture

I can implement the same logic with combination or ternary and null coalescing operator.

psf_’s picture

I see it OK, but I don't tested it.

I'm so busy, when I get free I will try it, or apply it if anyother tested it.

Thx so much

  • psf_ committed 2aafaa02 on 1.1.x authored by ankitv18
    Issue #3456210: Fix Typeerror openaiclient argument.
    
psf_’s picture

Status: Needs review » Fixed

Merged, thx : ))

psf_’s picture

Status: Fixed » Closed (fixed)