Views
No views yet
# Initialize LLM
self.llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
# Initialize vector store
self.vector_store = None
# Text splitter for long documents
self.text_splitter = RecursiveCharacterTextSplitter(
chunk_size=500,
chunk_overlap=50
)
# Search prompt
self.search_prompt = ChatPromptTemplate.from_template("""
You are a helpful course recommendation assistant. Based on the following course information
and the user's query, provide a concise response recommending the most relevant courses.
Course Information:
{context}
User Query: {query}
Please explain why these courses match their needs and include any relevant details about difficulty
level and duration. Keep your response clear and helpful.
""")
# Create the chain
self.chain = (
self.search_prompt
| self.llm
| StrOutputParser()
)
def load_courses(self, courses: List[Course]):
"""Load courses into the vector store"""
texts = []
metadatas = []
for course in courses:
# Combine course info into a single string
course_text = f"""
Title: {course.title}
Description: {course.description}
Curriculum: {course.curriculum}
Difficulty: {course.difficulty_level}
Duration: {course.duration}
"""
# Split long texts
splits = self.text_splitter.split_text(course_text)
texts.extend(splits)
# Add metadata for each split
for _ in splits:
metadatas.append({
"title": course.title,
"difficulty": course.difficulty_level,
"duration": course.duration
})
# Create vector store
self.vector_store = FAISS.from_texts(
texts,
self.embed_model,
metadatas=metadatas
)
def search(self, query: str, k: int = 3) -> str:
"""Search for courses based on the query"""
if not self.vector_store:
return "No courses loaded in the system."
# Retrieve relevant documents
docs = self.vector_store.similarity_search(query, k=k)
# Combine context from retrieved documents
context = "\n\n".join(doc.page_content for doc in docs)
# Generate response using the chain
response = self.chain.invoke({
"context": context,
"query": query
})
return responseinterface = gr.Interface(
fn=search_courses,
inputs=gr.Textbox(
lines=2,
placeholder="Enter your search query (e.g., 'beginner python course' or 'advanced machine learning')"
),
outputs=gr.Textbox(lines=10),
title="Smart Course Search",
description="Search for relevant courses based on your interests and requirements.",
examples=[
["I want to learn Python as a beginner"],
["Show me advanced machine learning courses"],
["What courses cover data visualization?"]
]
)
return interface