elevenlabs_field-1.0.0-beta7/modules/ai_interpolator_elevenlabs_field/src/Plugin/AiInterpolatorFieldRules/ElevenLabsDialogue.php
modules/ai_interpolator_elevenlabs_field/src/Plugin/AiInterpolatorFieldRules/ElevenLabsDialogue.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | <?php namespace Drupal\ai_interpolator_elevenlabs_field\Plugin\AiInterPolatorFieldRules; use Drupal\ai_interpolator\PluginInterfaces\AiInterpolatorFieldRuleInterface; use Drupal\ai_interpolator_openai\OpenAiBase; use Drupal\ai_interpolator_openai\OpenAiRequester; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\elevenlabs_field\ElevenLabsApiService; use Drupal\elevenlabs_field\ElevenLabsGenerateService; use Symfony\Component\DependencyInjection\ContainerInterface; /** * The rules for a ElevenLabs Field. * * @AiInterpolatorFieldRule( * id = "ai_interpolator_elevenlabs_dialogue", * title = @Translation("ElevenLabs Dialogue Creator"), * field_rule = "elevenlabs", * target = "file", * ) */ class ElevenLabsDialogue extends OpenAiBase implements AiInterpolatorFieldRuleInterface, ContainerFactoryPluginInterface { /** * {@inheritDoc} */ public $title = 'ElevenLabs Dialogue Creator' ; /** * The elevenLabs requester. */ public ElevenLabsApiService $elevenLabs ; /** * The Open AI requester. */ public OpenAiRequester $openAi ; /** * The elevenlabs generator. */ public ElevenLabsGenerateService $generator ; /** * Construct an image field. * * @param array $configuration * Inherited configuration. * @param string $plugin_id * Inherited plugin id. * @param mixed $plugin_definition * Inherited plugin definition. * @param \Drupal\elevenlabs_field\ElevenLabsApiService $elevenLabs * The elevenlabs requester. * @param \Drupal\ai_interpolator_openai\OpenAiRequester $openAi * The OpenAI requester. * @param \Drupal\elevenlabs_field\ElevenLabsGenerateService $generator * The elevenlabs generator. */ public function __construct( array $configuration , $plugin_id , $plugin_definition , ElevenLabsApiService $elevenLabs , OpenAiRequester $openAi , ElevenLabsGenerateService $generator ) { parent::__construct( $configuration , $plugin_id , $plugin_definition , $openAi ); $this ->elevenLabs = $elevenLabs ; $this ->openAi = $openAi ; $this ->generator = $generator ; } /** * {@inheritDoc} */ public static function create(ContainerInterface $container , array $configuration , $plugin_id , $plugin_definition ) { return new static ( $configuration , $plugin_id , $plugin_definition , $container ->get( 'elevenlabs_field.api_service' ), $container ->get( 'ai_interpolator_openai.request' ), $container ->get( 'elevenlabs_field.generator_service' ) ); } /** * {@inheritDoc} */ public function needsPrompt() { return TRUE; } /** * {@inheritDoc} */ public function advancedMode() { return TRUE; } /** * {@inheritDoc} */ public function placeholderText() { return "Based on the context text create a dialogue between podcast interviewer Richard Ayoade who is a fun loving character and Julia Stipes who is a serious guest expert on the topic and answering the questions. Start with an introduction and created 10 dialogues back and forth and then end with a thank you to the guest. The elevenlabs voices to use are: Richard Ayoade = {{ clyde }} Julia Stipes = {{ rachel }} Context: {{ context }}"; } /** * {@inheritDoc} */ public function tokens() { $voices = $this ->elevenLabs->getVoices(); $tokens = [ 'context' => 'The cleaned text from the base field.' , 'raw_context' => 'The raw text from the base field. Can include HTML' , 'max_amount' => 'The max amount of entries to set. If unlimited this value will be empty.' , ]; foreach ( $voices [ 'voices' ] as $voices ) { $tokens [ strtolower ( $voices [ 'name' ])] = $this ->t( 'Voice of %name' , [ '%name' => $voices [ 'name' ]]); } return $tokens ; } /** * {@inheritDoc} */ public function extraAdvancedFormFields(ContentEntityInterface $entity , FieldDefinitionInterface $fieldDefinition ) { $form [ 'interpolator_openai_model' ] = [ '#type' => 'select' , '#title' => 'OpenAI Model' , '#description' => $this ->t( 'Choose the model you want to use here.' ), '#options' => $this ->openAi->getModels(), '#default_value' => $fieldDefinition ->getConfig( $entity ->bundle())->getThirdPartySetting( 'ai_interpolator' , 'interpolator_openai_model' , 'gpt-3.5-turbo-0613' ), '#weight' => 24, ]; $form [ 'interpolator_openai_role' ] = [ '#type' => 'textarea' , '#title' => 'OpenAI Role' , '#description' => $this ->t( 'If the AI should have some specific role, write it here.' ), '#attributes' => [ 'placeholder' => $this ->t( 'You are an OSK assistant helping with coming up with company party ideas.' ), ], '#default_value' => $fieldDefinition ->getConfig( $entity ->bundle())->getThirdPartySetting( 'ai_interpolator' , 'interpolator_openai_role' , '' ), '#weight' => 25, ]; return $form ; } /** * {@inheritDoc} */ public function generateTokens(ContentEntityInterface $entity , FieldDefinitionInterface $fieldDefinition , array $interpolatorConfig , $delta = 0) { $tokens = parent::generateTokens( $entity , $fieldDefinition , $interpolatorConfig ); $voices = $this ->elevenLabs->getVoices(); foreach ( $voices [ 'voices' ] as $voices ) { $tokens [ strtolower ( $voices [ 'name' ])] = $voices [ 'voice_id' ]; } return $tokens ; } /** * {@inheritDoc} */ public function generate(ContentEntityInterface $entity , FieldDefinitionInterface $fieldDefinition , array $interpolatorConfig ) { $prompts = parent::generate( $entity , $fieldDefinition , $interpolatorConfig ); // Add to get functional output. $total = []; // Add to get functional output. foreach ( $prompts as $prompt ) { $prompt .= "\n\nDo not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation. [ {\"value\": {\"speaker\": \"The name of the speaker\",\"speaker_id\": \"The id of the speaker\",\"text\": \"The dialogue\"}}, {\"value\": {\"speaker\": \"The name of the speaker\",\"speaker_id\": \"The id of the speaker\",\"text\": \"The dialogue\"}} ] An example of a 3 dialogue output would be: [ {\"value\": {\"speaker\": \"Jonas Nilsson\",\"speaker_id\": \"ThT5KcBeYPX3keUQqHPh\",\"text\": \"Hello and welcome to the show Jenny!\"}}, {\"value\": {\"speaker\": \"Jenny Moore\",\"speaker_id\": \"flq6f7yk4E4fJM5XTYuZ\",\"text\": \"Thank you Jonas, it's nice to be here.\"}}, {\"value\": {\"speaker\": \"Jonas Nilsson\",\"speaker_id\": \"ThT5KcBeYPX3keUQqHPh\",\"text\": \"I would like to start with asking you about tomorrows weather?\"}} ]"; try { $values = $this ->openAi->generateResponse( $prompt , $fieldDefinition , $interpolatorConfig ); $total = array_merge_recursive ( $total , $values ); } catch (\Exception $e ) { } } return $total ; } /** * {@inheritDoc} */ public function verifyValue(ContentEntityInterface $entity , $value , FieldDefinitionInterface $fieldDefinition ) { // Validate. if ( empty ( $value [ 'speaker' ]) && empty ( $value [ 'speaker_id' ]) && empty ( $value [ 'text' ])) { return FALSE; } // Otherwise it is ok. return TRUE; } /** * {@inheritDoc} */ public function storeValues(ContentEntityInterface $entity , array $values , FieldDefinitionInterface $fieldDefinition ) { // Transform string to stripped. foreach ( $values as $key => $value ) { // We need to generate right away, since presave does not always invoke. $data = $this ->generator->generateFile( $value [ 'text' ], $value [ 'speaker_id' ], $fieldDefinition , 'eleven_multilingual_v2' , [ 'stability' => 1, 'similarity_boost' => 0.75, 'style_exaggeration' => 0, ]); if (! empty ( $data [ 'file' ])) { $values [ $key ] = [ 'target_id' => $data [ 'file' ]->id(), 'history_item_id' => $data [ 'history_item_id' ], 'text' => $value [ 'text' ], 'speaker' => $value [ 'speaker_id' ], 'start_time' => 0, 'similarity_boost' => 0.75, 'style_exaggeration' => 0, 'stability' => 1, ]; } } // Then set the value. $entity ->set( $fieldDefinition ->getName(), $values ); } } |