manabb commited on
Commit
9eb70b1
·
verified ·
1 Parent(s): d216b1f

Create qa_chain.py

Browse files
Files changed (1) hide show
  1. qa_chain.py +28 -0
qa_chain.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_openai import ChatOpenAI
2
+ from langchain.chains import create_retrieval_chain
3
+ from langchain.chains.combine_documents import create_stuff_documents_chain
4
+ from langchain_core.prompts import ChatPromptTemplate
5
+
6
+ def manabQA(retriever, question):
7
+ os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
8
+ # Step 1: Initialize GPT-4o-mini LLM
9
+ llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
10
+
11
+ # Step 2: Create system prompt for QA
12
+ system_prompt = (
13
+ "You are a helpful assistant. Use the following context to answer the question. "
14
+ "If you don't know the answer, say so.\n\n"
15
+ "{context}"
16
+ )
17
+ prompt = ChatPromptTemplate.from_messages([
18
+ ("system", system_prompt),
19
+ ("human", "{input}")
20
+ ])
21
+
22
+ # Step 3: Create document chain and full retrieval chain
23
+ question_answer_chain = create_stuff_documents_chain(llm, prompt)
24
+ qa_chain = create_retrieval_chain(retriever, question_answer_chain)
25
+
26
+ # Step 4: Ask a question
27
+ response = qa_chain.invoke({"input": question})
28
+ return response