File size: 3,278 Bytes
2ebeded
 
326189b
 
 
2ebeded
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from sentence_transformers import SentenceTransformer
from transformers import pipeline
from langchain_core.prompts import ChatPromptTemplate
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain, SequentialChain
from langchain_groq import ChatGroq
from langchain.embeddings import HuggingFaceEmbeddings
from dotenv import load_dotenv
import os

load_dotenv()
api_key = os.getenv('GROQ_API_KEY')
# print(api_key)

class LearningPathModel:
    def __init__(self):
        # Initialize embedding and NLP models
        self.embedding_model = SentenceTransformer('all-MiniLM-L6-v2')
        self.qa_pipeline = pipeline('question-answering', model='distilbert-base-cased-distilled-squad')
        self.summarizer = pipeline('summarization', model='facebook/bart-large-cnn')

        # Define LangChain elements
        self.embedding_chain = LLMChain(
            llm=ChatGroq(model_name="llama-3.1-70b-versatile"),  # Example: replace with Groq LLM chain if needed
            prompt=PromptTemplate(template="Generate an embedding for the following text: {text}", input_variables=["text"])
        )

        self.qa_chain = LLMChain(
            llm=ChatGroq(model_name="llama-3.1-70b-versatile"),
            prompt=PromptTemplate(template="Based on the context provided, answer the question: {question}. Context: {context}", input_variables=["question", "context"])
        )

        self.summarization_chain = LLMChain(
            llm=ChatGroq(model_name="llama-3.1-70b-versatile"),
            prompt=PromptTemplate(template="Summarize the following text: {text}", input_variables=["text"])
        )

        # Combine chains into a sequential chain
        self.sequential_chain = SequentialChain(chains=[self.embedding_chain, self.qa_chain, self.summarization_chain], input_variables=['text', 'question', 'context'])

    def generate_embeddings(self, content_list):
        # Generate embeddings for a list of content items
        embeddings = [self.embedding_model.encode(content) for content in content_list]
        return embeddings

    def assess_knowledge(self, question, context):
        # Use the QA pipeline to assess knowledge
        response = self.qa_pipeline(question=question, context=context)
        return response['answer'], response['score']

    def summarize_content(self, content):
        # Summarize content using the summarizer chain
        summary = self.summarizer(content, max_length=60, min_length=30, do_sample=False)[0]['summary_text']
        return summary

    def recommend_learning_path(self, user_input, content_data):
        # Generate embeddings
        content_embeddings = self.generate_embeddings([c['description'] for c in content_data])
        user_embedding = self.generate_embeddings([user_input])[0]
        
        # Simple similarity scoring (cosine similarity) to match user input with content
        import numpy as np
        similarities = np.dot(content_embeddings, user_embedding) / (np.linalg.norm(content_embeddings, axis=1) * np.linalg.norm(user_embedding))
        
        # Recommend top 3 most similar content items
        top_indices = np.argsort(similarities)[-3:][::-1]
        recommendations = [content_data[i] for i in top_indices]
        return recommendations