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" },
|
2024-07-30 12:44:57 -04:00
|
|
|
{ name: "gpt-4o-mini", tokens: 131_072, display_name: "GPT-4 Omni Mini" },
|
2024-06-21 03:32:15 -04:00
|
|
|
{ name: "gpt-4-turbo", tokens: 131_072, display_name: "GPT-4 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-09-11 21:28:08 -04:00
|
|
|
providers = %w[
|
|
|
|
aws_bedrock
|
|
|
|
anthropic
|
|
|
|
vllm
|
|
|
|
hugging_face
|
|
|
|
cohere
|
|
|
|
open_ai
|
|
|
|
google
|
|
|
|
azure
|
|
|
|
samba_nova
|
|
|
|
]
|
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-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)
|
2024-10-04 19:46:57 -04:00
|
|
|
@prompts << prompt.dup if @prompts
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
def proxy(model)
|
|
|
|
llm_model =
|
|
|
|
if model.is_a?(LlmModel)
|
|
|
|
model
|
|
|
|
else
|
|
|
|
model_name_without_prov = model.split(":").last.to_i
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
LlmModel.find_by(id: model_name_without_prov)
|
2024-02-15 00:37:59 -05:00
|
|
|
end
|
2024-05-16 08:50:22 -04:00
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
raise UNKNOWN_MODEL if llm_model.nil?
|
2024-05-13 11:46:42 -04:00
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
model_provider = llm_model.provider
|
|
|
|
dialect_klass = DiscourseAi::Completions::Dialects::Dialect.dialect_for(model_provider)
|
2024-05-21 12:35:50 -04:00
|
|
|
|
|
|
|
if @canned_response
|
2024-07-30 12:44:57 -04:00
|
|
|
if @canned_llm && @canned_llm != model
|
|
|
|
raise "Invalid call LLM call, expected #{@canned_llm} but got #{model}"
|
2024-05-21 12:35:50 -04:00
|
|
|
end
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
return new(dialect_klass, nil, llm_model, gateway: @canned_response)
|
2024-05-21 12:35:50 -04:00
|
|
|
end
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
gateway_klass = DiscourseAi::Completions::Endpoints::Base.endpoint_for(model_provider)
|
2023-11-23 10:58:54 -05:00
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
new(dialect_klass, gateway_klass, llm_model)
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
def initialize(dialect_klass, gateway_klass, llm_model, gateway: 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
|
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-07-30 12:44:57 -04:00
|
|
|
dialect = dialect_klass.new(prompt, llm_model, opts: model_params)
|
2024-05-16 08:50:22 -04:00
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
gateway = @gateway || gateway_klass.new(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-07-30 12:44:57 -04:00
|
|
|
llm_model.max_prompt_tokens
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
|
|
|
|
2024-05-16 08:50:22 -04:00
|
|
|
def tokenizer
|
2024-07-30 12:44:57 -04:00
|
|
|
llm_model.tokenizer_class
|
2024-05-16 08:50:22 -04:00
|
|
|
end
|
2024-05-07 09:02:16 -04:00
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
attr_reader :llm_model
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
private
|
|
|
|
|
2024-07-03 20:48:18 -04:00
|
|
|
attr_reader :dialect_klass, :gateway_klass
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|