FEATURE: API scope for semantic search (#785)

The new API scope allows restricting access to semantic search
only.
This commit is contained in:
Sam 2024-08-30 09:35:20 +10:00 committed by GitHub
parent 4176fbd3dd
commit eee8e72756
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 35 additions and 0 deletions

View File

@ -1,6 +1,12 @@
en:
admin_js:
admin:
api:
scopes:
descriptions:
discourse_ai:
search: "Allows semantic search via the /discourse-ai/embeddings/semantic-search endpoint."
site_settings:
categories:
discourse_ai: "Discourse AI"

View File

@ -67,6 +67,11 @@ module DiscourseAi
plugin.on(:topic_edited, &callback)
plugin.on(:post_created, &callback)
plugin.on(:post_edited, &callback)
plugin.add_api_key_scope(
:discourse_ai,
{ search: { actions: %w[discourse_ai/embeddings/embeddings#search] } },
)
end
end
end

View File

@ -36,6 +36,30 @@ describe DiscourseAi::Embeddings::EmbeddingsController do
EmbeddingsGenerationStubs.openai_service(SiteSetting.ai_embeddings_model, query, embedding)
end
def create_api_key(user)
key = ApiKey.create!(user: user)
ApiKeyScope.create!(resource: "discourse_ai", action: "search", api_key_id: key.id)
key
end
it "is able to make API requests using a scoped API key" do
index(topic)
query = "test"
stub_embedding(query)
user = topic.user
api_key = create_api_key(user)
get "/discourse-ai/embeddings/semantic-search.json?q=#{query}&hyde=false",
headers: {
"Api-Key" => api_key.key,
"Api-Username" => user.username,
}
expect(response.status).to eq(200)
expect(response.parsed_body["topics"].map { |t| t["id"] }).to contain_exactly(topic.id)
end
it "returns results correctly when performing a non Hyde search" do
index(topic)
index(topic_in_subcategory)