-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path007-rag-with-lcel.py
104 lines (63 loc) · 2.77 KB
/
007-rag-with-lcel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
openai_api_key = os.environ["OPENAI_API_KEY"]
from langchain_openai import ChatOpenAI
chatModel = ChatOpenAI(model="gpt-3.5-turbo-0125")
from langchain_community.document_loaders import TextLoader
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
from langchain_chroma import Chroma
# Load the document, split it into chunks, embed each chunk and load it into the vector store.
loaded_document = TextLoader('./data/state_of_the_union.txt').load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
chunks_of_text = text_splitter.split_documents(loaded_document)
vector_db = Chroma.from_documents(chunks_of_text, OpenAIEmbeddings())
question = "What did the president say about the John Lewis Voting Rights Act?"
response = vector_db.similarity_search(question)
print("\n----------\n")
print("Ask the RAG App: What did the president say about the John Lewis Voting Rights Act?")
print("\n----------\n")
#print(response[0].page_content)
print("\n----------\n")
from langchain_community.document_loaders import TextLoader
loader = TextLoader("./data/state_of_the_union.txt")
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings
from langchain_text_splitters import CharacterTextSplitter
loaded_document = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
chunks_of_text = text_splitter.split_documents(loaded_document)
embeddings = OpenAIEmbeddings()
vector_db = FAISS.from_documents(chunks_of_text, embeddings)
retriever = vector_db.as_retriever()
response = retriever.invoke("what did he say about ketanji brown jackson?")
print("\n----------\n")
print("Ask the RAG App with Retriever: What did he say about ketanji brown jackson?")
print("\n----------\n")
#print(response[0].page_content)
print("\n----------\n")
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
template = """Answer the question based only on the following context:
{context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)
model = ChatOpenAI()
def format_docs(docs):
return "\n\n".join([d.page_content for d in docs])
chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| model
| StrOutputParser()
)
response = chain.invoke("what did he say about ketanji brown jackson?")
print("\n----------\n")
print("Ask the RAG App with LCEL: What did he say about ketanji brown jackson?")
print("\n----------\n")
print(response)
print("\n----------\n")