flawlessfr commited on
Commit
d26389c
·
verified ·
1 Parent(s): 4cd9be9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint
4
+ from langchain_community.vectorstores import FAISS
5
+ from langchain.chains import create_retrieval_chain
6
+ from langchain.chains.combine_documents import create_stuff_documents_chain
7
+ from langchain_core.prompts import ChatPromptTemplate
8
+
9
+ # 1. Load the pre-computed Vector Database
10
+ embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
11
+ # Allow dangerous deserialization is required for local FAISS loading
12
+ vectorstore = FAISS.load_local("learncpp_faiss_index", embeddings, allow_dangerous_deserialization=True)
13
+
14
+ # 2. Set up the LLM (Using Mistral-7B for excellent coding logic)
15
+ hf_token = os.environ.get("HF_TOKEN")
16
+ llm = HuggingFaceEndpoint(
17
+ repo_id="mistralai/Mistral-7B-Instruct-v0.2",
18
+ task="text-generation",
19
+ max_new_tokens=512,
20
+ temperature=0.1,
21
+ huggingfacehub_api_token=hf_token
22
+ )
23
+
24
+ # 3. Create the RAG Chain
25
+ system_prompt = (
26
+ "You are an expert C++ programming assistant. You answer questions strictly based "
27
+ "on the provided context from learncpp.com. If the answer is not in the context, "
28
+ "say 'I cannot find the answer in the LearnCpp documentation.'\n\n"
29
+ "Context:\n{context}"
30
+ )
31
+
32
+ prompt = ChatPromptTemplate.from_messages([
33
+ ("system", system_prompt),
34
+ ("human", "{input}"),
35
+ ])
36
+
37
+ qa_chain = create_stuff_documents_chain(llm, prompt)
38
+ rag_chain = create_retrieval_chain(vectorstore.as_retriever(search_kwargs={"k": 3}), qa_chain)
39
+
40
+ # 4. Define the Chat Interface
41
+ def chat_function(message, history):
42
+ try:
43
+ response = rag_chain.invoke({"input": message})
44
+ return response["answer"]
45
+ except Exception as e:
46
+ return f"Error: {str(e)}"
47
+
48
+ demo = gr.ChatInterface(
49
+ fn=chat_function,
50
+ title="LearnCpp.com AI Assistant",
51
+ description="Ask me any C++ question! I retrieve my answers directly from the LearnCpp tutorials.",
52
+ examples=["What is a pointer?", "Explain dynamic memory allocation."]
53
+ )
54
+
55
+ if __name__ == "__main__":
56
+ demo.launch()