generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05858ad
commit 8b44b77
Showing
38 changed files
with
566 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,7 @@ def ai_llm_params | |
:max_prompt_tokens, | ||
:url, | ||
:api_key, | ||
:bot_username, | ||
) | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class AddCompanionUserToLlmModel < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :llm_models, :bot_username, :string | ||
add_column :llm_models, :user_id, :integer | ||
end | ||
end |
51 changes: 0 additions & 51 deletions
51
db/post_migrate/20240523142424_custom_fields_to_track_ai_bot_users.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# frozen_string_literal: true | ||
|
||
class SeedOpenAiModels < ActiveRecord::Migration[7.0] | ||
def up | ||
models = [] | ||
|
||
open_ai_api_key = fetch_setting("ai_openai_api_key") | ||
|
||
if open_ai_api_key.present? | ||
models << mirror_open_ai( | ||
"GPT-3.5 Turbo", | ||
"gpt-3.5-turbo", | ||
8192, | ||
"ai_openai_gpt35_url", | ||
open_ai_api_key, | ||
"gpt3.5_bot", | ||
-111, | ||
) | ||
models << mirror_open_ai( | ||
"GPT-3.5 Turbo 16K", | ||
"gpt-3.5-turbo-16k", | ||
16_384, | ||
"ai_openai_gpt35_16k_url", | ||
open_ai_api_key, | ||
"gpt3.5_bot", | ||
-111, | ||
) | ||
models << mirror_open_ai( | ||
"GPT-4", | ||
"gpt-4", | ||
8192, | ||
"ai_openai_gpt4_url", | ||
open_ai_api_key, | ||
"gpt4_bot", | ||
-110, | ||
) | ||
models << mirror_open_ai( | ||
"GPT-4 32K", | ||
"gpt-4-32k", | ||
32_768, | ||
"ai_openai_gpt4_32k_url", | ||
open_ai_api_key, | ||
"gpt4_bot", | ||
-110, | ||
) | ||
models << mirror_open_ai( | ||
"GPT-4 Turbo", | ||
"gpt-4-turbo", | ||
131_072, | ||
"ai_openai_gpt4_turbo_url", | ||
open_ai_api_key, | ||
"gpt4t_bot", | ||
-113, | ||
) | ||
models << mirror_open_ai( | ||
"GPT-4o", | ||
"gpt-4o", | ||
131_072, | ||
"ai_openai_gpt4o_url", | ||
open_ai_api_key, | ||
"gpt4o_bot", | ||
-121, | ||
) | ||
end | ||
|
||
if models.present? | ||
rows = models.compact.join(",") | ||
|
||
DB.exec(<<~SQL, rows: rows) if rows.present? | ||
INSERT INTO llm_models (display_name, name, provider, tokenizer, max_prompt_tokens, url, api_key, bot_username, user_id, created_at, updated_at) | ||
VALUES :rows; | ||
SQL | ||
end | ||
end | ||
|
||
def has_companion_user?(user_id) | ||
DB.query_single("SELECT id FROM users WHERE id = :user_id", user_id: user_id).first.present? | ||
end | ||
|
||
def fetch_setting(name) | ||
DB.query_single( | ||
"SELECT value FROM site_settings WHERE name = :setting_name", | ||
setting_name: name, | ||
).first | ||
end | ||
|
||
def mirror_open_ai(display_name, name, max_prompt_tokens, setting_name, key, bot_username, bot_id) | ||
url = fetch_setting(setting_name) | ||
|
||
user_id = has_companion_user?(bot_id) ? bot_id : "NULL" | ||
|
||
if url | ||
"(#{name.titleize}, #{name}, open_ai, OpenAiTokenizer, #{max_prompt_tokens}, #{url}, #{key}, #{bot_username}, #{user_id}, NOW(), NOW())" | ||
end | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# frozen_string_literal: true | ||
|
||
class SeedClaudeModels < ActiveRecord::Migration[7.0] | ||
def up | ||
claude_models = %w[claude-instant-1 claude-2 claude-3-haiku claude-3-sonnet claude-3-opus] | ||
|
||
models = [] | ||
|
||
bedrock_secret_access_key = fetch_setting("ai_bedrock_secret_access_key") | ||
|
||
if bedrock_secret_access_key.present? | ||
bedrock_region = fetch_setting("ai_bedrock_region") | ||
|
||
claude_models.each do |cm| | ||
url = | ||
"https://bedrock-runtime.#{bedrock_region}.amazonaws.com/model/#{mapped_bedrock_model(cm)}/invoke" | ||
|
||
bot_id = claude_bot_id(cm) | ||
user_id = has_companion_user?(bot_id) ? bot_id : "NULL" | ||
|
||
models << "(#{display_name(cm)}, #{cm}, aws_bedrock, AnthropicTokenizer, 200000, #{url}, #{bedrock_secret_access_key}, #{claude_bot_username(cm)}, #{user_id}, NOW(), NOW())" | ||
end | ||
end | ||
|
||
anthropic_ai_api_key = fetch_setting("ai_anthropic_api_key") | ||
if anthropic_ai_api_key.present? | ||
claude_models.each do |cm| | ||
url = "https://api.anthropic.com/v1/messages" | ||
|
||
bot_id = claude_bot_id(cm) | ||
user_id = has_companion_user?(bot_id) ? bot_id : "NULL" | ||
|
||
models << "(#{display_name(cm)}, #{cm}, anthropic, AnthropicTokenizer, 200000, #{url}, #{anthropic_ai_api_key}, #{claude_bot_username(cm)}, #{user_id}, NOW(), NOW())" | ||
end | ||
end | ||
|
||
if models.present? | ||
rows = models.compact.join(",") | ||
|
||
DB.exec(<<~SQL, rows: rows) if rows.present? | ||
INSERT INTO llm_models (display_name, name, provider, tokenizer, max_prompt_tokens, url, api_key, bot_username, user_id, created_at, updated_at) | ||
VALUES :rows; | ||
SQL | ||
end | ||
end | ||
|
||
def has_companion_user?(user_id) | ||
DB.query_single("SELECT id FROM users WHERE id = :user_id", user_id: user_id).first.present? | ||
end | ||
|
||
def fetch_setting(name) | ||
DB.query_single( | ||
"SELECT value FROM site_settings WHERE name = :setting_name", | ||
setting_name: name, | ||
).first | ||
end | ||
|
||
def claude_bot_username(model) | ||
case model | ||
when "claude-2" | ||
"claude_bot" | ||
when "claude-3-haiku" | ||
"claude_3_haiku_bot" | ||
when "claude-3-sonnet" | ||
"claude_3_sonnet_bot" | ||
when "claude-instant-1" | ||
"anthropic.claude-instant-v1" | ||
when "claude-3-opus" | ||
"claude_3_opus_bot" | ||
else | ||
"claude_instant_bot" | ||
end | ||
end | ||
|
||
def claude_bot_id(model) | ||
case model | ||
when "claude-2" | ||
-112 | ||
when "claude-3-haiku" | ||
-119 | ||
when "claude-3-sonnet" | ||
-118 | ||
when "claude-instant-1" | ||
nil | ||
when "claude-3-opus" | ||
-117 | ||
end | ||
end | ||
|
||
def mapped_bedrock_model(model) | ||
case model | ||
when "claude-2" | ||
"anthropic.claude-v2:1" | ||
when "claude-3-haiku" | ||
"anthropic.claude-3-haiku-20240307-v1:0" | ||
when "claude-3-sonnet" | ||
"anthropic.claude-3-sonnet-20240229-v1:0" | ||
when "claude-instant-1" | ||
"anthropic.claude-instant-v1" | ||
when "claude-3-opus" | ||
"anthropic.claude-3-opus-20240229-v1:0" | ||
end | ||
end | ||
|
||
def display_name(model) | ||
case model | ||
when "claude-2" | ||
"Claude 2" | ||
when "claude-3-haiku" | ||
"Claude 3 Haiku" | ||
when "claude-3-sonnet" | ||
"Claude 3 Sonnet" | ||
when "claude-instant-1" | ||
"Claude Instant 1" | ||
when "claude-3-opus" | ||
"Claude 3 Opus" | ||
end | ||
end | ||
|
||
def down | ||
raise ActiveRecord::IrreversibleMigration | ||
end | ||
end |
Oops, something went wrong.