Skip to content

Commit

Permalink
Merge pull request #121 from merefield/add_support_for_reasoning_models
Browse files Browse the repository at this point in the history
FEATURE: support o1 reasoning models
  • Loading branch information
merefield authored Sep 24, 2024
2 parents da56bc3 + 8c79ca2 commit 6633cbf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
6 changes: 3 additions & 3 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a target='_blank' rel='noopener' href='https://platform.openai.com/account/api-keys/'>openai.com</a>"
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 <a target='_blank' rel='noopener' href='/admin/customize/site_texts?q=chatbot.prompt.system.'>Customize Text</a>"
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 <a target='_blank' rel='noopener' href='/admin/customize/site_texts?q=chatbot.prompt.system.'>Customize Text</a>"
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 <a target='_blank' rel='noopener' href='/admin/customize/site_texts?q=chatbot.prompt.system.'>Customize Text</a>"
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 <a target='_blank' rel='noopener' href='/admin/customize/site_texts?q=chatbot.prompt.system.'>Customize Text</a>"
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 <a target='_blank' rel='noopener' href='/admin/customize/site_texts?q=chatbot.prompt.system.'>Customize Text</a>"
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 <a target='_blank' rel='noopener' href='/admin/customize/site_texts?q=chatbot.prompt.system.'>Customize Text</a>"
chatbot_open_ai_model_low_trust: "(LOW TRUST USERS, UNLESS CUSTOM) The model to be accessed. More on supported models at <a target='_blank' rel='noopener' href='https://platform.openai.com/docs/models/overview'>OpenAI: Model overview</a>"
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"
Expand Down
12 changes: 9 additions & 3 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 24 additions & 9 deletions lib/discourse_chatbot/bots/open_ai_bot_basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class OpenAiBotBasic < OpenAIBotBase

def get_response(prompt, opts)
begin
reasoning_model = false
private_discussion = opts[:private] || false

if private_discussion
Expand All @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion lib/discourse_chatbot/bots/open_ai_bot_rag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 6633cbf

Please sign in to comment.