Skip to content

Commit

Permalink
fix: add updates for new api routes for vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaAK committed Jan 28, 2024
1 parent f456c17 commit 0559e05
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
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

0 comments on commit 0559e05

Please sign in to comment.