Changeset 3292957
- Timestamp:
- 05/14/2025 05:48:09 AM (11 months ago)
- Location:
- ai-translate-for-polylang
- Files:
-
- 8 added
- 2 edited
-
tags/1.1.1 (added)
-
tags/1.1.1/ai-translate-polylang.php (added)
-
tags/1.1.1/inc (added)
-
tags/1.1.1/inc/open-ai (added)
-
tags/1.1.1/inc/open-ai/OpenAi.php (added)
-
tags/1.1.1/inc/open-ai/Url.php (added)
-
tags/1.1.1/inc/settingslib.php (added)
-
tags/1.1.1/readme.txt (added)
-
trunk/ai-translate-polylang.php (modified) (9 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ai-translate-for-polylang/trunk/ai-translate-polylang.php
r3279533 r3292957 4 4 Plugin URI: https://wordpress.org/plugins/ai-translate-polylang/ 5 5 Description: Add auto AI translation caperbility to Polylang 6 Version: 1.1. 06 Version: 1.1.1 7 7 Author: James Low 8 8 Author URI: http://jameslow.com … … 23 23 public static $OPENAI_MODEL = 'gpt-4o'; 24 24 public static $CLAUDE_MODEL = 'claude-3-5-sonnet-20240620'; 25 public static $GEMINI_MODEL = 'gemini-2.0-flash'; 25 26 26 27 //Variables … … 63 64 array('id'=>'ai_translate_llm', 'type'=>'select', 'title'=>'LLM Service', 'default'=>'OpenAI', 'values'=>array( 64 65 'OpenAI', 65 'Claude' 66 'Claude', 67 'Gemini' 66 68 )), 67 69 array('id'=>'ai_translate_openai', 'type'=>'title', 'title'=>'OpenAI', 'description'=>''), … … 83 85 'claude-3-haiku-20240307' 84 86 )), 87 array('id'=>'ai_translate_gemini', 'type'=>'title', 'title'=>'Gemini', 'description'=>''), 88 array('id'=>'ai_translate_gemini_key', 'type'=>'string', 'title'=>'Gemini API Key', 'description'=>''), 89 array('id'=>'ai_translate_gemini_model', 'type'=>'select', 'title'=>'Gemini Model', 'default'=>self::$GEMINI_MODEL, 'values'=>array( 90 'gemini-2.0-flash', 91 'gemini-1.5-flash', 92 'gemini-1.5-pro' 93 )), 85 94 array('id'=>'ai_translate_meta', 'type'=>'title', 'title'=>'Meta', 'description'=>''), 86 95 array('id'=>'ai_translate_meta_clear', 'type'=>'text', 'title'=>'Meta keys to clear', 'description'=>'', 'default'=>''), … … 156 165 if ($text && trim($text) != '') { 157 166 $prompt = self::prompt($to, $from); 158 if (get_option('ai_translate_llm', 'OpenAI') == 'Claude') { 159 $result = self::claude_message($text, $prompt); 167 $service = get_option('ai_translate_llm', 'OpenAI'); 168 if ($service == 'Claude') { 169 $result = self::claude_api($text, $prompt); 160 170 $body = json_decode($result['body'], true); 161 171 if ($result['response']['code'] == 200) { 162 172 return $body['content'][0]['text']; 173 } else { 174 return 'ERROR: '.$body['error']['message']; 175 } 176 } elseif ($service == 'Gemini') { 177 $result = self::gemini_api($text, $prompt); 178 $body = json_decode($result['body'], true); 179 if ($result['response']['code'] == 200) { 180 return $body['candidates'][0]['content']['parts'][0]['text']; 163 181 } else { 164 182 return 'ERROR: '.$body['error']['message']; … … 263 281 264 282 /* APIs */ 265 public static function claude_ message($content, $role = 'You are a helpful assistant.', $tokens = 1000, $temp = 0) {283 public static function claude_api($content, $role = 'You are a helpful assistant.', $tokens = 5000, $temp = 0) { 266 284 $request = new \WP_Http(); 267 285 $headers = array( … … 272 290 $message = array( 273 291 'model' => get_option('ai_translate_claude_model', self::$CLAUDE_MODEL), 274 'max_tokens' => 1000,292 'max_tokens' => $tokens, 275 293 'temperature' => $temp, 276 294 'system' => $role, … … 298 316 return $openai; 299 317 } 300 public static function openai_api($content, $role = 'You are a helpful assistant.', $tokens = 1000) {318 public static function openai_api($content, $role = 'You are a helpful assistant.', $tokens = 5000) { 301 319 //https://packagist.org/packages/orhanerday/open-ai 302 320 return json_decode(self::openai()->chat([ … … 318 336 ]), true); 319 337 } 338 public static function gemini_api($content, $role = 'You are a helpful assistant.') { 339 //https://ai.google.dev/gemini-api/docs/text-generation#rest_2 340 $request = new \WP_Http(); 341 $headers = array( 342 'Content-Type' => 'application/json', 343 ); 344 $message = array( 345 'system_instruction' => array( 346 'parts' => array( 347 array('text' => $role) 348 ) 349 ), 350 'contents' => array( 351 'parts' => array( 352 array('text' => $content) 353 ) 354 ) 355 ); 356 $args = array( 357 'method' => 'POST', 358 'headers' => $headers, 359 'body' => json_encode($message), 360 'timeout' => 60, 361 ); 362 return $request->request('https://generativelanguage.googleapis.com/v1beta/models/'.get_option('ai_translate_gemini_model', self::$GEMINI_MODEL).':generateContent?key='.get_option('ai_translate_gemini_key', self::$GEMINI_MODEL), $args); 363 } 320 364 } 321 365 -
ai-translate-for-polylang/trunk/readme.txt
r3279533 r3292957 5 5 Requires at least: 3.0 6 6 Tested up to: 6.6.1 7 Stable tag: 1.1. 07 Stable tag: 1.1.1 8 8 License: MIT 9 9 License URI: https://opensource.org/licenses/MIT … … 34 34 == Changelog == 35 35 36 = 1.1.1 = 37 * Add Google Gemini support 38 36 39 = 1.1.0 = 37 * Set post language on new post before trying to tran lsate40 * Set post language on new post before trying to translate 38 41 39 42 = 1.0.9 =
Note: See TracChangeset
for help on using the changeset viewer.