How to use BGE embeddings
Quick answer
Use the
BGE embeddings model via the OpenAI API by calling client.embeddings.create with model="bge-small" or "bge-base". This returns a vector representation of input text useful for semantic search, clustering, or similarity tasks.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the openai Python package and set your OpenAI API key as an environment variable for authentication.
pip install openai>=1.0 Step by step
Use the OpenAI Python SDK to generate BGE embeddings for your text input. The example below shows how to create embeddings with the bge-small model and print the resulting vector length.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = client.embeddings.create(
model="bge-small",
input="OpenAI's BGE embeddings provide powerful text representations."
)
embedding_vector = response.data[0].embedding
print(f"Embedding vector length: {len(embedding_vector)}") output
Embedding vector length: 768
Common variations
- Use
bge-basefor higher-dimensional embeddings with potentially better accuracy. - Pass a list of strings to
inputto batch embed multiple texts in one request. - Use async calls with
asyncioand the OpenAI async client for concurrency.
import os
import asyncio
from openai import OpenAI
async def async_embed():
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
response = await client.embeddings.acreate(
model="bge-base",
input=["First text", "Second text"]
)
for i, data in enumerate(response.data):
print(f"Embedding {i} length: {len(data.embedding)}")
asyncio.run(async_embed()) output
Embedding 0 length: 1024 Embedding 1 length: 1024
Troubleshooting
- If you get an authentication error, verify your
OPENAI_API_KEYenvironment variable is set correctly. - For rate limit errors, implement exponential backoff retries.
- If embeddings seem off, check you are using the correct
modelname likebge-smallorbge-base.
Key Takeaways
- Use
bge-smallorbge-basemodels with OpenAI's embeddings API for BGE embeddings. - Batch multiple texts in one request by passing a list to
inputfor efficiency. - Async API calls improve throughput when embedding many texts concurrently.
- Always set your API key in
OPENAI_API_KEYenvironment variable to avoid auth errors. - Check model names carefully to avoid unexpected results or errors.