Skip to content

Commit f1d46de

Browse files
authored
Merge pull request #32 from fokx/main
support custom Open AI model URL, not limited to Azure
2 parents f8a95b0 + 681f190 commit f1d46de

File tree

5 files changed

+34
-33
lines changed

5 files changed

+34
-33
lines changed

config/locales/server.en.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
en:
22
site_settings:
33
chatbot_enabled: "Enable the chatbot plugin"
4-
chatbot_open_ai_token: "Your Open AI token. You can get one at <a target='_blank' rel='noopener' href='https://platform.openai.com/account/api-keys/'>openai.com</a>"
5-
chatbot_azure_open_ai_model_url: "Populate it if you want to use Azure OpenAI, e.g. https://custom-domain.openai.azure.com/openai/deployments/custom-name)."
6-
chatbot_azure_open_ai_token: "Your Azure Open AI token. Required if you supply Azure URL. You can get one at <a target='_blank' rel='noopener' href='https://portal.azure.com/'>Azure Portal</a>."
4+
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>"
75
chatbot_bot_type: "EXPERIMENTAL: To make the bot smarter, use 'agent' (but be aware this uses many more calls to the LLM increasing cost significantly!)"
86
chatbot_open_ai_model_custom: "Use Custom model name (ADVANCED USERS ONLY)"
97
chatbot_open_ai_model_custom_name: "(CUSTOM ONLY) Name of model"
108
chatbot_open_ai_model_custom_type: "(CUSTOM ONLY) IMPORTANT: Select which type of Open AI endpoint should be used, Chat (ChatGPT style), or Completion (Davinci style) - will break if you select wrong type"
9+
chatbot_open_ai_model_custom_url: "Populate it if you want to use proxy to Open AI or Azure OpenAI, e.g. https://custom-domain.openai.azure.com/openai/deployments/custom-name)."
10+
chatbot_open_ai_model_custom_api_type: "Fill in 'azure' if you use Azure OpenAI, otherwise will use Open AI"
11+
chatbot_open_ai_model_custom_api_version: "Fill in Azure OpenAI REST API version, default to '2023-05-15'. Required only when 'custom_api_type' is set to 'azure'. Refer to: <a target='_blank' rel='noopener' href='https://learn.microsoft.com/en-us/azure/ai-services/openai/reference'>Azure REST API reference</a>"
1112
chatbot_open_ai_model: "(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>"
1213
chatbot_reply_job_time_delay: 'Number of seconds before reply job is run. This helps prevent rate limits being hit and discourages spamming.'
1314
chatbot_include_whispers_in_post_history: "Include content of whispers in Post history bot sees (careful!)"

config/settings.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,21 @@ plugins:
8585
choices:
8686
- normal
8787
- agent
88-
chatbot_azure_open_ai_token:
88+
chatbot_open_ai_model_custom:
89+
default: false
8990
client: false
91+
chatbot_open_ai_model_custom_name:
9092
default: ''
91-
chatbot_azure_open_ai_model_url:
93+
client: false
94+
chatbot_open_ai_model_custom_url:
9295
client: false
9396
default: ''
94-
chatbot_open_ai_model_custom:
95-
default: false
97+
chatbot_open_ai_model_custom_api_type:
9698
client: false
97-
chatbot_open_ai_model_custom_name:
9899
default: ''
100+
chatbot_open_ai_model_custom_api_version:
99101
client: false
102+
default: '2023-05-15'
100103
chatbot_request_temperature:
101104
client: false
102105
default: 100

lib/discourse_chatbot/bot.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,24 @@ module ::DiscourseChatbot
55
class Bot
66

77
def initialize
8-
raise "Overwrite me!"
8+
if SiteSetting.chatbot_open_ai_model_custom_api_type == "azure"
9+
::OpenAI.configure do |config|
10+
config.access_token = SiteSetting.chatbot_open_ai_token
11+
config.uri_base = SiteSetting.chatbot_open_ai_model_custom_url
12+
config.api_type = :azure
13+
config.api_version = SiteSetting.chatbot_open_ai_model_custom_api_version
14+
end
15+
else
16+
if !SiteSetting.chatbot_open_ai_model_custom_url.blank?
17+
::OpenAI.configure do |config|
18+
config.access_token = SiteSetting.chatbot_open_ai_token
19+
config.uri_base = SiteSetting.chatbot_open_ai_model_custom_url
20+
end
21+
@client = ::OpenAI::Client.new
22+
else
23+
@client = ::OpenAI::Client.new(access_token: SiteSetting.chatbot_open_ai_token)
24+
end
25+
end
926
end
1027

1128
def get_response(prompt)

lib/discourse_chatbot/bots/open_ai_agent.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@ module ::DiscourseChatbot
66
class OpenAIAgent < Bot
77

88
def initialize
9-
if SiteSetting.chatbot_azure_open_ai_model_url.include?("azure")
10-
::OpenAI.configure do |config|
11-
config.access_token = SiteSetting.chatbot_azure_open_ai_token
12-
config.uri_base = SiteSetting.chatbot_azure_open_ai_model_url
13-
config.api_type = :azure
14-
config.api_version = "2023-05-15"
15-
end
16-
@client = ::OpenAI::Client.new
17-
else
18-
@client = ::OpenAI::Client.new(access_token: SiteSetting.chatbot_open_ai_token)
19-
end
9+
super
2010

2111
@model_name = SiteSetting.chatbot_open_ai_model_custom ? SiteSetting.chatbot_open_ai_model_custom_name : SiteSetting.chatbot_open_ai_model
2212

@@ -131,8 +121,8 @@ def call_function(func_name, args_str)
131121
func = @func_mapping[func_name]
132122
res = func.process(args)
133123
res
134-
rescue
135-
"There was something wrong with your function arguments"
124+
rescue
125+
"There was something wrong with your function arguments"
136126
end
137127
end
138128

lib/discourse_chatbot/bots/open_ai_bot.rb

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,7 @@ module ::DiscourseChatbot
66
class OpenAIBot < Bot
77

88
def initialize
9-
if SiteSetting.chatbot_azure_open_ai_model_url.include?("azure")
10-
::OpenAI.configure do |config|
11-
config.access_token = SiteSetting.chatbot_azure_open_ai_token
12-
config.uri_base = SiteSetting.chatbot_azure_open_ai_model_url
13-
config.api_type = :azure
14-
config.api_version = "2023-05-15"
15-
end
16-
@client = ::OpenAI::Client.new
17-
else
18-
@client = ::OpenAI::Client.new(access_token: SiteSetting.chatbot_open_ai_token)
19-
end
9+
super
2010
end
2111

2212
def get_response(prompt)

0 commit comments

Comments
 (0)