Skip to content

Commit e793e96

Browse files
committed
Add document loaders for various formats and implement loading logic
1 parent fae2a4d commit e793e96

File tree

11 files changed

+909
-0
lines changed

11 files changed

+909
-0
lines changed

Generative AI with Langchain/Document Loaders/Social_Network_Ads.csv

Lines changed: 401 additions & 0 deletions
Large diffs are not rendered by default.
Binary file not shown.
Binary file not shown.

Generative AI with Langchain/Document Loaders/cricket.txt

Lines changed: 399 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from langchain_community.document_loaders import CSVLoader
2+
3+
loader = CSVLoader('Social_Network_Ads.csv')
4+
5+
documents = loader.load()
6+
7+
# print(len(documents))
8+
print(documents[12].page_content)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from langchain_community.document_loaders import DirectoryLoader, PyPDFLoader
2+
3+
loader = DirectoryLoader(
4+
path='books',
5+
glob="*.pdf",
6+
loader_cls=PyPDFLoader
7+
)
8+
9+
# documents = loader.load()
10+
documents = loader.lazy_load()
11+
# print(len(documents))
12+
13+
# print(documents[325].page_content)
14+
# print(documents[325].metadata)
15+
16+
for doc in documents:
17+
print(doc.metadata)
113 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from langchain_community.document_loaders import PyPDFLoader
2+
3+
loader = PyPDFLoader('dl-curriculum.pdf')
4+
5+
documents = loader.load()
6+
7+
# print(documents)
8+
# print(len(documents))
9+
10+
print(documents[16].metadata)
11+
print(documents[0].page_content)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from langchain_community.document_loaders import TextLoader
2+
from langchain.chat_models import ChatOpenAI
3+
from langchain_core.output_parsers import StrOutputParser
4+
from langchain.prompts import PromptTemplate
5+
from dotenv import load_dotenv
6+
import os, warnings
7+
warnings.filterwarnings('ignore')
8+
9+
load_dotenv()
10+
11+
api_key = os.getenv('OPENAI_API_KEY')
12+
13+
model = ChatOpenAI(api_key=api_key)
14+
15+
prompt = PromptTemplate(
16+
template='Write a summary for the following poem:\n{poem}',
17+
input_variables=['poem']
18+
)
19+
20+
parser = StrOutputParser()
21+
22+
loader = TextLoader('cricket.txt', encoding='utf-8')
23+
24+
documents = loader.load()
25+
26+
# print(documents)
27+
# print(type(documents))
28+
# print(len(documents))
29+
30+
# print(documents[0].metadata)
31+
# print(documents[0].page_content)
32+
# print(type(documents[0]))
33+
34+
chain = prompt | model | parser
35+
36+
result = chain.invoke({'poem': documents[0].page_content})
37+
print(result)

0 commit comments

Comments
 (0)