如果您在colab上打开这个笔记本,您可能需要安装LlamaIndex 🦙。
%pip install llama-index-embeddings-openai
!pip install llama-index
import os
os.environ["OPENAI_API_KEY"] = "sk-..."
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.core import Settings
embed_model = OpenAIEmbedding(embed_batch_size=10)
Settings.embed_model = embed_model
text-embedding-3-large
和text-embedding-3-small
¶注意,你可能需要更新你的OpenAI客户端:pip install -U openai
# 获取API密钥并创建嵌入
from llama_index.embeddings.openai import OpenAIEmbedding
embed_model = OpenAIEmbedding(model="text-embedding-3-large")
embeddings = embed_model.get_text_embedding(
"Open AI new Embeddings models is great."
)
print(embeddings[:5])
[-0.011500772088766098, 0.02457442320883274, -0.01760469563305378, -0.017763426527380943, 0.029841400682926178]
print(len(embeddings))
3072
# 获取API密钥并创建嵌入
from llama_index.embeddings.openai import OpenAIEmbedding
embed_model = OpenAIEmbedding(
model="text-embedding-3-small",
)
embeddings = embed_model.get_text_embedding(
"Open AI new Embeddings models is awesome."
)
print(len(embeddings))
1536
注意:确保你安装了最新的OpenAI客户端。
# 获取API密钥并创建嵌入
from llama_index.embeddings.openai import OpenAIEmbedding
embed_model = OpenAIEmbedding(
model="text-embedding-3-large",
dimensions=512,
)
embeddings = embed_model.get_text_embedding(
"Open AI new Embeddings models with different dimensions is awesome."
)
print(len(embeddings))
512