File size: 712 Bytes
35a631f
e34bef0
35a631f
 
 
 
 
 
 
e34bef0
35a631f
e34bef0
 
35a631f
 
e34bef0
 
35a631f
 
e34bef0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from openai import OpenAI
from pinecone import Pinecone, ServerlessSpec

def get_embedding(text: str, openai_api_key: str, model="text-embedding-3-small"):
    client = OpenAI(api_key=openai_api_key)
    text = text.replace("\n", " ")
    return client.embeddings.create(input=[text], model=model).data[0].embedding

def init_pinecone(pinecone_api_key: str, index_name: str, dimension=1536):
    pc = Pinecone(api_key=pinecone_api_key)

    if index_name not in pc.list_indexes().names():
        pc.create_index(
            name=index_name,
            dimension=dimension,
            metric="cosine",
            spec=ServerlessSpec(cloud="aws", region="us-east-1")
        )

    return pc.Index(index_name)