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...
- The JSON array contains all three of a
baseUrl
key-value AND a apiKey
key-value AND a apiVersion
key-value, XOR,
- 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
- 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
- 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
- Install Drupal core using the Standard install profile
- Go to
/admin/modules
and install the Document OCR module.
- Go to
/admin/config/structure/document-ocr/transformers
and click the Add transformer button.
- 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...
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
- Write a patch
- Review and feedback
- RTBC and feedback
- Commit
- Release
User interface changes
None.
API changes
None.
Data model changes
None.