Prem AI

PremAI is a unified generative AI development platform for fine-tuning deploying, and monitoring AI models.

Qdrant is compatible with PremAI APIs.

Installing the SDKs

pip install premai qdrant-client

To install the npm package:

npm install @premai/prem-sdk @qdrant/js-client-rest

Import all required packages

from premai import Prem

from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams
import Prem from '@premai/prem-sdk';
import { QdrantClient } from '@qdrant/js-client-rest';

Define all the constants

We need to define the project ID and the embedding model to use. You can learn more about obtaining these in the PremAI docs.

PROJECT_ID = 123
EMBEDDING_MODEL = "text-embedding-3-large"
COLLECTION_NAME = "prem-collection-py"
QDRANT_SERVER_URL = "http://localhost:6333"
DOCUMENTS = [
    "This is a sample python document",
    "We will be using qdrant and premai python sdk"
]
const PROJECT_ID = 123;
const EMBEDDING_MODEL = "text-embedding-3-large";
const COLLECTION_NAME = "prem-collection-js";
const SERVER_URL = "http://localhost:6333"
const DOCUMENTS = [
    "This is a sample javascript document",
    "We will be using qdrant and premai javascript sdk"
];

Set up PremAI and Qdrant clients

prem_client = Prem(api_key="xxxx-xxx-xxx")
qdrant_client = QdrantClient(url=QDRANT_SERVER_URL)
const premaiClient = new Prem({
    apiKey: "xxxx-xxx-xxx"
})
const qdrantClient = new QdrantClient({ url: SERVER_URL });

Generating Embeddings

from typing import Union, List

def get_embeddings(
    project_id: int, 
    embedding_model: str, 
    documents: Union[str, List[str]]
) -> List[List[float]]:
    """
    Helper function to get the embeddings from premai sdk 
    Args
        project_id (int): The project id from prem saas platform.
        embedding_model (str): The embedding model alias to choose
        documents (Union[str, List[str]]): Single texts or list of texts to embed
    Returns:
        List[List[int]]: A list of list of integers that represents different
            embeddings
    """
    embeddings = []
    documents = [documents] if isinstance(documents, str) else documents 
    for embedding in prem_client.embeddings.create(
        project_id=project_id,
        model=embedding_model, 
        input=documents
    ).data:
        embeddings.append(embedding.embedding)
    
    return embeddings
async function getEmbeddings(projectID, embeddingModel, documents) {
    const response = await premaiClient.embeddings.create({
        project_id: projectID,
        model: embeddingModel,
        input: documents
    });
    return response;
}

Converting Embeddings to Qdrant Points

from qdrant_client.models import PointStruct

embeddings = get_embeddings(
    project_id=PROJECT_ID,
    embedding_model=EMBEDDING_MODEL, 
    documents=DOCUMENTS 
)

points = [
    PointStruct(
        id=idx, 
        vector=embedding,
        payload={"text": text},
    ) for idx, (embedding, text) in enumerate(zip(embeddings, DOCUMENTS))
]
function convertToQdrantPoints(embeddings, texts) {
    return embeddings.data.map((data, i) => {
        return {
            id: i,
            vector: data.embedding,
            payload: {
                text: texts[i]
            }
        };
    });
}

const embeddings = await getEmbeddings(PROJECT_ID, EMBEDDING_MODEL, DOCUMENTS);
const points = convertToQdrantPoints(embeddings, DOCUMENTS);

Set up a Qdrant Collection

qdrant_client.create_collection(
    collection_name=COLLECTION_NAME, 
    vectors_config=VectorParams(size=3072, distance=Distance.DOT)
)
await qdrantClient.createCollection(COLLECTION_NAME, {
    vectors: {
        size: 3072,
        distance: 'Cosine'
    }
})

Insert Documents into the Collection

doc_ids = list(range(len(embeddings)))

qdrant_client.upsert(
    collection_name=COLLECTION_NAME, 
    points=points
 )
await qdrantClient.upsert(COLLECTION_NAME, {
        wait: true,
        points
    });
query = "what is the extension of python document"

query_embedding = get_embeddings(
    project_id=PROJECT_ID, 
    embedding_model=EMBEDDING_MODEL, 
    documents=query
)

qdrant_client.search(collection_name=COLLECTION_NAME, query_vector=query_embedding[0])
const query = "what is the extension of javascript document"
const query_embedding_response = await getEmbeddings(PROJECT_ID, EMBEDDING_MODEL, query)

await qdrantClient.search(COLLECTION_NAME, {
    vector: query_embedding_response.data[0].embedding
});
Was this page useful?

Thank you for your feedback! 🙏

We are sorry to hear that. 😔 You can edit this page on GitHub, or create a GitHub issue.