Get embeddings
Get embeddings¶
The function get_embedding
will give us an embedding for an input text.
In [1]:
Copied!
import openai
embedding = openai.Embedding.create(
input="Your text goes here", model="text-embedding-ada-002"
)["data"][0]["embedding"]
len(embedding)
import openai
embedding = openai.Embedding.create(
input="Your text goes here", model="text-embedding-ada-002"
)["data"][0]["embedding"]
len(embedding)
Out[1]:
1536
In [2]:
Copied!
import openai
from tenacity import retry, wait_random_exponential, stop_after_attempt
@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))
def get_embedding(text: str, model="text-embedding-ada-002") -> list[float]:
return openai.Embedding.create(input=[text], model=model)["data"][0]["embedding"]
embedding = get_embedding("Your text goes here", model="text-embedding-ada-002")
print(len(embedding))
import openai
from tenacity import retry, wait_random_exponential, stop_after_attempt
@retry(wait=wait_random_exponential(min=1, max=20), stop=stop_after_attempt(6))
def get_embedding(text: str, model="text-embedding-ada-002") -> list[float]:
return openai.Embedding.create(input=[text], model=model)["data"][0]["embedding"]
embedding = get_embedding("Your text goes here", model="text-embedding-ada-002")
print(len(embedding))
1536