Skip to content

Commit

Permalink
Embeddings CRUD UI
Browse files Browse the repository at this point in the history
  • Loading branch information
romanrizzi committed Jan 9, 2025
1 parent 9c4d9eb commit 54cdb54
Show file tree
Hide file tree
Showing 20 changed files with 572 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import DiscourseRoute from "discourse/routes/discourse";

export default class AdminPluginsShowDiscourseAiEmbeddingsEdit extends DiscourseRoute {
async model(params) {
const allEmbeddings = this.modelFor("adminPlugins.show.discourse-ai-embeddings");
const id = parseInt(params.id, 10);
const record = allEmbeddings.findBy("id", id);
record.provider_params = record.provider_params || {};
return record;
}

setupController(controller, model) {
super.setupController(controller, model);
controller.set(
"allEmbeddings",
this.modelFor("adminPlugins.show.discourse-ai-embeddings")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import DiscourseRoute from "discourse/routes/discourse";

export default class AdminPluginsShowDiscourseAiEmbeddingsNew extends DiscourseRoute {
async model() {
const record = this.store.createRecord("ai-embedding");
record.provider_params = {};
return record;
}

setupController(controller, model) {
super.setupController(controller, model);
controller.set(
"allEmbeddings",
this.modelFor("adminPlugins.show.discourse-ai-embeddings")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import DiscourseRoute from "discourse/routes/discourse";

export default class DiscourseAiAiEmbeddingsRoute extends DiscourseRoute {
model() {
return this.store.findAll("ai-embedding");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<AiEmbeddingsListEditor
@embeddings={{this.allEmbeddings}}
@currentEmbedding={{this.model}}
/>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<AiEmbeddingsListEditor @embeddings={{this.model}} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<AiEmbeddingsListEditor
@embeddings={{this.allEmbeddings}}
@currentEmbedding={{this.model}}
/>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def index
meta: {
provider_params: EmbeddingDefinition.provider_params,
providers: EmbeddingDefinition.provider_names,
distance_functions: EmbeddingDefinition.distance_functions,
tokenizers:
EmbeddingDefinition.tokenizer_names.map { |tn|
{ id: tn, name: tn.split("::").last }
Expand Down
26 changes: 8 additions & 18 deletions app/models/embedding_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

class EmbeddingDefinition < ActiveRecord::Base
CLOUDFLARE = "cloudflare"
DISCOURSE = "discourse"
HUGGING_FACE = "hugging_face"
OPEN_AI = "open_ai"
GEMINI = "gemini"
GOOGLE = "google"

class << self
def provider_names
[CLOUDFLARE, DISCOURSE, HUGGING_FACE, OPEN_AI, GEMINI]
[CLOUDFLARE, HUGGING_FACE, OPEN_AI, GOOGLE]
end

def distance_functions
%w[<#> <=>]
end

def tokenizer_names
Expand All @@ -24,7 +27,7 @@ def tokenizer_names
end

def provider_params
{ discourse: { model_name: :text }, open_ai: { model_name: :text } }
{ open_ai: { model_name: :text } }
end
end

Expand All @@ -41,13 +44,11 @@ def inference_client
case provider
when CLOUDFLARE
cloudflare_client
when DISCOURSE
discourse_client
when HUGGING_FACE
hugging_face_client
when OPEN_AI
open_ai_client
when GEMINI
when GOOGLE
gemini_client
else
raise "Uknown embeddings provider"
Expand Down Expand Up @@ -91,17 +92,6 @@ def cloudflare_client
DiscourseAi::Inference::CloudflareWorkersAi.new(endpoint_url, api_key)
end

def discourse_client
client_url = endpoint_url
client_url = "#{client_url}/api/v1/classify" if url.starts_with?("srv://")

DiscourseAi::Inference::DiscourseClassifier.new(
client_url,
api_key,
lookup_custom_param("model_name"),
)
end

def hugging_face_client
DiscourseAi::Inference::HuggingFaceTextEmbeddings.new(endpoint_url, api_key)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,14 @@ export default {
});
this.route("discourse-ai-spam", { path: "ai-spam" });
this.route("discourse-ai-usage", { path: "ai-usage" });

this.route(
"discourse-ai-embeddings",
{ path: "ai-embeddings" },
function () {
this.route("new");
this.route("edit", { path: "/:id/edit" });
}
);
},
};
21 changes: 21 additions & 0 deletions assets/javascripts/discourse/admin/adapters/ai-embedding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import RestAdapter from "discourse/adapters/rest";

export default class Adapter extends RestAdapter {
jsonMode = true;

basePath() {
return "/admin/plugins/discourse-ai/";
}

pathFor(store, type, findArgs) {
// removes underscores which are implemented in base
let path =
this.basePath(store, type, findArgs) +
store.pluralize(this.apiNameFor(type));
return this.appendQueryParams(path, findArgs);
}

apiNameFor() {
return "ai-embedding";
}
}
33 changes: 33 additions & 0 deletions assets/javascripts/discourse/admin/models/ai-embedding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ajax } from "discourse/lib/ajax";
import RestModel from "discourse/models/rest";

export default class AiEmbedding extends RestModel {
createProperties() {
return this.getProperties(
"id",
"display_name",
"dimensions",
"provider",
"tokenizer_class",
"dimensions",
"url",
"api_key",
"max_sequence_length",
"provider_params",
"pg_function"
);
}

updateProperties() {
const attrs = this.createProperties();
attrs.id = this.id;

return attrs;
}

async testConfig() {
return await ajax(`/admin/plugins/discourse-ai/ai-embeddings/test.json`, {
data: { ai_embedding: this.createProperties() },
});
}
}
Loading

0 comments on commit 54cdb54

Please sign in to comment.