Pinecone

Pinecone is a hosted vector database. It does not call the exchange. Your code gets the vectors from the exchange and writes them to a Pinecone index.

Before you start

1. Export the keys

export LIQUID_API_KEY=sk-liquid-...
export PINECONE_API_KEY=...

2. Install the packages

# Python 3.9 or later
pip install openai pinecone

Create the index with the vector length of the model. text-embedding-3-small returns 1,536 numbers. Do not use a Pinecone integrated model: that index embeds the text itself and does not take your vectors.

import os

from openai import OpenAI
from pinecone import Pinecone, ServerlessSpec

MODEL = "text-embedding-3-small"
openai = OpenAI(
    base_url="https://router.inference.ai.exchange/v1",
    api_key=os.environ["LIQUID_API_KEY"],
)
pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])

def embed(texts):
    result = openai.embeddings.create(model=MODEL, input=texts)
    return [item.embedding for item in result.data]

if not pc.has_index("documents"):
    pc.create_index(
        name="documents",
        dimension=1536,
        metric="cosine",
        spec=ServerlessSpec(cloud="aws", region="us-east-1"),
    )
index = pc.Index("documents")

texts = ["a cat on a bicycle", "a dog in a car"]
index.upsert(
    vectors=[
        {"id": f"doc-{i}", "values": vector, "metadata": {"body": text}}
        for i, (text, vector) in enumerate(zip(texts, embed(texts)))
    ]
)

result = index.query(
    vector=embed(["an animal on two wheels"])[0],
    top_k=3,
    include_metadata=True,
)
for match in result.matches:
    print(f"{match.score:.3f}  {match.metadata['body']}")

A new write can take a few seconds to appear in a query. Use the same model for the stored texts and for the query.

Notes

Troubleshooting

Symptom Cause and fix
Vector dimension 1024 does not match The index length differs from the model. Create the index with the model's vector length.
401 from the exchange The key is wrong, revoked or expired. Export it again.
model_not_found The model is not on the book. Use an exact name from GET /v1/models.
A request is refused for lack of funds. The workspace balance cannot cover the cap. Add funds on the Credits page.

See Errors for the error codes.

Next steps