--- title: "Generate vector embeddings" description: "Generate a vector embedding with the GenAI API's embedding model and store it in a TIR Vector Database for similarity search." content_type: "how-to" product: "genai-api" platform: "TIR" url: "/tir/genai-api/how-to/generate-vector-embeddings/" topic: ["guides"] --- # Generate vector embeddings 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. ## Before you begin You need: - A Personal Access Token. See [Get started with TIR GenAI API](/tir/genai-api/get-started/). - A Qdrant database created in your project with TIR Vector Database, and its host and API key from the database's **Overview** tab. See [TIR Vector Database](/tir/vector-database/). ## Install the packages Install the TIR SDK, which calls the embedding model, and the Qdrant Python client, which talks to your vector database: ```bash pip install e2enetworks qdrant-client ``` ## Generate the embedding 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: ```python from e2enetworks.cloud import tir tir.init(api_key="", access_token="") tir_client = tir.ModelAPIClient(project="") text = "" response = tir_client.infer( model_name="e5-mistral-7b-instruct", data={"prompt": text}, ) embedding = response.outputs[0].data ``` `` is the Auth Token from your Personal Access Token. Copy it from the Create Token dialog or, later, from the **Personal Access Tokens** list. `` 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. `` is the project the token belongs to. ## Connect to your Qdrant database Import the Qdrant client and the types for creating a collection: ```python 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: ```python client = QdrantClient( host="", port=6333, api_key="", ) ``` To connect over gRPC instead, use port 6334 and set the client to prefer gRPC: ```python client = QdrantClient( host="", port=6334, api_key="", prefer_grpc=True, ) ``` ## Create a collection 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: ```python 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](https://qdrant.tech/documentation/concepts/). ## Add the vector Add the embedding to the collection with a unique ID and the text it came from, so you can read the text back later: ```python 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. ## Check the vector count Read the collection's details, including how many vectors it holds: ```python 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](/tir/genai-api/how-to/build-a-rag-pipeline/). To call the embedding model with a different method, see [Call the API with the TIR SDK](/tir/genai-api/how-to/call-the-api-with-the-tir-sdk/).