Skip to content

Generate vector embeddings

View as Markdown

Generate a vector embedding with the GenAI API’s embedding model, then store it in a TIR Vector Database so you can search it for similar text later.

You need:

Install the TIR SDK, which calls the embedding model, and the Qdrant Python client, which talks to your vector database:

pip install e2enetworks qdrant-client

The embedding model’s API name uses underscores where its display name uses hyphens. Call it through the TIR SDK and read the vector from the response:

from e2enetworks.cloud import tir
tir.init(api_key="<your-api-key>", access_token="<your-auth-token>")
tir_client = tir.ModelAPIClient(project="<your-project-id>")
text = "<the text you want to embed>"
response = tir_client.infer(
model_name="e5-mistral-7b-instruct",
data={"prompt": text},
)
embedding = response.outputs[0].data

<your-auth-token> is the Auth Token from your Personal Access Token. Copy it from the Create Token dialog or, later, from the Personal Access Tokens list. <your-api-key> is that same token’s API Key. To get it, use the Download Config File action next to the token in the list; the file holds both values. <your-project-id> is the project the token belongs to.

Import the Qdrant client and the types for creating a collection:

from qdrant_client import QdrantClient
from qdrant_client.http.models import Distance, VectorParams

Connect with the host and API key from your Vector Database’s Overview tab. Use port 6333 for a REST connection:

client = QdrantClient(
host="<your-qdrant-host>",
port=6333,
api_key="<your-vector-database-api-key>",
)

To connect over gRPC instead, use port 6334 and set the client to prefer gRPC:

client = QdrantClient(
host="<your-qdrant-host>",
port=6334,
api_key="<your-vector-database-api-key>",
prefer_grpc=True,
)

A collection holds the vectors you add to it. Set size to the length of the embedding you generated. Choose the distance metric you want Qdrant to use when it compares vectors, such as dot product:

client.create_collection(
collection_name="example_collection",
vectors_config=VectorParams(size=len(embedding), distance=Distance.DOT),
shard_number=3,
replication_factor=3,
)

The shard number splits the collection’s data into that many shards, and the replication factor keeps that many copies of each shard. For more on sharding and replication, see Qdrant’s own Concepts documentation.

Add the embedding to the collection with a unique ID and the text it came from, so you can read the text back later:

from qdrant_client.http.models import PointStruct
client.upsert(
collection_name="example_collection",
points=[
PointStruct(
id=1,
vector=embedding,
payload={"data": text},
),
],
wait=True,
)

A successful call returns a result whose status is completed. An error raises an exception instead.

Read the collection’s details, including how many vectors it holds:

collection_info = client.get_collection("example_collection")
print(collection_info)

The response shows a vector count of 1 after you add your first vector.

To search the collection and pass the results to a language model, see Build a RAG pipeline. To call the embedding model with a different method, see Call the API with the TIR SDK.