Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract the Vector from an OpenAI Embeddings Call?

I use nearly the same code as here in this Git Repo to get Embeddings from OpenAI:
https://gist.github.com/limcheekin/997de2ae0757cd46db796f162c3dd58c

oai = OpenAI(
# This is the default and can be omitted
api_key="sk-.....",
)

def get_embedding(text_to_embed, openai):
   
    response = openai.embeddings.create(
        model= "text-embedding-ada-002",
        input=[text_to_embed]
    )
    
    return response

embedding_raw = get_embedding(text,oai)

According to the Git Repo the Vector should be in response['data'][0]['embedding']. But it isn’t in my case.

When I print the response Variable, I got this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

print(embedding_raw)

Output:

CreateEmbeddingResponse(data=[Embedding(embedding=[0.009792150929570198, -0.01779201813042164, 0.011846082285046577, -0.0036859565880149603, -0.0013213189085945487, 0.00037509595858864486,..... -0.0121011883020401, -0.015751168131828308], index=0, object='embedding')], model='text-embedding-ada-002', object='list', usage=Usage(prompt_tokens=360, total_tokens=360))

Sorry, I’m new to python, but how can I access the vector data?

>Solution :

Simply return just the embedding vector as follows:

def get_embedding(text_to_embed, openai):
   
    response = openai.embeddings.create(
        model= "text-embedding-ada-002",
        input=[text_to_embed]
    )
    
    return response.data[0].embedding # Change this

embedding_raw = get_embedding(text,oai)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading