learning-path-navigator / learning_path_model.py
Sharath1036's picture
Update learning_path_model.py
326189b verified
raw
history blame
3.28 kB
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