|
|
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') |
|
|
|
|
|
|
|
|
class LearningPathModel: |
|
|
def __init__(self): |
|
|
|
|
|
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') |
|
|
|
|
|
|
|
|
self.embedding_chain = LLMChain( |
|
|
llm=ChatGroq(model_name="llama-3.1-70b-versatile"), |
|
|
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"]) |
|
|
) |
|
|
|
|
|
|
|
|
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): |
|
|
|
|
|
embeddings = [self.embedding_model.encode(content) for content in content_list] |
|
|
return embeddings |
|
|
|
|
|
def assess_knowledge(self, question, context): |
|
|
|
|
|
response = self.qa_pipeline(question=question, context=context) |
|
|
return response['answer'], response['score'] |
|
|
|
|
|
def summarize_content(self, content): |
|
|
|
|
|
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): |
|
|
|
|
|
content_embeddings = self.generate_embeddings([c['description'] for c in content_data]) |
|
|
user_embedding = self.generate_embeddings([user_input])[0] |
|
|
|
|
|
|
|
|
import numpy as np |
|
|
similarities = np.dot(content_embeddings, user_embedding) / (np.linalg.norm(content_embeddings, axis=1) * np.linalg.norm(user_embedding)) |
|
|
|
|
|
|
|
|
top_indices = np.argsort(similarities)[-3:][::-1] |
|
|
recommendations = [content_data[i] for i in top_indices] |
|
|
return recommendations |
|
|
|