-
Notifications
You must be signed in to change notification settings - Fork 0
/
txtinput.py
47 lines (40 loc) · 1.51 KB
/
txtinput.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
# setup environment variables
from dotenv import load_dotenv
import os
load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain.chains import create_retrieval_chain
from langchain_community.document_loaders import TextLoader
import textwrap
def consoleformat(input,width=100):
formatted=textwrap.fill(input,width)
return formatted
llm=ChatOpenAI()
prompt=ChatPromptTemplate.from_template("""Answer the following request based only on the provided context:
<context>
{context}
</context>
Question: {input}""")
documentchain=create_stuff_documents_chain(llm,prompt)
outputparser=StrOutputParser()
chain=prompt|llm|outputparser
loader=TextLoader('book.txt')
docs=loader.load()
embeddings=OpenAIEmbeddings()
tsplitter=RecursiveCharacterTextSplitter()
documents=tsplitter.split_documents(docs)
vec=FAISS.from_documents(documents,embeddings)
retriever=vec.as_retriever()
retrievalchain=create_retrieval_chain(retriever,documentchain)
question='Please summarize the given file in 5 sentences or less'
response=retrievalchain.invoke({'input':question})
print(question)
cleanans=response['answer']
print(consoleformat(cleanans))