|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module DiscourseAi |
| 4 | + module Completions |
| 5 | + module Endpoints |
| 6 | + class Cohere < Base |
| 7 | + class << self |
| 8 | + def can_contact?(endpoint_name, model_name) |
| 9 | + return false unless endpoint_name == "cohere" |
| 10 | + |
| 11 | + %w[command-light command command-r command-r-plus].include?(model_name) |
| 12 | + end |
| 13 | + |
| 14 | + def dependant_setting_names |
| 15 | + %w[ai_cohere_api_key] |
| 16 | + end |
| 17 | + |
| 18 | + def correctly_configured?(model_name) |
| 19 | + SiteSetting.ai_cohere_api_key.present? |
| 20 | + end |
| 21 | + |
| 22 | + def endpoint_name(model_name) |
| 23 | + "Cohere - #{model_name}" |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + def normalize_model_params(model_params) |
| 28 | + model_params = model_params.dup |
| 29 | + |
| 30 | + model_params[:p] = model_params.delete(:top_p) if model_params[:top_p] |
| 31 | + |
| 32 | + model_params |
| 33 | + end |
| 34 | + |
| 35 | + def default_options(dialect) |
| 36 | + options = { model: "command-r-plus" } |
| 37 | + |
| 38 | + options[:stop_sequences] = ["</function_calls>"] if dialect.prompt.has_tools? |
| 39 | + options |
| 40 | + end |
| 41 | + |
| 42 | + def provider_id |
| 43 | + AiApiAuditLog::Provider::Cohere |
| 44 | + end |
| 45 | + |
| 46 | + private |
| 47 | + |
| 48 | + def model_uri |
| 49 | + URI("https://api.cohere.ai/v1/chat") |
| 50 | + end |
| 51 | + |
| 52 | + def prepare_payload(prompt, model_params, dialect) |
| 53 | + payload = default_options(dialect).merge(model_params).merge(prompt) |
| 54 | + |
| 55 | + payload[:stream] = true if @streaming_mode |
| 56 | + |
| 57 | + payload |
| 58 | + end |
| 59 | + |
| 60 | + def prepare_request(payload) |
| 61 | + headers = { |
| 62 | + "Content-Type" => "application/json", |
| 63 | + "Authorization" => "Bearer #{SiteSetting.ai_cohere_api_key}", |
| 64 | + } |
| 65 | + |
| 66 | + Net::HTTP::Post.new(model_uri, headers).tap { |r| r.body = payload } |
| 67 | + end |
| 68 | + |
| 69 | + def extract_completion_from(response_raw) |
| 70 | + parsed = JSON.parse(response_raw, symbolize_names: true) |
| 71 | + |
| 72 | + if @streaming_mode |
| 73 | + if parsed[:event_type] == "text-generation" |
| 74 | + parsed[:text] |
| 75 | + else |
| 76 | + if parsed[:event_type] == "stream-end" |
| 77 | + @input_tokens = parsed.dig(:response, :meta, :billed_units, :input_tokens) |
| 78 | + @output_tokens = parsed.dig(:response, :meta, :billed_units, :output_tokens) |
| 79 | + end |
| 80 | + nil |
| 81 | + end |
| 82 | + else |
| 83 | + @input_tokens = parsed.dig(:meta, :billed_units, :input_tokens) |
| 84 | + @output_tokens = parsed.dig(:meta, :billed_units, :output_tokens) |
| 85 | + parsed[:text].to_s |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + def final_log_update(log) |
| 90 | + log.request_tokens = @input_tokens if @input_tokens |
| 91 | + log.response_tokens = @output_tokens if @output_tokens |
| 92 | + end |
| 93 | + |
| 94 | + def partials_from(decoded_chunk) |
| 95 | + decoded_chunk.split("\n").compact |
| 96 | + end |
| 97 | + |
| 98 | + def extract_prompt_for_tokenizer(prompt) |
| 99 | + text = +"" |
| 100 | + if prompt[:chat_history] |
| 101 | + text << prompt[:chat_history] |
| 102 | + .map { |message| message[:content] || message["content"] || "" } |
| 103 | + .join("\n") |
| 104 | + end |
| 105 | + |
| 106 | + text << prompt[:message] if prompt[:message] |
| 107 | + text << prompt[:preamble] if prompt[:preamble] |
| 108 | + |
| 109 | + text |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments