flawlessfr commited on
Commit
bab0b25
·
verified ·
1 Parent(s): 2958016

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -15
app.py CHANGED
@@ -2,26 +2,26 @@ 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, "
@@ -34,22 +34,41 @@ prompt = ChatPromptTemplate.from_messages([
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__":
 
2
  import gradio as gr
3
  from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint
4
  from langchain_community.vectorstores import FAISS
 
 
5
  from langchain_core.prompts import ChatPromptTemplate
6
+ from langchain_core.runnables import RunnablePassthrough
7
+ from langchain_core.output_parsers import StrOutputParser
8
 
9
+ # 1. Load the FULL pre-computed Vector Database
10
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
11
+ vectorstore = FAISS.load_local("learncpp_faiss_index_full", embeddings, allow_dangerous_deserialization=True)
12
+ retriever = vectorstore.as_retriever(search_kwargs={"k": 5})
13
 
14
+ # 2. Set up the LLM
15
  hf_token = os.environ.get("HF_TOKEN")
16
  llm = HuggingFaceEndpoint(
17
+ repo_id="HuggingFaceH4/zephyr-7b-beta",
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 using modern LCEL (No langchain.chains needed!)
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, "
 
34
  ("human", "{input}"),
35
  ])
36
 
37
+ # Helper function to extract text from our vector chunks
38
+ def format_docs(docs):
39
+ return "\n\n".join(doc.page_content for doc in docs)
40
 
41
+ # The modern LangChain pipeline
42
+ rag_chain = (
43
+ {"context": retriever | format_docs, "input": RunnablePassthrough()}
44
+ | prompt
45
+ | llm
46
+ | StrOutputParser()
47
+ )
48
+
49
+ # 4. Define the Chat Interface WITH Memory
50
  def chat_function(message, history):
51
  try:
52
+ formatted_input = ""
53
+ if history:
54
+ formatted_input += "Previous Conversation Context:\n"
55
+ for user_msg, bot_msg in history:
56
+ formatted_input += f"User: {user_msg}\nBot: {bot_msg}\n"
57
+ formatted_input += "\n"
58
+
59
+ formatted_input += f"Current Question: {message}"
60
+
61
+ # Invoke the LCEL chain
62
+ response = rag_chain.invoke(formatted_input)
63
+ return response
64
  except Exception as e:
65
  return f"Error: {str(e)}"
66
 
67
  demo = gr.ChatInterface(
68
  fn=chat_function,
69
  title="LearnCpp.com AI Assistant",
70
+ description="Ask me any C++ question! I retrieve my answers directly from the complete LearnCpp tutorials.",
71
+ examples=["What is a pointer?", "Explain dynamic memory allocation.", "Give me an example of the previous concept."]
72
  )
73
 
74
  if __name__ == "__main__":