README for OpenAI processor credentials is wrong

Created on 17 March 2025, about 2 months ago

Problem/Motivation

The README for adding an OpenAI processor is wrong, especially if you don't have an OpenAI organization.

The README says...

3. Create JSON file and store your credentials as follows:
```
{
  "apikey": "[API-KEY]",
  "apiorg": "[API-ORG]" (optional)
}
```

But \Drupal\document_ocr\Services\OpenAi::setCredentials() looks like...

try {
      if (!empty($credentials['baseUrl']) && !empty($credentials['apiKey']) && !empty($credentials['apiVersion'])) {
        $this->client = \OpenAI::factory()
          ->withBaseUri($credentials['baseUrl'])
          ->withHttpHeader('api-key', $credentials['apiKey'])
          ->withQueryParam('api-version', $credentials['apiVersion'])
          ->make();
      }
      else {
        $org = !empty($credentials['apiorg']) ? $credentials['apiorg'] : NULL;
        if (!empty($credentials['apikey'])) {
          $this->client = \OpenAI::client($credentials['apikey'], $org);
        }
        else {
          $this->loggerFactory->error('Missing or incorrect OpenAI API Key');
        }
      }
    }
    catch (\Exception $ex) {
      $this->loggerFactory->error($ex->getMessage());
    }
    return $this;

That is to say, it will only create $this->client if...

  1. The JSON array contains all three of a baseUrl key-value AND a apiKey key-value AND a apiVersion key-value, XOR,
  2. The JSON array contains both a apiorg key-value AND a apikey key-value

The README file should say...

3. Create JSON file and store your credentials as follows:
```
{
  "apikey": "[your API key]",
  "apiorg": "[API-ORG]"
}
```
... or...
```
{
  "baseUrl": "api.openai.com/v1",
  "apiKey": "[your API key]",
  "apiVersion": "v1"
}
```

Steps to reproduce

  1. Set up a site with ddev-drupal-contrib:
    git clone --branch 1.0.x https://git.drupalcode.org/project/document_ocr.git document_ocr
    cd document_ocr
    ddev config --project-type=drupal --docroot=web --php-version=8.3 --corepack-enable --project-name=document-ocr
    ddev add-on get ddev/ddev-drupal-contrib
    ddev start
    ddev poser
    ddev symlink-project
    ddev launch
    
  2. Create a file according to the (old) README with your credentials (I put mine in the public file system as an example: NEVER DO THIS on a public site):
    mkdir -p web/sites/default/files/document-ocr
    echo <<EOF | tee -a web/sites/default/files/document-ocr/open-ai.json
    {
      "apiKey": "[API-KEY]",
      "apiorg": "[API-ORG]"
    }
    EOF
    
  3. Install Drupal core using the Standard install profile
  4. Go to /admin/modules and install the Document OCR module.
  5. Go to /admin/config/structure/document-ocr/transformers and click the Add transformer button.
  6. Enter a Label, set Transformer = OpenAI -> OpenAI Chat, and set Transformer Credentials = public://document-ocr/open-ai.json, then click the Continue to Configuration button...
    • Expected behavior: you see a configuration page
    • Actual behavior: you see the following error message:
      The website encountered an unexpected error. Try again later.
      
      Error: Call to a member function models() on null in Drupal\document_ocr\Services\OpenAi->getModels() (line 49 of /var/www/html/src/Services/OpenAi.php).
      
      Drupal\document_ocr\Repository\OpenAiModelsRepository->getModels() (Line: 92)
      Drupal\document_ocr\Plugin\document_ocr\transformer\OpenAiChat->configurationForm() (Line: 34)
      Drupal\document_ocr\Form\Transformer\ConfigurationForm->buildForm()
      call_user_func_array() (Line: 528)
      Drupal\Core\Form\FormBuilder->retrieveForm() (Line: 279)
      Drupal\Core\Form\FormBuilder->buildForm() (Line: 73)
      Drupal\Core\Controller\FormController->getContentResult()
      call_user_func_array() (Line: 123)
      Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 593)
      Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
      Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
      Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 183)
      Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
      Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
      Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
      Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
      Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
      Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 116)
      Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 90)
      Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
      Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
      Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 36)
      Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 51)
      Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 709)
      Drupal\Core\DrupalKernel->handle() (Line: 19)
      

Proposed resolution

Update the README file...

diff --git a/README.md b/README.md
index 95dbf12..c3c317f 100644
--- a/README.md
+++ b/README.md
@@ -108,8 +108,16 @@ Enable Translate service and get the API credentials. Use the following format f
 3. Create JSON file and store your credentials as follows:
 ```
 {
   "apikey": "[API-KEY]",
-  "apiorg": "[API-ORG]" (optional)
+  "apiorg": "[API-ORG]"
+}
+```
+... or...
+```
+{
+  "baseUrl": "api.openai.com/v1",
+  "apiKey": "[your API key]",
+  "apiVersion": "v1"
 }
 ```

Remaining tasks

  1. Write a patch
  2. Review and feedback
  3. RTBC and feedback
  4. Commit
  5. Release

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇨🇦Canada mparker17 UTC-4

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024