QdrantSearch: Add basic test
This commit is contained in:
parent
61e9027131
commit
933117785f
@ -5,12 +5,13 @@ defmodule Pleroma.Search.QdrantSearch do
|
|||||||
|
|
||||||
alias __MODULE__.QdrantClient
|
alias __MODULE__.QdrantClient
|
||||||
alias __MODULE__.OllamaClient
|
alias __MODULE__.OllamaClient
|
||||||
|
alias Pleroma.Config.Getting, as: Config
|
||||||
|
|
||||||
import Pleroma.Search.Meilisearch, only: [object_to_search_data: 1]
|
import Pleroma.Search.Meilisearch, only: [object_to_search_data: 1]
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def create_index() do
|
def create_index() do
|
||||||
payload = Pleroma.Config.get([Pleroma.Search.QdrantSearch, :qdrant_index_configuration])
|
payload = Config.get([Pleroma.Search.QdrantSearch, :qdrant_index_configuration])
|
||||||
|
|
||||||
with {:ok, %{status: 200}} <- QdrantClient.put("/collections/posts", payload) do
|
with {:ok, %{status: 200}} <- QdrantClient.put("/collections/posts", payload) do
|
||||||
:ok
|
:ok
|
||||||
@ -32,7 +33,7 @@ defmodule Pleroma.Search.QdrantSearch do
|
|||||||
with {:ok, %{body: %{"embedding" => embedding}}} <-
|
with {:ok, %{body: %{"embedding" => embedding}}} <-
|
||||||
OllamaClient.post("/api/embeddings", %{
|
OllamaClient.post("/api/embeddings", %{
|
||||||
prompt: text,
|
prompt: text,
|
||||||
model: Pleroma.Config.get([Pleroma.Search.QdrantSearch, :ollama_model])
|
model: Config.get([Pleroma.Search.QdrantSearch, :ollama_model])
|
||||||
}) do
|
}) do
|
||||||
{:ok, embedding}
|
{:ok, embedding}
|
||||||
else
|
else
|
||||||
@ -111,15 +112,17 @@ end
|
|||||||
|
|
||||||
defmodule Pleroma.Search.QdrantSearch.OllamaClient do
|
defmodule Pleroma.Search.QdrantSearch.OllamaClient do
|
||||||
use Tesla
|
use Tesla
|
||||||
|
alias Pleroma.Config.Getting, as: Config
|
||||||
|
|
||||||
plug(Tesla.Middleware.BaseUrl, Pleroma.Config.get([Pleroma.Search.QdrantSearch, :ollama_url]))
|
plug(Tesla.Middleware.BaseUrl, Config.get([Pleroma.Search.QdrantSearch, :ollama_url]))
|
||||||
plug(Tesla.Middleware.JSON)
|
plug(Tesla.Middleware.JSON)
|
||||||
end
|
end
|
||||||
|
|
||||||
defmodule Pleroma.Search.QdrantSearch.QdrantClient do
|
defmodule Pleroma.Search.QdrantSearch.QdrantClient do
|
||||||
use Tesla
|
use Tesla
|
||||||
|
alias Pleroma.Config.Getting, as: Config
|
||||||
|
|
||||||
plug(Tesla.Middleware.BaseUrl, Pleroma.Config.get([Pleroma.Search.QdrantSearch, :qdrant_url]))
|
plug(Tesla.Middleware.BaseUrl, Config.get([Pleroma.Search.QdrantSearch, :qdrant_url]))
|
||||||
plug(Tesla.Middleware.JSON)
|
plug(Tesla.Middleware.JSON)
|
||||||
|
|
||||||
plug(Tesla.Middleware.Headers, [
|
plug(Tesla.Middleware.Headers, [
|
||||||
|
65
test/pleroma/search/qdrant_search_test.exs
Normal file
65
test/pleroma/search/qdrant_search_test.exs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Search.QdrantSearchTest do
|
||||||
|
use Pleroma.DataCase, async: true
|
||||||
|
use Oban.Testing, repo: Pleroma.Repo
|
||||||
|
|
||||||
|
import Pleroma.Factory
|
||||||
|
import Mox
|
||||||
|
|
||||||
|
alias Pleroma.Web.CommonAPI
|
||||||
|
alias Pleroma.UnstubbedConfigMock, as: Config
|
||||||
|
alias Pleroma.Search.QdrantSearch
|
||||||
|
alias Pleroma.Workers.SearchIndexingWorker
|
||||||
|
|
||||||
|
describe "Qdrant search" do
|
||||||
|
test "indexes a public post on creation" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
Tesla.Mock.mock(fn
|
||||||
|
%{method: :post, url: "https://ollama.url/api/embeddings"} ->
|
||||||
|
send(self(), "posted_to_ollama")
|
||||||
|
Tesla.Mock.json(%{embedding: [1, 2, 3]})
|
||||||
|
|
||||||
|
%{method: :put, url: "https://qdrant.url/collections/posts/points", body: body} ->
|
||||||
|
send(self(), "posted_to_qdrant")
|
||||||
|
|
||||||
|
assert match?(%{"points" => [%{"vector" => [1, 2, 3]}]}, Jason.decode!(body))
|
||||||
|
|
||||||
|
Tesla.Mock.json("ok")
|
||||||
|
end)
|
||||||
|
|
||||||
|
Config
|
||||||
|
|> expect(:get, 4, fn
|
||||||
|
[Pleroma.Search, :module], nil ->
|
||||||
|
QdrantSearch
|
||||||
|
|
||||||
|
[Pleroma.Search.QdrantSearch, key], nil ->
|
||||||
|
%{
|
||||||
|
ollama_model: "a_model",
|
||||||
|
ollama_url: "https://ollama.url",
|
||||||
|
qdrant_url: "https://qdrant.url"
|
||||||
|
}[key]
|
||||||
|
end)
|
||||||
|
|
||||||
|
{:ok, activity} =
|
||||||
|
CommonAPI.post(user, %{
|
||||||
|
status: "guys i just don't wanna leave the swamp",
|
||||||
|
visibility: "public"
|
||||||
|
})
|
||||||
|
|
||||||
|
args = %{"op" => "add_to_index", "activity" => activity.id}
|
||||||
|
|
||||||
|
assert_enqueued(
|
||||||
|
worker: SearchIndexingWorker,
|
||||||
|
args: args
|
||||||
|
)
|
||||||
|
|
||||||
|
assert :ok = perform_job(SearchIndexingWorker, args)
|
||||||
|
assert_received("posted_to_ollama")
|
||||||
|
assert_received("posted_to_qdrant")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user