from pinecone import Pinecone
pc = Pinecone(api_key="YOUR_API_KEY")
pc.create_index(
name="example-index",
dimension=1536,
metric="cosine",
spec=PodSpec(
environment="eastus-azure",
pod_type="p1.x1",
pods=1,
)
)
https://docs.pinecone.io/docs/manage-indexes#create-a-serverless-index
from pinecone import Pinecone
pc = Pinecone(api_key="YOUR_API_KEY")
pc.create_index(
name="example-index",
dimension=1536,
metric="cosine",
spec=PodSpec(
environment="eastus-azure",
pod_type="p1.x1",
pods=1,
metadata_config = {
"indexed": ["genre", "year"]
}
)
)
starter plan index의 경우 pinecone의 모든 기능을 활용할 수 없습니다.
https://docs.pinecone.io/docs/limits#starter-index-limitations
인덱스 생성 예시에서는 openai embedding 모델의 output size인 1536을 사용했지만 해당 예시에서는 소량 벡터로 기입했습니다(실 사용 시에는 인덱스 생성 시 넣어준 크기와 동일하게 벡터를 생성해야 합니다).
from pinecone import Pinecone
pc = Pinecone(api_key="YOUR_API_KEY")
index = pc.Index("example-index")
index.upsert(
vectors=[
{"id": "A", "values": [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]},
{"id": "B", "values": [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2]},
{"id": "C", "values": [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3]},
{"id": "D", "values": [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4]}
]
)
https://docs.pinecone.io/docs/upsert-data#upsert-records-into-the-default-namespace
String
Number(integer or floating point, gets converted to a 64 bit floating point)
Booleans
List of String
https://docs.pinecone.io/docs/metadata-filtering#supported-metadata-types
from pinecone import Pinecone
pc = Pinecone(api_key="YOUR_API_KEY")
index = pc.Index("example-index")
index.upsert(
vectors=[
{
"id": "A",
"values": [0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
"metadata": {"genre": "comedy", "year": 2020}
},
{
"id": "B",
"values": [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2],
"metadata": {"genre": "documentary", "year": 2019}
},
{
"id": "C",
"values": [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3],
"metadata": {"genre": "comedy", "year": 2019}
},
{
"id": "D",
"values": [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4],
"metadata": {"genre": "drama"}
}
]
)
위 index 생성 예제에서 metadata_config 내에 “genre”, “year”을 넣어주었으므로 적재 시에도 해당되는 key를 넣어주는 것을 확인할 수 있습니다. https://docs.pinecone.io/docs/upsert-data#upsert-records-with-metadatafrom pinecone import Pinecone
pc = Pinecone(api_key="YOUR_API_KEY")
index = pc.Index("example-index")
index.query(
vector=[0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3],
top_k=3,
include_values=True
)
# Returns:
# {
# "matches": [
# {
# "id": "C",
# "score": -1.76717265e-07,
# "values": [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3],
# },
# {
# "id": "B",
# "score": 0.080000028,
# "values": [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2],
# },
# {
# "id": "D",
# "score": 0.0800001323,
# "values": [0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4],
# },
# ],
# "namespace": "",
# "usage": {"readUnits": 5}
# }
https://docs.pinecone.io/docs/query-data#sending-a-queryfrom pinecone import Pinecone
pc = Pinecone(api_key="YOUR_API_KEY")
index = pc.Index("pinecone-index")
index.query(
vector=[0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
filter={
"genre": {"$eq": "documentary"},
"year": 2019
},
top_k=1,
include_metadata=True
)