시맨틱 검색을 위한 벡터 업서트
이제 텍스트 데이터를 임베딩하고, 벡터와 메타데이터를 'pinecone-datacamp' 인덱스에 업서트해 보세요! squad_dataset.csv라는 데이터셋이 제공되며, 그중 200개 샘플이 DataFrame df로 로드되어 있어요.
이 연습에서는 OpenAI API의 임베딩 모델을 사용하기 위해 별도로 API 키를 생성하거나 사용할 필요가 없습니다. 유효한 OpenAI 클라이언트가 이미 생성되어 client 변수에 할당되어 있어요.
여러분의 과제는 OpenAI API로 텍스트를 임베딩한 뒤, 임베딩과 메타데이터를 squad_dataset 네임스페이스 아래 Pinecone 인덱스에 업서트하는 것입니다.
이 연습은 강의의 일부입니다
Pinecone로 AI 애플리케이션 구축하기
연습 안내
- Pinecone 클라이언트를 본인의 API 키로 초기화하세요(OpenAI 클라이언트는 이미
client로 제공됩니다). - 배치의 각
row에서'id','text','title'메타데이터를 추출하세요. - OpenAI의
'text-embedding-3-small'을 사용해 차원1536으로texts를 인코딩하세요. - 벡터와 메타데이터를
'squad_dataset'네임스페이스에 업서트하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Initialize the Pinecone client
pc = Pinecone(api_key="____")
index = pc.Index('pinecone-datacamp')
batch_limit = 100
for batch in np.array_split(df, len(df) / batch_limit):
# Extract the metadata from each row
metadatas = [{
"text_id": row['____'],
"text": row['____'],
"title": row['____']} for _, row in batch.iterrows()]
texts = batch['text'].tolist()
ids = [str(uuid4()) for _ in range(len(texts))]
# Encode texts using OpenAI
response = ____(input=____, model="____")
embeds = [np.array(x.embedding) for x in response.data]
# Upsert vectors to the correct namespace
____(vectors=____(ids, embeds, metadatas), namespace=____)