2023-11-23 10:58:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# A facade that abstracts multiple LLMs behind a single interface.
|
|
|
|
#
|
|
|
|
# Internally, it consists of the combination of a dialect and an endpoint.
|
2024-01-19 06:51:26 -05:00
|
|
|
# After receiving a prompt using our generic format, it translates it to
|
2023-11-23 10:58:54 -05:00
|
|
|
# the target model and routes the completion request through the correct gateway.
|
|
|
|
#
|
|
|
|
# Use the .proxy method to instantiate an object.
|
2024-01-29 14:04:25 -05:00
|
|
|
# It chooses the correct dialect and endpoint for the model you want to interact with.
|
2023-11-23 10:58:54 -05:00
|
|
|
#
|
|
|
|
# Tests of modules that perform LLM calls can use .with_prepared_responses to return canned responses
|
|
|
|
# instead of relying on WebMock stubs like we did in the past.
|
|
|
|
#
|
|
|
|
module DiscourseAi
|
|
|
|
module Completions
|
2023-11-28 23:17:46 -05:00
|
|
|
class Llm
|
2023-11-23 10:58:54 -05:00
|
|
|
UNKNOWN_MODEL = Class.new(StandardError)
|
|
|
|
|
2024-01-29 14:04:25 -05:00
|
|
|
class << self
|
2024-06-21 03:32:15 -04:00
|
|
|
def presets
|
|
|
|
# Sam: I am not sure if it makes sense to translate model names at all
|
|
|
|
@presets ||=
|
|
|
|
begin
|
|
|
|
[
|
|
|
|
{
|
|
|
|
id: "anthropic",
|
|
|
|
models: [
|
|
|
|
{
|
|
|
|
name: "claude-3-5-sonnet",
|
|
|
|
tokens: 200_000,
|
|
|
|
display_name: "Claude 3.5 Sonnet",
|
|
|
|
},
|
|
|
|
{ name: "claude-3-opus", tokens: 200_000, display_name: "Claude 3 Opus" },
|
|
|
|
{ name: "claude-3-sonnet", tokens: 200_000, display_name: "Claude 3 Sonnet" },
|
|
|
|
{ name: "claude-3-haiku", tokens: 200_000, display_name: "Claude 3 Haiku" },
|
|
|
|
],
|
|
|
|
tokenizer: DiscourseAi::Tokenizer::AnthropicTokenizer,
|
|
|
|
endpoint: "https://api.anthropic.com/v1/messages",
|
|
|
|
provider: "anthropic",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "google",
|
|
|
|
models: [
|
|
|
|
{
|
|
|
|
name: "gemini-1.5-pro",
|
|
|
|
tokens: 800_000,
|
|
|
|
endpoint:
|
|
|
|
"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest",
|
|
|
|
display_name: "Gemini 1.5 Pro",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "gemini-1.5-flash",
|
|
|
|
tokens: 800_000,
|
|
|
|
endpoint:
|
|
|
|
"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest",
|
|
|
|
display_name: "Gemini 1.5 Flash",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
tokenizer: DiscourseAi::Tokenizer::OpenAiTokenizer,
|
|
|
|
provider: "google",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "open_ai",
|
|
|
|
models: [
|
|
|
|
{ name: "gpt-4o", tokens: 131_072, display_name: "GPT-4 Omni" },
|
|
|
|
{ name: "gpt-4-turbo", tokens: 131_072, display_name: "GPT-4 Turbo" },
|
|
|
|
{ name: "gpt-3.5-turbo", tokens: 16_385, display_name: "GPT-3.5 Turbo" },
|
|
|
|
],
|
|
|
|
tokenizer: DiscourseAi::Tokenizer::OpenAiTokenizer,
|
|
|
|
endpoint: "https://api.openai.com/v1/chat/completions",
|
|
|
|
provider: "open_ai",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-05-13 11:46:42 -04:00
|
|
|
def provider_names
|
2024-05-13 14:54:42 -04:00
|
|
|
providers = %w[aws_bedrock anthropic vllm hugging_face cohere open_ai google azure]
|
2024-06-19 17:01:35 -04:00
|
|
|
if !Rails.env.production?
|
|
|
|
providers << "fake"
|
|
|
|
providers << "ollama"
|
|
|
|
end
|
|
|
|
|
2024-05-13 14:54:42 -04:00
|
|
|
providers
|
2024-05-13 11:46:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tokenizer_names
|
2024-05-16 08:50:22 -04:00
|
|
|
DiscourseAi::Tokenizer::BasicTokenizer.available_llm_tokenizers.map(&:name)
|
2024-05-13 11:46:42 -04:00
|
|
|
end
|
|
|
|
|
2024-05-28 09:31:15 -04:00
|
|
|
def vision_models_by_provider
|
|
|
|
@vision_models_by_provider ||= {
|
|
|
|
aws_bedrock: %w[claude-3-sonnet claude-3-opus claude-3-haiku],
|
|
|
|
anthropic: %w[claude-3-sonnet claude-3-opus claude-3-haiku],
|
|
|
|
open_ai: %w[gpt-4-vision-preview gpt-4-turbo gpt-4o],
|
|
|
|
google: %w[gemini-1.5-pro gemini-1.5-flash],
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2024-01-29 14:04:25 -05:00
|
|
|
def models_by_provider
|
|
|
|
# ChatGPT models are listed under open_ai but they are actually available through OpenAI and Azure.
|
|
|
|
# However, since they use the same URL/key settings, there's no reason to duplicate them.
|
2024-02-15 00:37:59 -05:00
|
|
|
@models_by_provider ||=
|
|
|
|
{
|
2024-04-17 01:37:19 -04:00
|
|
|
aws_bedrock: %w[
|
|
|
|
claude-instant-1
|
|
|
|
claude-2
|
|
|
|
claude-3-haiku
|
|
|
|
claude-3-sonnet
|
|
|
|
claude-3-opus
|
|
|
|
],
|
2024-03-18 15:48:46 -04:00
|
|
|
anthropic: %w[claude-instant-1 claude-2 claude-3-haiku claude-3-sonnet claude-3-opus],
|
2024-05-07 09:02:16 -04:00
|
|
|
vllm: %w[mistralai/Mixtral-8x7B-Instruct-v0.1 mistralai/Mistral-7B-Instruct-v0.2],
|
2024-02-15 00:37:59 -05:00
|
|
|
hugging_face: %w[
|
|
|
|
mistralai/Mixtral-8x7B-Instruct-v0.1
|
|
|
|
mistralai/Mistral-7B-Instruct-v0.2
|
|
|
|
],
|
2024-04-12 00:46:58 -04:00
|
|
|
cohere: %w[command-light command command-r command-r-plus],
|
2024-02-19 12:56:28 -05:00
|
|
|
open_ai: %w[
|
|
|
|
gpt-3.5-turbo
|
|
|
|
gpt-4
|
|
|
|
gpt-3.5-turbo-16k
|
|
|
|
gpt-4-32k
|
|
|
|
gpt-4-turbo
|
|
|
|
gpt-4-vision-preview
|
2024-05-13 23:28:46 -04:00
|
|
|
gpt-4o
|
2024-02-19 12:56:28 -05:00
|
|
|
],
|
2024-05-22 02:35:29 -04:00
|
|
|
google: %w[gemini-pro gemini-1.5-pro gemini-1.5-flash],
|
2024-05-07 09:02:16 -04:00
|
|
|
}.tap do |h|
|
|
|
|
h[:ollama] = ["mistral"] if Rails.env.development?
|
|
|
|
h[:fake] = ["fake"] if Rails.env.test? || Rails.env.development?
|
|
|
|
end
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
def valid_provider_models
|
|
|
|
return @valid_provider_models if defined?(@valid_provider_models)
|
|
|
|
|
|
|
|
valid_provider_models = []
|
|
|
|
models_by_provider.each do |provider, models|
|
|
|
|
valid_provider_models.concat(models.map { |model| "#{provider}:#{model}" })
|
|
|
|
end
|
|
|
|
@valid_provider_models = Set.new(valid_provider_models)
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_prepared_responses(responses, llm: nil)
|
2024-01-29 14:04:25 -05:00
|
|
|
@canned_response = DiscourseAi::Completions::Endpoints::CannedResponse.new(responses)
|
2024-02-15 00:37:59 -05:00
|
|
|
@canned_llm = llm
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
@prompts = []
|
2024-01-29 14:04:25 -05:00
|
|
|
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
yield(@canned_response, llm, @prompts)
|
2024-01-29 14:04:25 -05:00
|
|
|
ensure
|
|
|
|
# Don't leak prepared response if there's an exception.
|
|
|
|
@canned_response = nil
|
2024-02-15 00:37:59 -05:00
|
|
|
@canned_llm = nil
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
@prompts = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def record_prompt(prompt)
|
|
|
|
@prompts << prompt if @prompts
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def proxy(model_name)
|
|
|
|
provider_and_model_name = model_name.split(":")
|
|
|
|
provider_name = provider_and_model_name.first
|
|
|
|
model_name_without_prov = provider_and_model_name[1..].join
|
2024-05-13 11:46:42 -04:00
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
# We are in the process of transitioning to always use objects here.
|
|
|
|
# We'll live with this hack for a while.
|
|
|
|
if provider_name == "custom"
|
2024-05-13 11:46:42 -04:00
|
|
|
llm_model = LlmModel.find(model_name_without_prov)
|
2024-05-16 08:50:22 -04:00
|
|
|
raise UNKNOWN_MODEL if !llm_model
|
|
|
|
return proxy_from_obj(llm_model)
|
2024-05-13 11:46:42 -04:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-01-29 14:04:25 -05:00
|
|
|
dialect_klass =
|
|
|
|
DiscourseAi::Completions::Dialects::Dialect.dialect_for(model_name_without_prov)
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
if @canned_response
|
|
|
|
if @canned_llm && @canned_llm != model_name
|
|
|
|
raise "Invalid call LLM call, expected #{@canned_llm} but got #{model_name}"
|
|
|
|
end
|
2024-05-16 08:50:22 -04:00
|
|
|
|
|
|
|
return new(dialect_klass, nil, model_name, gateway: @canned_response)
|
2024-02-15 00:37:59 -05:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
gateway_klass = DiscourseAi::Completions::Endpoints::Base.endpoint_for(provider_name)
|
|
|
|
|
|
|
|
new(dialect_klass, gateway_klass, model_name_without_prov)
|
|
|
|
end
|
|
|
|
|
|
|
|
def proxy_from_obj(llm_model)
|
|
|
|
provider_name = llm_model.provider
|
|
|
|
model_name = llm_model.name
|
2024-05-13 11:46:42 -04:00
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
dialect_klass = DiscourseAi::Completions::Dialects::Dialect.dialect_for(model_name)
|
2024-05-21 12:35:50 -04:00
|
|
|
|
|
|
|
if @canned_response
|
2024-06-18 13:32:14 -04:00
|
|
|
if @canned_llm && @canned_llm != [provider_name, model_name].join(":")
|
2024-05-21 12:35:50 -04:00
|
|
|
raise "Invalid call LLM call, expected #{@canned_llm} but got #{model_name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
return new(dialect_klass, nil, model_name, gateway: @canned_response)
|
|
|
|
end
|
|
|
|
|
2024-05-13 14:54:42 -04:00
|
|
|
gateway_klass = DiscourseAi::Completions::Endpoints::Base.endpoint_for(provider_name)
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
new(dialect_klass, gateway_klass, model_name, llm_model: llm_model)
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
def initialize(dialect_klass, gateway_klass, model_name, gateway: nil, llm_model: nil)
|
2023-12-18 16:06:01 -05:00
|
|
|
@dialect_klass = dialect_klass
|
2024-05-01 03:50:58 -04:00
|
|
|
@gateway_klass = gateway_klass
|
2023-11-23 10:58:54 -05:00
|
|
|
@model_name = model_name
|
2024-05-16 08:50:22 -04:00
|
|
|
@gateway = gateway
|
|
|
|
@llm_model = llm_model
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
# @param generic_prompt { DiscourseAi::Completions::Prompt } - Our generic prompt object
|
2023-11-23 10:58:54 -05:00
|
|
|
# @param user { User } - User requesting the summary.
|
|
|
|
#
|
|
|
|
# @param &on_partial_blk { Block - Optional } - The passed block will get called with the LLM partial response alongside a cancel function.
|
|
|
|
#
|
|
|
|
# @returns { String } - Completion result.
|
2023-12-18 16:06:01 -05:00
|
|
|
#
|
|
|
|
# When the model invokes a tool, we'll wait until the endpoint finishes replying and feed you a fully-formed tool,
|
|
|
|
# even if you passed a partial_read_blk block. Invocations are strings that look like this:
|
|
|
|
#
|
|
|
|
# <function_calls>
|
|
|
|
# <invoke>
|
|
|
|
# <tool_name>get_weather</tool_name>
|
|
|
|
# <tool_id>get_weather</tool_id>
|
|
|
|
# <parameters>
|
|
|
|
# <location>Sydney</location>
|
|
|
|
# <unit>c</unit>
|
|
|
|
# </parameters>
|
|
|
|
# </invoke>
|
|
|
|
# </function_calls>
|
|
|
|
#
|
2024-01-04 07:53:47 -05:00
|
|
|
def generate(
|
2024-01-12 12:36:44 -05:00
|
|
|
prompt,
|
2024-01-04 07:53:47 -05:00
|
|
|
temperature: nil,
|
2024-01-30 17:58:25 -05:00
|
|
|
top_p: nil,
|
2024-01-04 07:53:47 -05:00
|
|
|
max_tokens: nil,
|
|
|
|
stop_sequences: nil,
|
|
|
|
user:,
|
2024-05-13 23:28:46 -04:00
|
|
|
feature_name: nil,
|
2024-01-04 07:53:47 -05:00
|
|
|
&partial_read_blk
|
|
|
|
)
|
FEATURE: Add vision support to AI personas (Claude 3) (#546)
This commit adds the ability to enable vision for AI personas, allowing them to understand images that are posted in the conversation.
For personas with vision enabled, any images the user has posted will be resized to be within the configured max_pixels limit, base64 encoded and included in the prompt sent to the AI provider.
The persona editor allows enabling/disabling vision and has a dropdown to select the max supported image size (low, medium, high). Vision is disabled by default.
This initial vision support has been tested and implemented with Anthropic's claude-3 models which accept images in a special format as part of the prompt.
Other integrations will need to be updated to support images.
Several specs were added to test the new functionality at the persona, prompt building and API layers.
- Gemini is omitted, pending API support for Gemini 1.5. Current Gemini bot is not performing well, adding images is unlikely to make it perform any better.
- Open AI is omitted, vision support on GPT-4 it limited in that the API has no tool support when images are enabled so we would need to full back to a different prompting technique, something that would add lots of complexity
---------
Co-authored-by: Martin Brennan <martin@discourse.org>
2024-03-26 23:30:11 -04:00
|
|
|
self.class.record_prompt(prompt)
|
|
|
|
|
2024-01-30 17:58:25 -05:00
|
|
|
model_params = { max_tokens: max_tokens, stop_sequences: stop_sequences }
|
|
|
|
|
|
|
|
model_params[:temperature] = temperature if temperature
|
|
|
|
model_params[:top_p] = top_p if top_p
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-01-15 02:51:14 -05:00
|
|
|
if prompt.is_a?(String)
|
|
|
|
prompt =
|
|
|
|
DiscourseAi::Completions::Prompt.new(
|
|
|
|
"You are a helpful bot",
|
|
|
|
messages: [{ type: :user, content: prompt }],
|
|
|
|
)
|
|
|
|
elsif prompt.is_a?(Array)
|
|
|
|
prompt = DiscourseAi::Completions::Prompt.new(messages: prompt)
|
|
|
|
end
|
|
|
|
|
2024-01-15 23:21:58 -05:00
|
|
|
if !prompt.is_a?(DiscourseAi::Completions::Prompt)
|
|
|
|
raise ArgumentError, "Prompt must be either a string, array, of Prompt object"
|
|
|
|
end
|
|
|
|
|
2024-01-04 07:53:47 -05:00
|
|
|
model_params.keys.each { |key| model_params.delete(key) if model_params[key].nil? }
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
dialect = dialect_klass.new(prompt, model_name, opts: model_params, llm_model: llm_model)
|
|
|
|
|
|
|
|
gateway = @gateway || gateway_klass.new(model_name, dialect.tokenizer, llm_model: llm_model)
|
2024-05-13 23:28:46 -04:00
|
|
|
gateway.perform_completion!(
|
|
|
|
dialect,
|
|
|
|
user,
|
|
|
|
model_params,
|
|
|
|
feature_name: feature_name,
|
|
|
|
&partial_read_blk
|
|
|
|
)
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
def max_prompt_tokens
|
2024-05-16 08:50:22 -04:00
|
|
|
llm_model&.max_prompt_tokens ||
|
|
|
|
dialect_klass.new(DiscourseAi::Completions::Prompt.new(""), model_name).max_prompt_tokens
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
def tokenizer
|
|
|
|
llm_model&.tokenizer_class ||
|
|
|
|
dialect_klass.new(DiscourseAi::Completions::Prompt.new(""), model_name).tokenizer
|
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
attr_reader :model_name
|
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
private
|
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
attr_reader :dialect_klass, :gateway_klass, :llm_model
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|