Skip to content

Commit fb04ee6

Browse files
committed
feat: add support for custom api, compatible with openai
1 parent a173a55 commit fb04ee6

File tree

12 files changed

+281
-60
lines changed

12 files changed

+281
-60
lines changed

_build/gpm.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ systemSettings:
2525
area: api
2626
description: "Your API key found at https://console.anthropic.com/settings/keys"
2727

28+
- key: api.custom.key
29+
type: text-password
30+
area: api
31+
- key: api.custom.url
32+
area: api
33+
- key: api.custom.compatibility
34+
area: api
35+
value: openai
36+
2837
- key: global.base.output
2938
name: 'Base Output Instructions'
3039
area: global

core/components/modai/src/Processors/Prompt/FreeText.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,8 @@ public function process()
3939
$systemInstructions[] = $base;
4040
}
4141

42-
$aiService = AIServiceFactory::new($model, $this->modx);
43-
4442
try {
43+
$aiService = AIServiceFactory::new($model, $this->modx);
4544
$result = $aiService->getCompletions([$prompt], CompletionsConfig::new($model)->maxTokens($maxTokens)->temperature($temperature)->systemInstructions($systemInstructions));
4645

4746
return $this->success('', ['content' => $result]);

core/components/modai/src/Processors/Prompt/Image.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ public function process()
3131
return $this->failure('image.quality setting is required');
3232
}
3333

34-
$aiService = AIServiceFactory::new($model, $this->modx);
35-
3634
try {
35+
$aiService = AIServiceFactory::new($model, $this->modx);
3736
$result = $aiService->generateImage($prompt, ImageConfig::new($model)->size($size)->quality($quality));
3837

3938
return $this->success('', ['url' => $result]);

core/components/modai/src/Processors/Prompt/Text.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ public function process()
7272
$systemInstructions[] = $fieldPrompt;
7373
}
7474

75-
$aiService = AIServiceFactory::new($model, $this->modx);
76-
7775
try {
76+
$aiService = AIServiceFactory::new($model, $this->modx);
7877
$result = $aiService->getCompletions([$content], CompletionsConfig::new($model)->maxTokens($maxTokens)->temperature($temperature)->systemInstructions($systemInstructions));
7978

8079
return $this->success('', ['content' => $result]);

core/components/modai/src/Processors/Prompt/Vision.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ public function process()
2525
return $this->failure('vision.prompt setting is required');
2626
}
2727

28-
$aiService = AIServiceFactory::new($model, $this->modx);
29-
3028
try {
29+
$aiService = AIServiceFactory::new($model, $this->modx);
3130
$result = $aiService->getVision($prompt, $image, VisionConfig::new($model));
3231

3332
return $this->success('', ['content' => $result]);

core/components/modai/src/Services/AIServiceFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ public static function new($model, modX &$modx): AIService {
1313
return new Claude($modx);
1414
}
1515

16+
if (substr($model, 0,7) === 'custom_') {
17+
$compatibility = $modx->getOption('modai.api.custom.compatibility');
18+
if ($compatibility === 'openai') {
19+
return new CustomChatGPT($modx);
20+
}
21+
22+
throw new \Error("Unsupported API compatibility mode (modai.api.custom.compatibility).");
23+
}
24+
1625
switch ($model) {
1726
case 'text-embedding-004':
1827
case 'learnlm-1.5-pro-experimental':

core/components/modai/src/Services/ChatGPT.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public function __construct(modX &$modx)
1919
}
2020

2121
public function generateImage(string $prompt, ImageConfig $config): string {
22-
$chatgptApiKey = $this->modx->getOption('modai.api.chatgpt.key');
23-
if (empty($chatgptApiKey)) {
22+
$apiKey = $this->modx->getOption('modai.api.chatgpt.key');
23+
if (empty($apiKey)) {
2424
throw new \Exception('Missing modai.api.chatgpt.key');
2525
}
2626

@@ -38,7 +38,7 @@ public function generateImage(string $prompt, ImageConfig $config): string {
3838
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($input));
3939
curl_setopt($ch, CURLOPT_HTTPHEADER, [
4040
'Content-Type: application/json',
41-
'Authorization: Bearer ' . $chatgptApiKey
41+
'Authorization: Bearer ' . $apiKey
4242
]);
4343

4444
$response = curl_exec($ch);
@@ -71,8 +71,8 @@ public function generateImage(string $prompt, ImageConfig $config): string {
7171
*/
7272
public function getCompletions(array $data, CompletionsConfig $config): string
7373
{
74-
$chatgptApiKey = $this->modx->getOption('modai.api.chatgpt.key');
75-
if (empty($chatgptApiKey)) {
74+
$apiKey = $this->modx->getOption('modai.api.chatgpt.key');
75+
if (empty($apiKey)) {
7676
throw new \Exception('Missing modai.api.chatgpt.key');
7777
}
7878

@@ -105,7 +105,7 @@ public function getCompletions(array $data, CompletionsConfig $config): string
105105
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($input));
106106
curl_setopt($ch, CURLOPT_HTTPHEADER, [
107107
'Content-Type: application/json',
108-
'Authorization: Bearer ' . $chatgptApiKey
108+
'Authorization: Bearer ' . $apiKey
109109
]);
110110

111111
$response = curl_exec($ch);
@@ -138,8 +138,8 @@ public function getCompletions(array $data, CompletionsConfig $config): string
138138
*/
139139
public function getVision(string $prompt, string $image, VisionConfig $config): string
140140
{
141-
$chatgptApiKey = $this->modx->getOption('modai.api.chatgpt.key');
142-
if (empty($chatgptApiKey)) {
141+
$apiKey = $this->modx->getOption('modai.api.chatgpt.key');
142+
if (empty($apiKey)) {
143143
throw new \Exception('Missing modai.api.chatgpt.key');
144144
}
145145

@@ -168,7 +168,7 @@ public function getVision(string $prompt, string $image, VisionConfig $config):
168168
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($input));
169169
curl_setopt($ch, CURLOPT_HTTPHEADER, [
170170
'Content-Type: application/json',
171-
'Authorization: Bearer ' . $chatgptApiKey
171+
'Authorization: Bearer ' . $apiKey
172172
]);
173173

174174
$response = curl_exec($ch);

core/components/modai/src/Services/Config/CompletionsConfig.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@
22
namespace modAI\Services\Config;
33

44
class CompletionsConfig {
5-
private string $model;
5+
use Model;
66
private float $temperature;
77
private int $maxTokens;
88
private array $systemInstructions = [];
99

10-
private function __construct(string $model)
11-
{
12-
$this->model = $model;
13-
}
14-
15-
public static function new(string $model): self {
16-
return new self($model);
17-
}
18-
1910
public function temperature(float $temperature): self {
2011
$this->temperature = $temperature;
2112

@@ -34,10 +25,6 @@ public function systemInstructions(array $systemInstructions): self {
3425
return $this;
3526
}
3627

37-
public function getModel(): string {
38-
return $this->model;
39-
}
40-
4128
public function getTemperature(): float
4229
{
4330
return $this->temperature;

core/components/modai/src/Services/Config/ImageConfig.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22
namespace modAI\Services\Config;
33

44
class ImageConfig {
5-
private string $model;
5+
use Model;
6+
67
private int $n = 1;
78
private string $size;
89
private string $quality;
910

10-
private function __construct(string $model)
11-
{
12-
$this->model = $model;
13-
}
14-
15-
public static function new(string $model): self {
16-
return new self($model);
17-
}
18-
1911
public function size(string $size): self {
2012
$this->size = $size;
2113

@@ -28,10 +20,6 @@ public function quality(string $quality): self {
2820
return $this;
2921
}
3022

31-
public function getModel(): string {
32-
return $this->model;
33-
}
34-
3523
public function getN(): int
3624
{
3725
return $this->n;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace modAI\Services\Config;
3+
4+
trait Model {
5+
private string $model;
6+
7+
private function __construct(string $model)
8+
{
9+
$this->model = $model;
10+
}
11+
12+
public static function new(string $model): self {
13+
return new self($model);
14+
}
15+
16+
public function getModel(): string {
17+
if (substr($this->model, 0,7) === 'custom_') {
18+
return substr($this->model, 7);
19+
}
20+
21+
return $this->model;
22+
}
23+
}

0 commit comments

Comments
 (0)