File size: 2,154 Bytes
01b7e90
 
 
 
 
 
 
 
 
 
 
 
 
 
04f9bf9
01b7e90
 
 
 
 
 
 
04f9bf9
01b7e90
04f9bf9
01b7e90
 
 
 
 
 
 
 
 
 
 
 
 
 
04f9bf9
 
 
 
 
01b7e90
04f9bf9
 
 
01b7e90
 
 
 
 
 
 
 
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
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