From 8c79ca2d47423057141105bb23dd2881f266cb8e Mon Sep 17 00:00:00 2001 From: merefield Date: Tue, 24 Sep 2024 09:19:24 +0100 Subject: [PATCH] support o1 reasoning models (basic mode only) --- config/locales/server.en.yml | 6 ++-- config/settings.yml | 12 +++++-- .../bots/open_ai_bot_basic.rb | 33 ++++++++++++++----- lib/discourse_chatbot/bots/open_ai_bot_rag.rb | 2 +- plugin.rb | 4 ++- 5 files changed, 40 insertions(+), 17 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 56a3143..75db617 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2,9 +2,9 @@ en: site_settings: chatbot_enabled: "Enable the chatbot plugin" chatbot_open_ai_token: "Your Open AI token. Will also be used as token if you set a custom URL to service other than Open AI. You can get one at openai.com" - chatbot_bot_type_low_trust: "(LOW TRUST USERS) To make the bot smarter, use 'RAG', which will enhance it will local deterministic functions (but be aware this uses many more calls to the LLM increasing cost significantly!). Maintain associated prompts at Customize Text" - chatbot_bot_type_medium_trust: "(MEDIUM TRUST USERS) To make the bot smarter, use 'RAG', which will enhance it will local deterministic functions (but be aware this uses many more calls to the LLM increasing cost significantly!). Maintain associated prompts at Customize Text" - chatbot_bot_type_high_trust: "(HIGH TRUST USERS) To make the bot smarter, use 'RAG', which will enhance it will local deterministic functions (but be aware this uses many more calls to the LLM increasing cost significantly!). Maintain associated prompts at Customize Text" + chatbot_bot_type_low_trust: "(LOW TRUST USERS) New Reasoning models (o1 series) currently require 'basic'. 'RAG' will give bot local deterministic functions (but be aware this uses more calls to the LLM increasing cost significantly!). Maintain associated prompts at Customize Text" + chatbot_bot_type_medium_trust: "(MEDIUM TRUST USERS) New Reasoning models (o1 series) currently require 'basic'. 'RAG' will give bot local deterministic functions (but be aware this uses more calls to the LLM increasing cost significantly!). Maintain associated prompts at Customize Text" + chatbot_bot_type_high_trust: "(HIGH TRUST USERS) New Reasoning models (o1 series) currently require 'basic'. 'RAG' will give bot local deterministic functions (but be aware this uses more calls to the LLM increasing cost significantly!). Maintain associated prompts at Customize Text" chatbot_open_ai_model_low_trust: "(LOW TRUST USERS, UNLESS CUSTOM) The model to be accessed. More on supported models at OpenAI: Model overview" chatbot_open_ai_model_custom_low_trust: "(LOW TRUST USERS) Use Custom model name (ADVANCED USERS ONLY)" chatbot_open_ai_model_custom_name_low_trust: "(LOW TRUST USERS, CUSTOM ONLY) Name of model" diff --git a/config/settings.yml b/config/settings.yml index 431949a..7c53e32 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -2,6 +2,9 @@ plugins: chatbot_enabled: default: true client: true + chatbot_open_ai_token: + client: false + default: '' chatbot_permitted_in_private_messages: default: true client: true @@ -44,6 +47,8 @@ plugins: - gpt-4-turbo - gpt-4 - gpt-4-32k + - o1-preview + - o1-mini chatbot_open_ai_model_custom_high_trust: default: false client: false @@ -71,6 +76,8 @@ plugins: - gpt-4-turbo - gpt-4 - gpt-4-32k + - o1-preview + - o1-mini chatbot_open_ai_model_custom_medium_trust: default: false client: false @@ -98,6 +105,8 @@ plugins: - gpt-4-turbo - gpt-4 - gpt-4-32k + - o1-preview + - o1-mini chatbot_open_ai_model_custom_low_trust: default: false client: false @@ -244,9 +253,6 @@ plugins: default: 2 min: 0 max: 5 - chatbot_open_ai_token: - client: false - default: '' chatbot_request_temperature: client: false default: 100 diff --git a/lib/discourse_chatbot/bots/open_ai_bot_basic.rb b/lib/discourse_chatbot/bots/open_ai_bot_basic.rb index f15c845..407f02a 100644 --- a/lib/discourse_chatbot/bots/open_ai_bot_basic.rb +++ b/lib/discourse_chatbot/bots/open_ai_bot_basic.rb @@ -7,6 +7,7 @@ class OpenAiBotBasic < OpenAIBotBase def get_response(prompt, opts) begin + reasoning_model = false private_discussion = opts[:private] || false if private_discussion @@ -15,18 +16,32 @@ def get_response(prompt, opts) system_message = { "role": "system", "content": I18n.t("chatbot.prompt.system.basic.open", current_date_time: DateTime.current) } end - prompt.unshift(system_message) + reasoning_model = true if REASONING_MODELS.include?(@model_name) - response = @client.chat( - parameters: { + if !reasoning_model + prompt.unshift(system_message) + end + + parameters = { model: @model_name, messages: prompt, - max_tokens: SiteSetting.chatbot_max_response_tokens, - temperature: SiteSetting.chatbot_request_temperature / 100.0, - top_p: SiteSetting.chatbot_request_top_p / 100.0, - frequency_penalty: SiteSetting.chatbot_request_frequency_penalty / 100.0, - presence_penalty: SiteSetting.chatbot_request_presence_penalty / 100.0 - }) + max_completion_tokens: SiteSetting.chatbot_max_response_tokens, + } + + additional_parameters = { + temperature: SiteSetting.chatbot_request_temperature / 100.0, + top_p: SiteSetting.chatbot_request_top_p / 100.0, + frequency_penalty: SiteSetting.chatbot_request_frequency_penalty / 100.0, + presence_penalty: SiteSetting.chatbot_request_presence_penalty / 100.0 + } + + if !reasoning_model + parameters.merge!(additional_parameters) + end + + response = @client.chat( + parameters: parameters + ) { reply: response.dig("choices", 0, "message", "content"), diff --git a/lib/discourse_chatbot/bots/open_ai_bot_rag.rb b/lib/discourse_chatbot/bots/open_ai_bot_rag.rb index f6fcab0..ab087ef 100644 --- a/lib/discourse_chatbot/bots/open_ai_bot_rag.rb +++ b/lib/discourse_chatbot/bots/open_ai_bot_rag.rb @@ -147,7 +147,7 @@ def create_chat_completion(messages, use_functions = true, iteration) parameters = { model: @model_name, messages: messages, - max_tokens: SiteSetting.chatbot_max_response_tokens, + max_completion_tokens: SiteSetting.chatbot_max_response_tokens, temperature: SiteSetting.chatbot_request_temperature / 100.0, top_p: SiteSetting.chatbot_request_top_p / 100.0, frequency_penalty: SiteSetting.chatbot_request_frequency_penalty / 100.0, diff --git a/plugin.rb b/plugin.rb index 5f744bd..f9cd33e 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-chatbot # about: a plugin that allows you to have a conversation with a configurable chatbot in Discourse Chat, Topics and Private Messages -# version: 1.0.6 +# version: 1.1.0 # authors: merefield # url: https://github.com/merefield/discourse-chatbot @@ -38,6 +38,8 @@ module ::DiscourseChatbot POST_URL_REGEX = %r{\/t/[^/]+/(\d+)/(\d+)(?!\d|\/)} NON_POST_URL_REGEX = %r{\bhttps?:\/\/[^\s\/$.?#].[^\s)]*} + REASONING_MODELS = ["o1-preview", "o1-mini"] + def progress_debug_message(message) puts "Chatbot: #{message}" if SiteSetting.chatbot_enable_verbose_console_logging if SiteSetting.chatbot_enable_verbose_rails_logging == "all"