|
|
from sentence_transformers import SentenceTransformer |
|
|
from langchain_core.prompts import ChatPromptTemplate |
|
|
from langchain_core.messages import AIMessage, HumanMessage |
|
|
from langchain_chroma import Chroma |
|
|
import gradio as gr |
|
|
from huggingface_hub import InferenceClient |
|
|
import os |
|
|
import shutil |
|
|
|
|
|
shutil.rmtree("/root/.cache", ignore_errors=True) |
|
|
shutil.rmtree("/tmp", ignore_errors=True) |
|
|
|
|
|
|
|
|
CHROMA_PATH = "chroma" |
|
|
KEY = os.getenv("token") |
|
|
|
|
|
|
|
|
repo_id = "Qwen/Qwen2.5-7B-Instruct" |
|
|
|
|
|
PROMPT_TEMPLATE = """ |
|
|
Answer the question based on the context provided. If no relevant information is found, state so. |
|
|
|
|
|
Context: |
|
|
{context} |
|
|
|
|
|
Question: |
|
|
{question} |
|
|
|
|
|
Answer: |
|
|
// Do not include the statement "Based on the provided context" in your answer. Start directly with the answer. |
|
|
// If the user mentions that he does not want to know about these courses, ask him what topic he wants to learn about in the answer. |
|
|
|
|
|
If you encounter a text message which is not related to the context, the bot must respond with "I don't have relevant information to answer that. Kindly ask queries related to free data science and machine learning courses on Analytics Vidhya." |
|
|
""" |
|
|
|
|
|
|
|
|
embedding_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2') |
|
|
|
|
|
|
|
|
class LocalEmbeddingFunction: |
|
|
def embed_documents(self, texts): |
|
|
|
|
|
embeddings = embedding_model.encode(texts) |
|
|
return embeddings.tolist() if hasattr(embeddings, 'tolist') else embeddings |
|
|
|
|
|
def embed_query(self, query): |
|
|
|
|
|
query_embedding = embedding_model.encode(query) |
|
|
return query_embedding.tolist() if hasattr(query_embedding, 'tolist') else query_embedding |
|
|
|
|
|
|
|
|
class LLM: |
|
|
|
|
|
def generate_response(self, prompt): |
|
|
client = InferenceClient( |
|
|
provider="together", |
|
|
api_key = KEY |
|
|
) |
|
|
completion = client.chat.completions.create( |
|
|
model=repo_id, |
|
|
messages=[ |
|
|
{ |
|
|
"role": "user", |
|
|
"content": prompt |
|
|
} |
|
|
], |
|
|
max_tokens=500, |
|
|
temperature=0.8 |
|
|
) |
|
|
return str(completion.choices[0].message.content) |
|
|
|
|
|
|
|
|
def get_embedding_function(): |
|
|
return LocalEmbeddingFunction() |
|
|
|
|
|
def get_chat_response(query, history): |
|
|
|
|
|
embedding_function = get_embedding_function() |
|
|
db = Chroma(persist_directory=CHROMA_PATH, embedding_function=embedding_function) |
|
|
|
|
|
|
|
|
results = db.similarity_search_with_score(query, k=5) |
|
|
context_text = "\n\n---\n\n".join([doc.page_content for doc, _score in results]) |
|
|
|
|
|
|
|
|
prompt_template = ChatPromptTemplate.from_template(PROMPT_TEMPLATE) |
|
|
prompt = prompt_template.format(context=context_text, question=query) |
|
|
|
|
|
|
|
|
model = LLM() |
|
|
response_text = model.generate_response(prompt) |
|
|
|
|
|
|
|
|
history.append(AIMessage(content = response_text)) |
|
|
|
|
|
return response_text |
|
|
|
|
|
|
|
|
|
|
|
def predict(message, history): |
|
|
|
|
|
history_langchain_format = [] |
|
|
|
|
|
for msg in history: |
|
|
|
|
|
if msg['role'] == "user": |
|
|
history_langchain_format.append(HumanMessage(content=msg['content'])) |
|
|
|
|
|
elif msg['role'] == "assistant": |
|
|
history_langchain_format.append(AIMessage(content=msg['content'])) |
|
|
|
|
|
history_langchain_format.append(HumanMessage(content=message)) |
|
|
|
|
|
|
|
|
response = get_chat_response(message, history_langchain_format) |
|
|
|
|
|
return response |
|
|
|
|
|
|
|
|
intro_content = """ |
|
|
# Course Recommendation Bot |
|
|
This bot helps you find **free courses related to python, data science and machine learning** available on [Analytics Vidhya](https://www.analyticsvidhya.com/). |
|
|
You can ask any questions related to these courses. |
|
|
|
|
|
For example: |
|
|
- "What are the free courses on Python?" |
|
|
- "Do you have any courses on data science?" |
|
|
""" |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown(intro_content) |
|
|
chatbot = gr.ChatInterface(predict, type="messages") |
|
|
|
|
|
demo.launch(share=False, ssr_mode=False) |
|
|
|