From 7e656bbd013cd30e3e170939dacfc08ea4185da3 Mon Sep 17 00:00:00 2001 From: Hardeep Asrani Date: Thu, 26 Sep 2024 21:31:01 +0530 Subject: [PATCH] feat: allow users to change GPT model --- inc/API.php | 3 +++ inc/Main.php | 1 + inc/OpenAI.php | 11 ++++++++++- src/backend/parts/settings/Assistant.js | 23 ++++++++++++++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/inc/API.php b/inc/API.php index 33c0fa3..4a96a8c 100644 --- a/inc/API.php +++ b/inc/API.php @@ -244,6 +244,9 @@ public function update_settings( $request ) { 'default_message' => function ( $value ) { return is_string( $value ); }, + 'chat_model' => function ( $value ) { + return is_string( $value ); + }, 'temperature' => function ( $value ) { return is_numeric( $value ); }, diff --git a/inc/Main.php b/inc/Main.php index 7507795..df8391e 100644 --- a/inc/Main.php +++ b/inc/Main.php @@ -172,6 +172,7 @@ public static function get_default_settings() { 'chat_enabled' => true, 'welcome_message' => __( 'Hello! How can I help you today?', 'hyve-lite' ), 'default_message' => __( 'Sorry, I\'m not able to help with that.', 'hyve-lite' ), + 'chat_model' => 'gpt-4o-mini', 'temperature' => 1, 'top_p' => 1, 'moderation_threshold' => array( diff --git a/inc/OpenAI.php b/inc/OpenAI.php index 8c03e19..6814d2e 100644 --- a/inc/OpenAI.php +++ b/inc/OpenAI.php @@ -27,6 +27,13 @@ class OpenAI { */ private $prompt_version = '1.1.0'; + /** + * Chat Model. + * + * @var string + */ + private $chat_model = 'gpt-4o-mini'; + /** * API Key. * @@ -50,6 +57,7 @@ public function __construct( $api_key = '' ) { $settings = Main::get_settings(); $this->api_key = ! empty( $api_key ) ? $api_key : ( isset( $settings['api_key'] ) ? $settings['api_key'] : '' ); $this->assistant_id = isset( $settings['assistant_id'] ) ? $settings['assistant_id'] : ''; + $this->chat_model = isset( $settings['chat_model'] ) ? $settings['chat_model'] : $this->chat_model; if ( $this->assistant_id && version_compare( $this->prompt_version, get_option( 'hyve_prompt_version', '1.0.0' ), '>' ) ) { $this->update_assistant(); @@ -86,7 +94,7 @@ public function create_assistant() { array( 'instructions' => "Assistant Role & Concise Response Guidelines: As a Support Assistant, provide precise, to-the-point answers based exclusively on the previously provided context.\r\n\r\nSET OF PRINCIPLES TO FOLLOW:\r\n\r\n1. **Identify the Context and Question**:\r\n1.1. **START CONTEXT**: Identify the context provided in the message. **: END CONTEXT**\r\n1.2. **START QUESTION**: Identify the question that needs to be answered based on the context.. **: END QUESTION**\r\n\r\n2. **Check the Context for Relevance**:\r\n2.1. Determine if the context contains information directly relevant to the question.\r\n2.2. If the context addresses the user's question, proceed to the next step.\r\n2.3. If the question is a greeting, respond appropriately with the greeting.\r\n2.4. If the context does not address the user's question, respond with: `{\"response\": \"\", \"success\": false}`.\r\n\r\n3. **Formulate the Response**:\r\n3.1. If the context is sufficient, formulate a clear and concise response using only the information provided in the context.\r\n3.2. Ensure the response includes all important details covered in the context, but avoid any extraneous information.\r\n\r\n4. **Avoid Referring to the Context**:\r\n4.1. Do not refer to the context or state that the response is based on the context in your answer.\r\n4.2. Ensure the response is straightforward and directly answers the question.\r\n\r\n5. **Generate the JSON Response**:\r\n5.1. Structure the response according to the following JSON schema:\r\n\r\n\r\n{\r\n \"\$schema\": \"http:\/\/json-schema.org\/draft-07\/schema#\",\r\n \"type\": \"object\",\r\n \"properties\": {\r\n \"response\": {\r\n \"type\": \"string\",\r\n \"description\": \"Contains the response to the question. Do not include it if the answer wasn't available in the context.\"\r\n },\r\n \"success\": {\r\n \"type\": \"boolean\",\r\n \"description\": \"Indicates whether the question was successfully answered from provided context.\"\r\n }\r\n },\r\n \"required\": [\"success\"]\r\n}\r\n\r\nExample Usage:\r\n\r\nContext: [Provide context here]\r\nQuestion: [Provide question here]\r\n\r\nExpected Behavior:\r\n\r\n- If the question is fully covered by the context, provide a response using the provided JSON schema.\r\n- If the question is not fully covered by the context, respond with: {\"response\": \"\", \"success\": false}.\r\n\r\nExample Responses:\r\n\r\n- Context covers the question: {\"response\": \"Here is the information you requested.\", \"success\": true}\r\n- Context does not cover the question: {\"response\": \"\", \"success\": false}\r\n- Context does not cover the question but is a greeting: {\"response\": \"Hello, what can I help you with?.\", \"success\": true}", 'name' => 'Chatbot by Hyve', - 'model' => 'gpt-3.5-turbo-0125', + 'model' => $this->chat_model, ) ); @@ -273,6 +281,7 @@ public function create_run( $messages, $thread ) { array( 'assistant_id' => $this->assistant_id, 'additional_messages' => $messages, + 'model' => $this->chat_model, 'temperature' => $settings['temperature'], 'top_p' => $settings['top_p'], 'response_format' => array( diff --git a/src/backend/parts/settings/Assistant.js b/src/backend/parts/settings/Assistant.js index 53954aa..37e6fce 100644 --- a/src/backend/parts/settings/Assistant.js +++ b/src/backend/parts/settings/Assistant.js @@ -9,7 +9,8 @@ import { Button, Panel, PanelRow, - RangeControl + RangeControl, + SelectControl } from '@wordpress/components'; import { @@ -70,6 +71,26 @@ const Assistant = () => { + + setSetting( 'chat_model', newValue ) } + /> + +