Code Monkey home page Code Monkey logo

pinecone-client's Introduction

Unofficial Pinecone.io Client

Build Status npm version

An unofficial fetch based client for the Pinecone.io vector database with excellent TypeScript support.

Pinecone recently released a similar client. It's a great option if you aren't picky about fully typed metadata.

Highlights

  • Support for all vector operation endpoints
  • Fully typed metadata with TypeScript generics
  • Automatically remove null metadata values (Pinecone doesn't nulls)
  • Supports modern fetch based runtimes (Cloudlflare workers, Deno, etc)
  • In-editor documentation with IntelliSense/TS server
  • Tiny package size. Less than 5kb gzipped
  • Full e2e test coverage

Example Usage

import { PineconeClient } from 'pinecone-client';

// Specify the type of your metadata
type Metadata = { size: number, tags?: string[] | null };

// Instantiate a client
const pinecone = new PineconeClient<Metadata>({ namespace: 'test' });

// Upsert vectors with metadata.
await pinecone.upsert({
  vectors: [
    { id: '1', values: [1, 2, 3], metadata: { size: 3, tags: ['a', 'b', 'c'] } },
    { id: '2', values: [4, 5, 6], metadata: { size: 10, tags: null } },
  ],
});

// Query vectors with metadata filters.
const { matches } = await pinecone.query({
  topK: 2,
  id: '2',
  filter: { size: { $lt: 20 } },
  includeMetadata: true,
});

// typeof matches = {
//   id: string;
//   score: number;
//   metadata: Metadata;
// }[];

Install

Warning: This package is native ESM and no longer provides a CommonJS export. If your project uses CommonJS, you will have to convert to ESM or use the dynamic import() function. Please don't open issues for questions regarding CommonJS / ESM.

Runtimes

  • Supported: Deno, Node v18+, Cloudflare Workers, browsers
  • Unsupported: Anything without a native fetch implementation (Node<v17)
npm install pinecone-client
import { PineconeClient } from 'pinecone-client';

const pinecone = new PineconeClient({ /* ... */ });

Setup

Once installed, you need to create an instance of the PineconeClient class to make API calls.

import { PineconeClient } from 'pinecone-client';

// A type representing your metadata
type Metadata = {};

const pinecone = new PineconeClient<Metadata>({
  apiKey: '<your api key>',
  baseUrl: '<your index url>',
  namespace: 'testing',
});

Both apiKey and baseUrl are optional and will be read from the following environment variables:

  • process.env.PINECONE_API_KEY
  • process.env.PINECONE_BASE_URL

API

The client supports all of the vector operations from the Pinecone API using the same method names and parameters. It also supports creating and deleting indexes.

For detailed documentation with links to the Pinecone docs, see the source code.

Supported methods:

  • pinecone.delete()
  • pinecone.describeIndexStats()
  • pinecone.fetch()
  • pinecone.query()
  • pinecone.update()
  • pinecone.upsert()
  • pinecone.createIndex()
  • pinecone.deleteIndex()

You can also find more example usage in the e2e tests.

pinecone-client's People

Contributors

drob avatar okomarov avatar rileytomasek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

pinecone-client's Issues

Upsert Error code

Everytime I try to upsert I get the following error :
[TypeError: Cannot construct a Request with a Request object that has already been used.]

Environnement

"next": "13.2.4"
"typescript": "4.9.5"
"pinecone-client": "^1.1.0"

$ node -v
v18.12.1

Source code

import { PineconeClient } from "pinecone-client";

export type Metadata = { userId: string; text: string;};

export const pinecone = new PineconeClient<Metadata>({
  apiKey: process.env.PINECONE_API_KEY,
  baseUrl: process.env.PINECONE_BASE_URL,
  namespace: process.env.PINECONE_INDEX
})
await pinecone.upsert({
    vectors: [
    {
      id,
      values: vectorEmbedding,
      metadata: { userId: user.id, text },
    },
  ],
})

vectorEmbedding is a 1536 dimension vecteur of type number[]
(Returned from openAI embeddings)

upsert previously worked, has stopped working?

Upsert function previously worked, but has now stopped working. query still works. (same env variables, nothing else changed on my end)

Failed reading HTTPError response body SyntaxError: Unexpected token : in JSON at position 0
at JSON.parse ()
at Response.json (node:internal/deps/undici/undici:6160:23)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async distribution_default.extend.hooks.beforeError (C:~\node_modules\pinecone-client\dist\index.cjs.js:534:28)
at async fn (C:~\node_modules\pinecone-client\dist\index.cjs.js:255:19)
at async result. [as json] (C:~\node_modules\pinecone-client\dist\index.cjs.js:275:31)
at async PineconeClient.upsert (C:~\node_modules\pinecone-client\dist\index.cjs.js:716:7)
at async indexVectorEmbeddings (C:~\functions\semantic-functions\index-vector-embeddings.js:9:14)
at async C:~\functions\semantic-functions\embed-vectors.js:24:25
Something went wrong: HTTPError: Request failed with status code 400 Bad Request

Ky is incompatible with Edge Runtime

Hello @rileytomasek!

As you already know (since I happened to stumble across you dealing with this same issue here: dexaai/openai-fetch#4)

The edge runtime is incompatible with ky right now for some strange reason.
It looks like there is a temporary workaround that could be used: vercel/next.js#41531 (comment)

Is this something we could integrate with the fetch-api as an interim? It's actually a huge blocker on my current project right now.

By the way, your work on this client has been amazing. It's quite frankly in much better shape than the official client which I've had to switch over from.

Increase Ky request timeout

The default is too short and sometimes gets hit when it shouldn't in cases where Pinecone is just slow to respond.

Typescript: Interface 'FetchOptions' incorrectly extends interface 'Options'

Typescript v5.2.2 build fails with pinecone-client v1.1.1:

Interface 'FetchOptions' incorrectly extends interface 'Options'.
  Types of property 'credentials' are incompatible.
    Type 'string | undefined' is not assignable to type 'RequestCredentials | undefined'.
      Type 'string' is not assignable to type 'RequestCredentials | undefined'.ts(2430)

calling new PineconeClient({}) throws exception

My code:
const pinecone = new PineconeClient({ baseUrl: config.PINECONE_BASE_URL, apiKey: config.PINECONE_API_KEY, });

Exception:
Screenshot 2023-07-11 at 9 52 16 PM

The base URL I am using does appear to be correct based on the documentation
https://v2-84de346.svc.us-west4-gcp-free.pinecone.io

Environment: MacOS running Node v16.17.0

What I've tried:
I've tried following the codepath through the pinecone client, and its a bit further upstream. It all blows up in this KY.extend call

Using with node v19 results in a ky TypeError

See upstream: sindresorhus/ky#467 (TypeError: Cannot set property duplex of # which has only a getter)

This happens when trying to call .upsert (possibly other API methods as well, but this is the one I ran into).

Downgrading to Node.js v18 solves the error. I don't think there's anything you can do about this since this is an issue with ky itself, but just opening an issue here in case other people run into it as well.

Thanks for the awesome lib! ๐Ÿ™

CommonJS Node

Hello
great work with this client.

I use CommonJS in Nodejs though, so I get:

uncaughtException: Cannot use import statement outside a module
import { PineconeClient } from 'pinecone-client'

is there any chance it could support hybrid-module syntax, not only ESModules ?

cheers

Urgent: ERRor pinecone PineconeError: Method Not Allowed

Hello sir.
I'm building chatbot using pinecone-client modal. My project is nextjs

.env file
PINECONE_API_KEY="............"
PINECONE_BASE_URL="https://controller.us-west4-gcp.pinecone.io/databases"

pinecone.ts file

export const pinecone = new PineconeClient({
apiKey: process.env.PINECONE_API_KEY,
baseUrl: process.env.PINECONE_BASE_URL,
namespace:"semantic-search-index"
});

openapi.tsx file

import { pinecone } from "./pinecone";
await pinecone.upsert({
vectors: [
{
id,
values: vectorEmbedding,
metadata: { id, text:req.body.prompt, title:req.body.title },
},
],
});

But when I call upsert function, I faced this error:
ERRor pinecone PineconeError: Method Not Allowed
[cause]: HTTPError: Request failed with status code 405 Method Not Allowed

Uninformative error when a fetch polyfill is missing.

I ran into a TypeError: globalThis.Headers is not a constructor when attempting to import { PineconeClient } from 'pinecone-client'. This was ultimately due to lack of a fetch polyfill. (Fixed by adding import 'cross-fetch/polyfill'; before the pinecone-client import, in case anyone else is running into this.)

Would it be possible to emit a more useful error when the fetch polyfill is missing? Another option might be to use something like https://github.com/sindresorhus/ky-universal.

Relevant parts of the stacktrace:

./node_modules/pinecone-client/node_modules/ky/source/core/constants.ts:10:20
node_modules/pinecone-client/dist/index.cjs.js (./node_modules/pinecone-client/node_modules/ky/source/core/constants.ts:22:1)
__require (./.netlify/functions-serve/server/src/express/server.js:37:50)
Object.<anonymous> (./lib/pinecone.ts:1:32)
Module._compile (node:internal/modules/cjs/loader:1165:14)
Object.Module._extensions..js (node:internal/modules/cjs/loader:1219:10)
Module.load (node:internal/modules/cjs/loader:1043:32)
Function.Module._load (node:internal/modules/cjs/loader:878:12)
Module.require (node:internal/modules/cjs/loader:1067:19)
require (node:internal/modules/cjs/helpers:103:18)

Node v18.12.1.

Suggestion: Upsert call should return the count of vectors inserted

Currently, the Upsert call handles batching (which is ๐Ÿ‘Œ ), but I noticed that right now the function doesn't return anything.

It may be useful to modify this function to be consistent with the Upsert Pinecone API call which returns an object that has:

upsertedCount as a number when successful.

To support batch mode it could return a response that has the sum of all the upsertedCounts from each individual batch's response.

  async upsert(params: {
    vectors: SetRequired<Vector<Metadata>, 'metadata'>[];
    batchSize?: number;
  }): Promise<void> {
    // Don't upsert more than `params.batchSize` vectors in a single request
    const batchSize = params.batchSize || 50;
    for (let i = 0; i < params.vectors.length; i += batchSize) {
      const vectors = params.vectors.slice(i, i + batchSize);
      const vectorsWithoutMetadataNulls = vectors.map(removeNullValues);
      await this.api
        .post('vectors/upsert', {
          json: {
            namespace: this.namespace,
            vectors: vectorsWithoutMetadataNulls,
          },
        })
        .json();
    }
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.