Spaces:
Runtime error
Runtime error
| from dotenv import load_dotenv | |
| # langchain libraries | |
| from langchain.chat_models import ChatOpenAI | |
| from langchain.chains import RetrievalQAWithSourcesChain | |
| from langchain.prompts.chat import ( | |
| ChatPromptTemplate, | |
| SystemMessagePromptTemplate, | |
| HumanMessagePromptTemplate, | |
| ) | |
| import openai | |
| import os | |
| from vector_store import get_or_create_vector_store | |
| from pinecone_vector_store import get_pinecone_store | |
| load_dotenv() | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| def generate_prompt_templates(): | |
| system_template = """Use the following pieces of context to answer the users question as long as possible. Provided context is regulations of subway company in busan. what users want to query is exact details or inferable information from regulations. | |
| Given the following summaries of a long document and a question, create a final answer with references to the document. | |
| If you don't know the answer, just say that "μ£μ‘ν©λλ€. ν΄λΉμ§λ¬Έμ λν λ΅μ μ°Ύμ§ λͺ»νμ΅λλ€.πΏ\\n μ¬κ·μ ν¬ν¨λ λ΄μ©μ ꡬ체μ μΌλ‘ λ²μλ₯Ό μ’νμ μ§λ¬Έν΄μ£ΌμΈμ.π€\\n μνλ λ΅μ μ»μ§ λͺ»νμ κ²½μ° μ΄λ€ κ·μ μ λμμΌλ‘ μ§λ¬Ένλμ§ λͺ μν΄μ λ€μ μ§λ¬Έν΄ 보μΈμ! ", don't try to make up an answer. + '\n' | |
| ---------------- | |
| {summaries} | |
| You MUST answer in Korean:""" | |
| messages = [ | |
| SystemMessagePromptTemplate.from_template(system_template), | |
| HumanMessagePromptTemplate.from_template("{question}") | |
| ] | |
| prompt = ChatPromptTemplate.from_messages(messages) | |
| return prompt | |
| def generate_chain(pincone=True): | |
| if pincone: | |
| vector_store = get_pinecone_store() | |
| else: | |
| vector_store = get_or_create_vector_store() | |
| retriever = vector_store.as_retriever(search_kwargs={"k": 4}) | |
| llm = ChatOpenAI(model_name="gpt-3.5-turbo-16k", temperature=0.3) | |
| chain_type_kwargs = {"prompt": generate_prompt_templates()} | |
| chain = RetrievalQAWithSourcesChain.from_chain_type( | |
| llm=llm, | |
| chain_type="stuff", | |
| retriever=retriever, | |
| return_source_documents=True, | |
| chain_type_kwargs=chain_type_kwargs) | |
| return chain | |