import dotenv
dotenv.load_dotenv()
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain_community.document_loaders import WebBaseLoader
from langchain.chains import RetrievalQA
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
import streamlit as st
웹사이트 링크 입력하는 부분 만들기 (steamlit)
# 웹사이트 제목
st.title("Summarizer")
def get_text()
"""웹사이트 링크 입력"""
input_text = st.text_input("Type in the website below", key="input")
return input_text
# 웹사이트 링크 받기
user_input = get_text()
LLM 생성 및 실행
# 웹사이트가 입력 되면
if(user_input) :
# 웹사이트 데이터 읽어오기
loader = WebBaseLoader(user_input)
data = loader.load()
# 읽어온 데이터를 chunking 하기
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
all_splits = text_splitter.split_documents(data)
# chunking 한 데이터를 vectordb에 저장
vectorsotre = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
# LLM chain 생성
question = "Summarize the main points of this texts"
template = """Use the following pieces of context to answer the question at the end.
If you don't know the answer, just say that you don't know, don't try to make up an answer.
Return the result in Korean.
{context}
Question: {question}
Helpful Answer:"""
QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
qa_chain = RetrievalQA.from_chain_type(
llm,
retriever=vectorsotre.as_retriever(),
chain_type_kwargs={"prompt": QA_CHAIN_PROMPT}
)
# LLM Chain 실행
result = qa_chain({"query" : question})
result["result"]