Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📚 Documentation: Request for RAG based use case documentation #1554

Open
2 tasks done
SadmanYasar opened this issue Dec 12, 2024 · 1 comment
Open
2 tasks done
Labels
documentation Improvements or additions to documentation

Comments

@SadmanYasar
Copy link

💭 Description

Currently in the process of whether or not to choose Appwrite for a project using Refine.dev and Nextjs. It will have a chatbot with RBAC so the response contents should be restricted based on the roles. I looked through the docs but only found basic implementation with OpenAI, Huggingface etc but no docs regarding saving embeddings to the database then converting user query to embedding and finding the nearest match etc. I would like to know if the use case I mentioned is possible using Appwrite. TIA.

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

@SadmanYasar SadmanYasar added the documentation Improvements or additions to documentation label Dec 12, 2024
@ChiragAgg5k
Copy link
Member

ChiragAgg5k commented Dec 19, 2024

I think we can implement something that you are looking for like this:

  1. generateEmbedding
async function generateEmbedding(text: string): Promise<number[]> {
    const response = await openai.createEmbedding({
        model: 'text-embedding-ada-002',
        input: text,
    });
    return response.data.data[0].embedding;
}
  1. storeEmbedding
async function storeEmbedding(text: string, embedding: number[], metadata: any) {
    await databases.createDocument(
        '[DATABASE_ID]',
        '[COLLECTION_ID]',
        'unique()',
        {
            text,
            embedding,
            metadata,
        }
    );
}
  1. searchEmbedding
async function searchSimilarEmbeddings(queryEmbedding: number[], limit: number = 5) {
    const response = await databases.listDocuments(
        '[DATABASE_ID]',
        '[COLLECTION_ID]',
        [
            Query.orderBy('embedding', 'ASC'),
            Query.limit(limit),
        ]
    );

    return response.documents.map(doc => ({
        text: doc.text,
        similarity: cosineSimilarity(queryEmbedding, doc.embedding),
        metadata: doc.metadata,
    })).sort((a, b) => b.similarity - a.similarity);
}

function cosineSimilarity(a: number[], b: number[]): number {
    const dotProduct = a.reduce((sum, _, i) => sum + a[i] * b[i], 0);
    const magnitudeA = Math.sqrt(a.reduce((sum, val) => sum + val * val, 0));
    const magnitudeB = Math.sqrt(b.reduce((sum, val) => sum + val * val, 0));
    return dotProduct / (magnitudeA * magnitudeB);
}

The similarity function can be adjusted as per your need.

As for RBAC, appwrite uses concept of Permissions that you can use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants