Pinecone 간단 정리

Jeong-Minju·2024년 1월 29일
0

pinecone

1. 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,
  )
)

https://docs.pinecone.io/docs/manage-indexes#create-a-serverless-index

metadata 적재 방법

  • index 생성 시 metadata를 기입하지 않을 경우, 추후 검색에 사용될 수 있는 metadatafilter 인자는 모든 값이 됩니다.
  • 만약 검색에 사용될 metadata를 제한하고 싶을 경우 index 생성 시 metadata_config 인자에 기입을 해 주어야 합니다.
    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"]
        }
      )
    )

index 추가정보

  1. starter plan index의 경우 pinecone의 모든 기능을 활용할 수 없습니다.

    • 제일 큰 문제는 metadata기반 검색/삭제 등을 할 수 없다는 점입니다.

    https://docs.pinecone.io/docs/limits#starter-index-limitations

2. 데이터 적재(upsert)

  • index.upsert(…) 함수를 통해 데이터를 index 내에 적재할 수 있습니다. 이때, “values”키에 해당되는 값은 vector값입니다.
    • 인덱스 생성 예시에서는 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

metadata 포함 적재법

  • metadata에 들어갈 수 있는 타입 목록입니다.
    1. String

    2. Number(integer or floating point, gets converted to a 64 bit floating point)

    3. Booleans

    4. List of String

      https://docs.pinecone.io/docs/metadata-filtering#supported-metadata-types

    • 만약 metadata에 날자 데이터를 넣어줄 경우, metadata에서 datetime 타입을 지원하지 않으므로 현재(24.01.29) 기준 unix timestamp로 변환해 넣어주는 것을 추천드립니다.
  • metadata를 포함해 적재를 하는 예제입니다.
    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-metadata

3. 검색(query)

  • index.query(…) 함수를 통해 index 내에 있는 데이터를 검색할 수 있습니다.
  • pinecone의 query는 질문 벡터 - index 적재 벡터간의 유사도 비교를 수행 해 상위 데이터를 가져오는 형식으로 진행됩니다.
    from 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-query

metadata filter활용 query

profile
RAG를 좋아하는 사람입니다 :)

0개의 댓글