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

fix: add updates for new api routes for vectors #14

Merged
merged 1 commit into from
Jan 28, 2024
Merged
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
48 changes: 36 additions & 12 deletions lib/pinecone.ex
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,10 @@ defmodule Pinecone do
success_type(map()) | error_type()
@spec describe_index_stats(index :: index_type(), opts :: keyword()) ::
success_type(map()) | error_type()
def describe_index_stats(%Index{name: name, project_name: project_name}, opts \\ []) do
def describe_index_stats(%Index{name: name}, opts \\ []) do
opts = Keyword.validate!(opts, [:config])

get({:vectors, "#{name}-#{project_name}"}, "describe_index_stats", opts[:config])
get_vector("describe_index_stats", name, opts[:config], params: opts[:params])
end

@doc """
Expand All @@ -269,7 +269,7 @@ defmodule Pinecone do
success_type(map()) | error_type()
@spec upsert_vectors(index :: index_type(), vectors :: list(), opts :: keyword()) ::
success_type(map()) | error_type()
def upsert_vectors(%Index{name: name, project_name: project_name}, vectors, opts \\ []) do
def upsert_vectors(%Index{name: name}, vectors, opts \\ []) do
opts = Keyword.validate!(opts, [:config, :namespace])

body = %{"vectors" => List.wrap(vectors)}
Expand All @@ -282,7 +282,7 @@ defmodule Pinecone do
body
end

post({:vectors, "#{name}-#{project_name}"}, "vectors/upsert", body, opts[:config])
post_vector("upsert", name, body, opts[:config])
end

@doc """
Expand All @@ -297,12 +297,12 @@ defmodule Pinecone do
success_type(map()) | error_type()
@spec fetch_vectors(index :: index_type(), ids :: list(), opts :: keyword()) ::
success_type(map()) | error_type()
def fetch_vectors(%Index{name: name, project_name: project_name}, ids, opts \\ []) do
def fetch_vectors(%Index{name: name}, ids, opts \\ []) do
opts = Keyword.validate!(opts, [:config])

ids = Enum.map(ids, &{"ids", &1})

get({:vectors, "#{name}-#{project_name}"}, "vectors/fetch", opts[:config], params: ids)
get_vector("fetch", name, opts[:config], params: ids)
end

@doc """
Expand All @@ -320,14 +320,14 @@ defmodule Pinecone do
success_type(String.t()) | error_type()
@spec delete_vectors(index :: index_type(), ids :: list(), opts :: keyword()) ::
success_type(String.t()) | error_type()
def delete_vectors(%Index{name: name, project_name: project_name}, ids, opts \\ [])
def delete_vectors(%Index{name: name}, ids, opts \\ [])
when is_list(ids) do
opts = Keyword.validate!(opts, [:config, :namespace])

params = Enum.map(ids, &{"ids", &1})
params = if opts[:namespace], do: [{"namespace", opts[:namespace]} | params], else: params

delete({:vectors, "#{name}-#{project_name}"}, "vectors/delete", opts[:config], params: params)
delete_vector("delete", name, opts[:config], params: params)
end

@doc """
Expand All @@ -348,7 +348,7 @@ defmodule Pinecone do
success_type(String.t()) | error_type()
@spec delete_all_vectors(index :: index_type(), opts :: keyword()) ::
success_type(String.t()) | error_type()
def delete_all_vectors(%Index{name: name, project_name: project_name}, opts \\ []) do
def delete_all_vectors(%Index{name: name}, opts \\ []) do
opts = Keyword.validate!(opts, [:config, :namespace, :filter])

body =
Expand All @@ -358,7 +358,31 @@ defmodule Pinecone do

body = if opts[:namespace], do: Map.put(body, "namespace", opts[:namespace]), else: body

post({:vectors, "#{name}-#{project_name}"}, "vectors/delete", body, opts[:config])
post_vector("delete", name, body, opts[:config])
end

defp delete_vector(path, name, config, opts) do
with {:ok, host} <- index_host(name) do
delete({:vectors, host}, path, config, opts)
end
end

defp get_vector(path, name, config, opts) do
with {:ok, host} <- index_host(name) do
get({:vectors, host}, path, config, opts)
end
end

defp post_vector(path, name, body, opts) do
with {:ok, host} <- index_host(name) do
post({:vectors, host}, path, body, opts)
end
end

defp index_host(index_name) do
with {:ok, %{"host" => host}} <- describe_index(index_name) do
{:ok, host}
end
end

@doc """
Expand Down Expand Up @@ -387,7 +411,7 @@ defmodule Pinecone do
success_type(map()) | error_type()
@spec query(index :: index_type(), vector :: list(), opts :: keyword()) ::
success_type(map()) | error_type()
def query(%Index{name: name, project_name: project_name}, vector, opts \\ []) do
def query(%Index{name: name}, vector, opts \\ []) do
opts =
Keyword.validate!(opts, [
:config,
Expand All @@ -413,7 +437,7 @@ defmodule Pinecone do

body = if opts[:namespace], do: Map.put(body, "namespace", opts[:namespace]), else: body

post({:vectors, "#{name}-#{project_name}"}, "query", body, opts[:config])
post_vector("query", name, body, opts[:config])
end

## Collection Operations
Expand Down
10 changes: 7 additions & 3 deletions lib/pinecone/http.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule Pinecone.Http do
@moduledoc false

require Logger

def get(type, endpoint, config \\ [], opts \\ []) do
params = opts[:params] || []

Expand Down Expand Up @@ -37,7 +39,9 @@ defmodule Pinecone.Http do
{:ok, body}
end

defp parse_response({:ok, %{body: body}}) do
defp parse_response({:ok, %{body: body} = e}) do
Logger.warning("Failed pinecone request: #{inspect(e)}")

{:error, body}
end

Expand All @@ -62,8 +66,8 @@ defmodule Pinecone.Http do
Path.join("https://api.pinecone.io/collections", endpoint)
end

defp url({:vectors, slug}, endpoint, env) do
Path.join("https://#{slug}.svc.#{env}.pinecone.io", endpoint)
defp url({:vectors, host}, endpoint, _env) do
Path.join("https://#{host}/vectors", endpoint)
end

defp headers(api_key) do
Expand Down
Loading