Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NekkoAPI executor #71

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/helpers/model_versions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module ModelVersionsHelper
DEFAULT_CONFIGURATION = {
'openai' => '{"model":"gpt-3.5-turbo","temperature":0.5}',
'ollama' => '{"model":"llama3.1", "stream": false}',
'nekkoai' => '{"model":"llama","max_tokens":200,"top_p":0.3,"stop":["4.","sushi"]}',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything but "model": "llama" has only meaning in the context of the example it was used in. We definitely don't care about sushi in llamagator, or do we? :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure at this moment if nekkoai is correct name for this connector.
The company is named "Ainekko". The llama.cpp wrapper - NekkoAPI (or nekko-api).
@Sergyenko please advise.

'llama_cpp' => '{"n_predict":500,"temperature":0.5,"stop":["<|end|>","<|user|>","<|assistant|>","<|endoftext|>","<|system|>"]}'
}.freeze

Expand Down
1 change: 1 addition & 0 deletions app/javascript/controllers/model_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Controller } from "@hotwired/stimulus"
const DEFAULT_CONFIGURATION = {
openai: '{"model":"gpt-3.5-turbo","temperature":0.5}',
ollama: '{"model":"llama3.1", "stream": false}',
nekkoai: '{"model":"llama","max_tokens":200,"top_p":0.3,"stop":["4.","sushi"]}',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same: please leave just model specifier.

llama_cpp: '{"n_predict":500,"temperature":0.5,"stop":["<|end|>","<|user|>","<|assistant|>","<|endoftext|>","<|system|>"]}'
}

Expand Down
2 changes: 1 addition & 1 deletion app/models/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Model < ApplicationRecord
has_many :model_versions, dependent: :destroy
accepts_nested_attributes_for :model_versions, allow_destroy: true, reject_if: :all_blank

enum :executor_type, { llama_cpp: 0, openai: 1, ollama: 2 }, scopes: false, default: :llama_cpp
enum :executor_type, { llama_cpp: 0, openai: 1, ollama: 2, nekkoai: 3 }, scopes: false, default: :llama_cpp

default_scope { order(id: :desc) }
end
24 changes: 24 additions & 0 deletions app/services/clients/nekkoai_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Clients
class NekkoaiClient
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need custom client for Nekkoai - it's fully OpenAI compatible. You can use OpenAI client and just change config (URL).

attr_reader :api_url, :model

def initialize(api_url:, model:)
@api_url = api_url
@model = model
end

def chat_completions(payload)
uri = URI(api_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')

request = Net::HTTP::Post.new(uri.path, { 'Content-Type' => 'application/json' })
request.body = payload.to_json

response = http.request(request)
JSON.parse(response.body)
end
end
end
29 changes: 29 additions & 0 deletions app/services/executors/nekkoai.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Executors
class Nekkoai < Executors::Base
def initialize(model_version)
@client = Clients::NekkoaiClient.new(api_url: model_version.model.url, model: 'llama')
super(model_version)
end

def call(prompt)
model_version.model

parameters = model_version.configuration.merge(messages: [{ role: 'user', content: prompt }])

response = client.chat_completions(parameters)
result = response['choices'].first['message']['content']

{ status: :completed, result: result.to_json.to_s }
rescue StandardError => e
return { status: :failed, result: e.response[:body].to_json.to_s } if e.respond_to?(:response)

{ status: :failed }
end

private

attr_reader :client
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ en:
llama_cpp: 'Llama.cpp'
openai: 'OpenAI'
ollama: 'Ollama'
nekkoai: 'NekkoAI'
Loading