This commit is contained in:
89
lib/elixir_ai/data/ai_provider.ex
Normal file
89
lib/elixir_ai/data/ai_provider.ex
Normal file
@@ -0,0 +1,89 @@
|
||||
defmodule ElixirAi.AiProvider do
|
||||
import Ecto.Query
|
||||
alias ElixirAi.Repo
|
||||
|
||||
def all do
|
||||
Repo.all(
|
||||
from(p in "ai_providers",
|
||||
select: %{
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
model_name: p.model_name,
|
||||
api_token: p.api_token,
|
||||
completions_url: p.completions_url
|
||||
}
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
def create(attrs) do
|
||||
now = DateTime.truncate(DateTime.utc_now(), :second)
|
||||
|
||||
case Repo.insert_all("ai_providers", [
|
||||
[
|
||||
name: attrs.name,
|
||||
model_name: attrs.model_name,
|
||||
api_token: attrs.api_token,
|
||||
completions_url: attrs.completions_url,
|
||||
inserted_at: now,
|
||||
updated_at: now
|
||||
]
|
||||
]) do
|
||||
{1, _} ->
|
||||
Phoenix.PubSub.broadcast(
|
||||
ElixirAi.PubSub,
|
||||
"ai_providers",
|
||||
{:provider_added, attrs}
|
||||
)
|
||||
|
||||
:ok
|
||||
|
||||
_ ->
|
||||
{:error, :db_error}
|
||||
end
|
||||
rescue
|
||||
e in Ecto.ConstraintError ->
|
||||
if e.constraint == "ai_providers_name_key",
|
||||
do: {:error, :already_exists},
|
||||
else: {:error, :db_error}
|
||||
end
|
||||
|
||||
def find_by_name(name) do
|
||||
case Repo.one(
|
||||
from(p in "ai_providers",
|
||||
where: p.name == ^name,
|
||||
select: %{
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
model_name: p.model_name,
|
||||
api_token: p.api_token,
|
||||
completions_url: p.completions_url
|
||||
}
|
||||
)
|
||||
) do
|
||||
nil -> {:error, :not_found}
|
||||
provider -> {:ok, provider}
|
||||
end
|
||||
end
|
||||
|
||||
def ensure_default_provider do
|
||||
case Repo.aggregate(from(p in "ai_providers"), :count) do
|
||||
0 ->
|
||||
attrs = %{
|
||||
name: System.get_env("DEFAULT_PROVIDER_NAME", "default_provider"),
|
||||
model_name: System.get_env("DEFAULT_MODEL_NAME", "gpt-4"),
|
||||
api_token: System.get_env("DEFAULT_API_TOKEN", ""),
|
||||
completions_url:
|
||||
System.get_env(
|
||||
"DEFAULT_COMPLETIONS_URL",
|
||||
"https://api.openai.com/v1/chat/completions"
|
||||
)
|
||||
}
|
||||
|
||||
create(attrs)
|
||||
|
||||
_ ->
|
||||
:ok
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,20 +3,39 @@ defmodule ElixirAi.Conversation do
|
||||
alias ElixirAi.Repo
|
||||
|
||||
def all_names do
|
||||
Repo.all(from c in "conversations", select: c.name)
|
||||
Repo.all(
|
||||
from(c in "conversations",
|
||||
left_join: p in "ai_providers",
|
||||
on: c.ai_provider_id == p.id,
|
||||
select: %{
|
||||
name: c.name,
|
||||
provider: %{
|
||||
name: p.name,
|
||||
model_name: p.model_name,
|
||||
api_token: p.api_token,
|
||||
completions_url: p.completions_url
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
def create(name) do
|
||||
case Repo.insert_all("conversations", [[name: name, inserted_at: now(), updated_at: now()]]) do
|
||||
def create(name, ai_provider_id) do
|
||||
case Repo.insert_all("conversations", [
|
||||
[name: name, ai_provider_id: ai_provider_id, inserted_at: now(), updated_at: now()]
|
||||
]) do
|
||||
{1, _} -> :ok
|
||||
_ -> {:error, :db_error}
|
||||
end
|
||||
rescue
|
||||
e in Ecto.ConstraintError -> if e.constraint == "conversations_name_index", do: {:error, :already_exists}, else: {:error, :db_error}
|
||||
e in Ecto.ConstraintError ->
|
||||
if e.constraint == "conversations_name_index",
|
||||
do: {:error, :already_exists},
|
||||
else: {:error, :db_error}
|
||||
end
|
||||
|
||||
def find_id(name) do
|
||||
case Repo.one(from c in "conversations", where: c.name == ^name, select: c.id) do
|
||||
case Repo.one(from(c in "conversations", where: c.name == ^name, select: c.id)) do
|
||||
nil -> {:error, :not_found}
|
||||
id -> {:ok, id}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user