mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-07-01 03:52:34 +00:00
* DEV: refactor bot internals This introduces a proper object for bot context, this makes it simpler to improve context management as we go cause we have a nice object to work with Starts refactoring allowing for a single message to have multiple uploads throughout * transplant method to message builder * chipping away at inline uploads * image support is improved but not fully fixed yet partially working in anthropic, still got quite a few dialects to go * open ai and claude are now working * Gemini is now working as well * fix nova * more dialects... * fix ollama * fix specs * update artifact fixed * more tests * spam scanner * pass more specs * bunch of specs improved * more bug fixes. * all the rest of the tests are working * improve tests coverage and ensure custom tools are aware of new context object * tests are working, but we need more tests * resolve merge conflict * new preamble and expanded specs on ai tool * remove concept of "standalone tools" This is no longer needed, we can set custom raw, tool details are injected into tool calls
98 lines
2.3 KiB
Ruby
98 lines
2.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module DiscourseAi
|
|
module Completions
|
|
module Dialects
|
|
class Ollama < Dialect
|
|
class << self
|
|
def can_translate?(llm_model)
|
|
llm_model.provider == "ollama"
|
|
end
|
|
end
|
|
|
|
def native_tool_support?
|
|
enable_native_tool?
|
|
end
|
|
|
|
def max_prompt_tokens
|
|
llm_model.max_prompt_tokens
|
|
end
|
|
|
|
private
|
|
|
|
def tools_dialect
|
|
if enable_native_tool?
|
|
@tools_dialect ||= DiscourseAi::Completions::Dialects::OllamaTools.new(prompt.tools)
|
|
else
|
|
super
|
|
end
|
|
end
|
|
|
|
def tokenizer
|
|
llm_model.tokenizer_class
|
|
end
|
|
|
|
def model_msg(msg)
|
|
{ role: "assistant", content: msg[:content] }
|
|
end
|
|
|
|
def tool_call_msg(msg)
|
|
if enable_native_tool?
|
|
tools_dialect.from_raw_tool_call(msg)
|
|
else
|
|
super
|
|
end
|
|
end
|
|
|
|
def tool_msg(msg)
|
|
if enable_native_tool?
|
|
tools_dialect.from_raw_tool(msg)
|
|
else
|
|
super
|
|
end
|
|
end
|
|
|
|
def system_msg(msg)
|
|
msg = { role: "system", content: msg[:content] }
|
|
|
|
if tools_dialect.instructions.present?
|
|
msg[:content] = msg[:content].dup << "\n\n#{tools_dialect.instructions}"
|
|
end
|
|
|
|
msg
|
|
end
|
|
|
|
def enable_native_tool?
|
|
return @enable_native_tool if defined?(@enable_native_tool)
|
|
|
|
@enable_native_tool = llm_model.lookup_custom_param("enable_native_tool")
|
|
end
|
|
|
|
def user_msg(msg)
|
|
user_message = { role: "user", content: prompt.text_only(msg) }
|
|
|
|
encoded_uploads = prompt.encoded_uploads(msg)
|
|
if encoded_uploads.present?
|
|
images =
|
|
encoded_uploads
|
|
.map do |upload|
|
|
if upload[:mime_type].start_with?("image/")
|
|
upload[:base64]
|
|
else
|
|
nil
|
|
end
|
|
end
|
|
.compact
|
|
|
|
user_message[:images] = images if images.present?
|
|
end
|
|
|
|
# TODO: Add support for user messages with embedded user ids
|
|
|
|
user_message
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|