Skip to content

Commit

Permalink
feat: activate options for OpenAI provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Lötscher committed Jul 9, 2024
1 parent 0090246 commit acfa19f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 11 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ Currently, there are three prompt types (Text creation, optimization and correct
- [DataObject Type](docs/02-data-objects.md)
- [UIkit AiHelper](docs/03-uikit-ai-helper.md)
- [Symfony Form Type](docs/04-form-type.md)
- [Add additional providers](docs/05-additional-providers.md)
- [Admin user permission](docs/06-user-permission.md)
- [Add Additional Providers](docs/05-additional-providers.md)
- [Admin Uuser Permission](docs/06-user-permission.md)
- [OpenAi Provider Options](docs/07-open-ai-options.md)

## Possible planned features

Expand Down
17 changes: 17 additions & 0 deletions docs/07-open-ai-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# OpenAI Provider Options

![OpenAI Provider Options](images/open-ai-options.png)

### Currently implemented options

- **role**: Supported values are: 'system', 'assistant', 'user', 'function', and 'tool'.
- **model**: See [here](https://platform.openai.com/docs/models) for more information

### Syntax

Add your options semicolon separated to the configuration.
> option1: value; option2: other value;
Example:
> role: user; model: gpt-3.5-turbo;
Binary file added docs/images/open-ai-options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions src/PimcoreAiToolsBundle/Controller/PromptController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public function textAction(Request $request): JsonResponse
$configuration = $this->getConfiguration($type, $promptType, $request);

$prompt = $configuration['prompt'] . $text;
// ToDo: Get options from configuration and set it as third parameter (needs to be an array)
$result = $this->promptService->getText($configuration['provider'], $prompt);
$result = $this->promptService->getText($configuration['provider'], $prompt, $configuration['options']);

return $this->json([
'result' => $result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ pimcore.bundle.pimcore_ai_tools.settings = Class.create({

activate: function () {
var tabPanel = Ext.getCmp("pimcore_panel_tabs");
tabPanel.setActiveItem("pimcore_pimcore_ai_tools");
tabPanel.setActiveItem("bundle_pimcore_ai_tools");
},

getTabPanel: function () {

if (!this.panel) {
this.panel = new Ext.Panel({
id: "pimcore_pimcore_ai_tools",
id: "bundle_pimcore_ai_tools",
iconCls: "pimcore_ai_tools_nav_icon",
title: t("pimcore_ai_tools_configuration"),
border: false,
Expand All @@ -50,7 +50,7 @@ pimcore.bundle.pimcore_ai_tools.settings = Class.create({

var tabPanel = Ext.getCmp("pimcore_panel_tabs");
tabPanel.add(this.panel);
tabPanel.setActiveItem("pimcore_pimcore_ai_tools");
tabPanel.setActiveItem("bundle_pimcore_ai_tools");

this.panel.on("destroy", function () {
pimcore.globalmanager.remove("bundle_pimcore_ai_tools");
Expand Down
45 changes: 42 additions & 3 deletions src/PimcoreAiToolsBundle/Services/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,25 @@ public function getObjectConfiguration(string $className, string $fieldName, str
$key = 'object' . \implode('', \array_map('ucfirst', \explode('_', $type)));

if (empty($configuration['provider'])) {
// Get default provider if not set
$configuration['provider'] = $this->defaultConfiguration['textProvider'];
}

if (empty($configuration['prompt'])) {
// Get default prompt if not set
$configuration['prompt'] = $this->defaultConfiguration[$key];
}

// Get provider
$provider = $this->providerLocator->getProvider($configuration['provider']);

// Convert options from string to array
$optionsArray = $this->getOptionsArray($configuration['options']);

return [
'provider' => $provider,
'prompt' => $configuration['prompt'],
'options' => $configuration['options'],
'options' => $optionsArray,
];
}

Expand All @@ -73,20 +79,27 @@ public function getEditableConfiguration(string $areabrick, string $editable, st
$configuration = $list->getData()[0]->getData();
$key = 'editable' . \implode('', \array_map('ucfirst', \explode('_', $type)));


if (empty($configuration['provider'])) {
// Get default provider if not set
$configuration['provider'] = $this->defaultConfiguration['textProvider'];
}

if (empty($configuration['prompt'])) {
// Get default prompt if not set
$configuration['prompt'] = $this->defaultConfiguration[$key];
}

// Get provider
$provider = $this->providerLocator->getProvider($configuration['provider']);

// Convert options from string to array
$optionsArray = $this->getOptionsArray($configuration['options']);

return [
'provider' => $provider,
'prompt' => $configuration['prompt'],
'options' => $configuration['options'],
'options' => $optionsArray,
];
}

Expand All @@ -104,19 +117,45 @@ public function getFrontendConfiguration(string $name, string $type): array
$key = 'frontend' . \implode('', \array_map('ucfirst', \explode('_', $type)));

if (empty($configuration['provider'])) {
// Get default provider if not set
$configuration['provider'] = $this->defaultConfiguration['textProvider'];
}

if (empty($configuration['prompt'])) {
// Get default prompt if not set
$configuration['prompt'] = $this->defaultConfiguration[$key];
}

// Get provider
$provider = $this->providerLocator->getProvider($configuration['provider']);

// Convert options from string to array
$optionsArray = $this->getOptionsArray($configuration['options']);

return [
'provider' => $provider,
'prompt' => $configuration['prompt'],
'options' => $configuration['options'],
'options' => $optionsArray,
];
}

private function getOptionsArray(?string $optionsString = null): ?array
{
if (!$optionsString) {
return null;
}

$optionsArray = [];
$options = \array_map('trim', \explode(';', $optionsString));
foreach($options as $option) {
if (empty($option)) {
continue;
}

$parts = \array_map('trim', \explode(':', $option));
$optionsArray[$parts[0]] = $parts[1];
}

return $optionsArray;
}
}
2 changes: 1 addition & 1 deletion src/PimcoreAiToolsBundle/Services/PromptService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class PromptService
public function getText(
TextProviderInterface $textProvider,
string $prompt,
array $options = [],
?array $options = null,
): string
{
$options['prompt'] = $prompt;
Expand Down

0 comments on commit acfa19f

Please sign in to comment.