4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/slack/smart-bot/ai/open_ai/connect.rb', line 4
def self.connect(ai_open_ai, general_config, personal_settings, reconnect: false, service: :chat_gpt)
require "openai"
require "nice_http"
user = Thread.current[:user]
team_id_user = Thread.current[:team_id_user]
ai_open_ai = {} if ai_open_ai.nil?
ai_open_ai_user = {}
ai_open_ai_user = {
host: general_config._ai.open_ai.host,
access_token: general_config._ai.open_ai.access_token,
models: {
client: nil,
host: general_config._ai.open_ai.models.host,
access_token: general_config._ai.open_ai.models.access_token,
url: general_config._ai.open_ai.models.url,
api_type: general_config._ai.open_ai.models.api_type,
api_version: general_config._ai.open_ai.models.api_version,
},
chat_gpt: {
client: nil,
host: general_config._ai.open_ai.chat_gpt.host,
access_token: general_config._ai.open_ai.chat_gpt.access_token,
model: general_config._ai.open_ai.chat_gpt.model,
smartbot_model: general_config._ai.open_ai.chat_gpt.smartbot_model,
api_type: general_config._ai.open_ai.chat_gpt.api_type,
api_version: general_config._ai.open_ai.chat_gpt.api_version,
fixed_user: general_config._ai.open_ai.chat_gpt.fixed_user, },
dall_e: {
client: nil,
host: general_config._ai.open_ai.dall_e.host,
access_token: general_config._ai.open_ai.dall_e.access_token,
image_size: general_config._ai.open_ai.dall_e.image_size,
model: general_config._ai.open_ai.dall_e.model,
},
whisper: {
client: nil,
host: general_config._ai.open_ai.whisper.host,
access_token: general_config._ai.open_ai.whisper.access_token,
model: general_config._ai.open_ai.whisper.model,
},
}
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.host") and
personal_settings[team_id_user]["ai.open_ai.host"] != ""
ai_open_ai_user[:host] = personal_settings[team_id_user]["ai.open_ai.host"]
ai_open_ai_user[:chat_gpt][:host] = ai_open_ai_user[:host]
ai_open_ai_user[:dall_e][:host] = ai_open_ai_user[:host]
ai_open_ai_user[:whisper][:host] = ai_open_ai_user[:host]
ai_open_ai_user[:models][:host] = ai_open_ai_user[:host]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.access_token") and
personal_settings[team_id_user]["ai.open_ai.access_token"] != ""
ai_open_ai_user[:access_token] = personal_settings[team_id_user]["ai.open_ai.access_token"]
ai_open_ai_user[:chat_gpt][:access_token] = ai_open_ai_user[:access_token]
ai_open_ai_user[:dall_e][:access_token] = ai_open_ai_user[:access_token]
ai_open_ai_user[:whisper][:access_token] = ai_open_ai_user[:access_token]
ai_open_ai_user[:models][:access_token] = ai_open_ai_user[:access_token]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.chat_gpt.model") and
personal_settings[team_id_user]["ai.open_ai.chat_gpt.model"] != ""
ai_open_ai_user[:chat_gpt][:model] = personal_settings[team_id_user]["ai.open_ai.chat_gpt.model"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.chat_gpt.smartbot_model") and
personal_settings[team_id_user]["ai.open_ai.chat_gpt.smartbot_model"] != ""
ai_open_ai_user[:chat_gpt][:smartbot_model] = personal_settings[team_id_user]["ai.open_ai.chat_gpt.smartbot_model"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.whisper.model") and
personal_settings[team_id_user]["ai.open_ai.whisper.model"] != ""
ai_open_ai_user[:whisper][:model] = personal_settings[team_id_user]["ai.open_ai.whisper.model"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.dall_e.image_size") and
personal_settings[team_id_user]["ai.open_ai.dall_e.image_size"] != ""
ai_open_ai_user[:dall_e][:image_size] = personal_settings[team_id_user]["ai.open_ai.dall_e.image_size"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.chat_gpt.host") and
personal_settings[team_id_user]["ai.open_ai.chat_gpt.host"] != ""
ai_open_ai_user[:chat_gpt][:host] = personal_settings[team_id_user]["ai.open_ai.chat_gpt.host"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.dall_e.host") and
personal_settings[team_id_user]["ai.open_ai.dall_e.host"] != ""
ai_open_ai_user[:dall_e][:host] = personal_settings[team_id_user]["ai.open_ai.dall_e.host"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.whisper.host") and
personal_settings[team_id_user]["ai.open_ai.whisper.host"] != ""
ai_open_ai_user[:whisper][:host] = personal_settings[team_id_user]["ai.open_ai.whisper.host"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.chat_gpt.access_token") and
personal_settings[team_id_user]["ai.open_ai.chat_gpt.access_token"] != ""
ai_open_ai_user[:chat_gpt][:access_token] = personal_settings[team_id_user]["ai.open_ai.chat_gpt.access_token"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.dall_e.access_token") and
personal_settings[team_id_user]["ai.open_ai.dall_e.access_token"] != ""
ai_open_ai_user[:dall_e][:access_token] = personal_settings[team_id_user]["ai.open_ai.dall_e.access_token"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.whisper.access_token") and
personal_settings[team_id_user]["ai.open_ai.whisper.access_token"] != ""
ai_open_ai_user[:whisper][:access_token] = personal_settings[team_id_user]["ai.open_ai.whisper.access_token"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.chat_gpt.api_type") and
personal_settings[team_id_user]["ai.open_ai.chat_gpt.api_type"] != ""
ai_open_ai_user[:chat_gpt][:api_type] = personal_settings[team_id_user]["ai.open_ai.chat_gpt.api_type"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.chat_gpt.api_version") and
personal_settings[team_id_user]["ai.open_ai.chat_gpt.api_version"] != ""
ai_open_ai_user[:chat_gpt][:api_version] = personal_settings[team_id_user]["ai.open_ai.chat_gpt.api_version"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.models.host") and
personal_settings[team_id_user]["ai.open_ai.models.host"] != ""
ai_open_ai_user[:models][:host] = personal_settings[team_id_user]["ai.open_ai.models.host"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.models.access_token") and
personal_settings[team_id_user]["ai.open_ai.models.access_token"] != ""
ai_open_ai_user[:models][:access_token] = personal_settings[team_id_user]["ai.open_ai.models.access_token"]
end
if personal_settings.key?(team_id_user) and personal_settings[team_id_user].key?("ai.open_ai.models.url") and
personal_settings[team_id_user]["ai.open_ai.models.url"] != ""
ai_open_ai_user[:models][:url] = personal_settings[team_id_user]["ai.open_ai.models.url"]
end
host = ai_open_ai_user[service].host
access_token = ai_open_ai_user[service].access_token
ai_open_ai[team_id_user] ||= ai_open_ai_user.deep_copy
if ai_open_ai.key?(team_id_user) and ai_open_ai[team_id_user] != nil and ai_open_ai[team_id_user][service].key?(:client) and
ai_open_ai[team_id_user][service][:client] != nil and !reconnect
elsif access_token.to_s != ""
ai_open_ai[team_id_user][service] = ai_open_ai_user[service].deep_copy
if host == ""
ai_open_ai[team_id_user][service][:client] = ::OpenAI::Client.new(uri_base: "https://api.openai.com/", access_token: access_token, request_timeout: 300)
else
if ai_open_ai_user[service].key?(:url) and ai_open_ai_user[service][:url] != ""
ai_open_ai[team_id_user][service][:client] = NiceHttp.new(host: host, headers: { 'Authorization': "Bearer #{ai_open_ai_user[service][:access_token]}" }, ssl: true, timeout: (30))
elsif ai_open_ai_user[service].key?(:api_type) and ai_open_ai_user[service][:api_type] == :openai_azure
if general_config._ai.open_ai.key?(:testing) and general_config._ai.open_ai.testing log = "#{general_config.path}/logs/chat_gpt_azure_#{team_id_user}.log"
else
log = :no
end
ai_open_ai[team_id_user][service][:client] = NiceHttp.new(host: host, headers: { 'api-key': access_token }, ssl: true, timeout: (30), log: log)
else
ai_open_ai[team_id_user][service][:client] = ::OpenAI::Client.new(uri_base: host, access_token: access_token, request_timeout: 300)
end
end
else
ai_open_ai[team_id_user] = nil
message = ["You need to set the OpenAI access token in the config file or in the personal settings."]
message << "You can get it from https://platform.openai.com/account/api-keys"
message << "If you want to use your personal access token, you can set it on a DM with SmartBot in the personal settings:"
message << " `set personal settings ai.open_ai.#{service}.access_token ACCESS_TOKEN`"
if service == :chat_gpt
message << "By default we will be using the chatgpt model #{general_config._ai.open_ai.chat_gpt.model}. You can change it in the config file or in personal settings:"
message << " `set personal settings ai.open_ai.chat_gpt.model gpt-4-0314`"
message << "For specifying the model for ChatGPT on REPLs: `set personal settings ai.open_ai.chat_gpt.smartbot_model gpt-4-0314`"
elsif service == :whisper
message << "By default we will be using the whisper model #{general_config._ai.open_ai.whisper.model}. You can change it in the config file or in personal settings:"
message << " `set personal settings ai.open_ai.whisper.model whisper-1`"
elsif service == :dall_e
message << "You can also change the image size in the config file or in personal settings:"
message << " `set personal settings ai.open_ai.dall_e.image_size 512x512`"
end
message << "In case you are a master admin, you can set it in the SmartBot config file:"
message << " `ai: { open_ai: { #{service}: { access_token: 'ACCESS_TOKEN'} } }`"
return ai_open_ai, message.join("\n")
end
return ai_open_ai, ""
end
|