Skip to content

Instantly share code, notes, and snippets.

@bwente
Created March 30, 2023 12:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwente/a69c3987804e712b123cc8dd39cd61da to your computer and use it in GitHub Desktop.
Save bwente/a69c3987804e712b123cc8dd39cd61da to your computer and use it in GitHub Desktop.
SummaryAI Plugin for MODX (uses OpenAi API)
<?php
$model = $modx->getOption('model', $scriptProperties);
$prompt = $modx->getOption('prompt', $scriptProperties);
$api_url = $modx->getOption('api_url', $scriptProperties);
$api_key = $modx->getOption('api_key', $scriptProperties);
switch($modx->event->name) {
case 'OnDocFormSave':
// Check if the resource has a summary
if (!$resource->get('introtext')) {
// Get the content of the resource
$content = $resource->get('content');
// Strip HTML tags
$content = strip_tags($content);
// Grab first 1500 words
$words = explode(" ", $content);
$truncated = implode(" ", array_slice($words, 0, 1500));
$stripped = preg_replace('/\s+/', ' ', $truncated);
$contentQuery = $prompt . " " . $stripped;
// Set up the Chat-GPT API request
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
);
$data = array(
'model' => $model,
'prompt' => $contentQuery,
'max_tokens' => 200,
'temperature' => 0.5,
'n' => 1
);
// Send the Chat-GPT API request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Extract the summary from the Chat-GPT API response
$response_data = json_decode($response, true);
$summary = $response_data['choices'][0]['text'];
// Update the resource with the summary
$resource->set('introtext', trim($summary));
$resource->save();
$modx->log(MODX_LOG_LEVEL_INFO, print_r($response_data,true) );
break;
}
}
@sebastian-marinescu
Copy link

❤️

@faitno
Copy link

faitno commented Feb 21, 2024

import properties
[{"name":"api_key","desc":"","xtype":"textfield","options":[],"value":"sk-XXXXXXXXXXXXXXXXXXX","lexicon":"","overridden":false,"desc_trans":"","area":"","area_trans":"","menu":null},{"name":"api_url","desc":"","xtype":"textfield","options":[],"value":"https://api.openai.com/v1/completions","lexicon":"","overridden":false,"desc_trans":"","area":"","area_trans":"","menu":null},{"name":"model","desc":"OpenAI API models with different capabilities and price points.","xtype":"list","options":[{"text":"text-davinci-003","value":"text-davinci-003","name":"text-davinci-003"},{"text":"text-davinci-002","value":"text-davinci-002","name":"text-davinci-002"},{"text":" text-curie-001","value":" text-curie-001","name":" text-curie-001"},{"text":"text-babbage-001","value":"text-babbage-001","name":"text-babbage-001"},{"text":"text-ada-001","value":"text-ada-001","name":"text-ada-001"},{"text":"davinci","value":"davinci","name":"davinci"},{"text":"curie","value":"curie","name":"curie"},{"text":"babbage","value":"babbage","name":"babbage"},{"text":"ada","value":"ada","name":"ada"}],"value":"text-ada-001","lexicon":"","overridden":false,"desc_trans":"OpenAI API models with different capabilities and price points.","area":"","area_trans":"","menu":null},{"name":"prompt","desc":"Chat completion prompt","xtype":"textfield","options":[],"value":"Write a SEO optimized description without html formatting in 160 words or less for the following...","lexicon":"","overridden":false,"desc_trans":"Chat completion prompt","area":"","area_trans":"","menu":null}]

@bwente
Copy link
Author

bwente commented Feb 21, 2024

@faitno Thank you! I should have thought of that to include.

@Sentinel-7
Copy link

Hi.
I got my api key and created the plugin but I have nothing happening? Can you help me?

@bwente
Copy link
Author

bwente commented Apr 3, 2024

Hi. I got my api key and created the plugin but I have nothing happening? Can you help me?

Did you import the properties that @faitno added above? That should make more sense.

@Sentinel-7
Copy link

Hi. I got my api key and created the plugin but I have nothing happening? Can you help me?

Did you import the properties that @faitno added above? That should make more sense.

Yeah. Nothing's happening, unfortunately.

@bwente
Copy link
Author

bwente commented Apr 3, 2024

Here is the original post.
https://community.modx.com/t/openai-chatgpt-plugin-to-create-introtext-from-content/6588

How are you testing? Or what are you expecting? If the introtext of a resource is empty, it will use the content field and chat-gpt to write a summary. If there is text in the field, it will not update when you save the resource.

@Sentinel-7
Copy link

Here is the original post. https://community.modx.com/t/openai-chatgpt-plugin-to-create-introtext-from-content/6588

How are you testing? Or what are you expecting? If the introtext of a resource is empty, it will use the content field and chat-gpt to write a summary. If there is text in the field, it will not update when you save the resource.

I've done it all. It doesn't work. No errors 🤷‍♂️

@bwente
Copy link
Author

bwente commented Apr 3, 2024

I am out of credits. But it should work, does it the api call work using curl manually?

image

@Sentinel-7
Copy link

I am out of credits. But it should work, does it the api call work using curl manually?

I don't know. But my grant expired, too. Maybe because of that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment