Spaces:
Runtime error
fix(app.py): import missing dependencies chromadb and langchain.PromptTemplate
Browse filesfix(app.py): remove unused import HumanMessagePromptTemplate
fix(app.py): change variable name vector_store to client to match the updated Chroma constructor
fix(app.py): change search_kwargs parameter value from {"k": 3} to {"k": 5} to retrieve more results
fix(app.py): uncomment the line initializing llm variable
fix(app.py): update system_template to provide clearer instructions and emphasize returning "SOURCES" part in the answer
fix(app.py): update prompt template to include question and summaries placeholders
fix(app.py): update respond function to include sources in the bot_message
fix(app.py): update initial_greeting message to provide more context about Sayno and encourage asking questions
fix(app.py): update clear button to not have any action
@@ -1,10 +1,11 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from langchain.chains import RetrievalQAWithSourcesChain
|
3 |
from langchain.chat_models import ChatOpenAI
|
4 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
5 |
from langchain.prompts.chat import (
|
6 |
ChatPromptTemplate,
|
7 |
-
HumanMessagePromptTemplate,
|
8 |
SystemMessagePromptTemplate,
|
9 |
)
|
10 |
from langchain.vectorstores import Chroma
|
@@ -12,28 +13,51 @@ from langchain.vectorstores import Chroma
|
|
12 |
from constants import persist_directory
|
13 |
|
14 |
embedding = OpenAIEmbeddings()
|
15 |
-
|
16 |
-
|
|
|
17 |
|
|
|
18 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
|
19 |
|
20 |
-
system_template = """Your name is Sayno. You act like a successful rich man.
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
{summaries}
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
33 |
|
34 |
-
prompt = ChatPromptTemplate.from_messages(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
chain_type_kwargs = {"prompt": prompt}
|
37 |
|
38 |
chain = RetrievalQAWithSourcesChain.from_chain_type(
|
39 |
llm=llm,
|
@@ -48,7 +72,7 @@ chain = RetrievalQAWithSourcesChain.from_chain_type(
|
|
48 |
|
49 |
def respond(message, chat_history):
|
50 |
result = chain(message)
|
51 |
-
bot_message = result["answer"]
|
52 |
chat_history.append((message, bot_message))
|
53 |
return "", chat_history
|
54 |
|
@@ -57,8 +81,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
57 |
gr.Markdown("# 안녕하세요. 세이노와 대화해보세요.")
|
58 |
initial_greeting = "안녕하세요!\n저는 세이노처럼 경험과 지식을 갖춘 인공지능 ChatGPT입니다. 세이노는 사업, 경영, 투자에 대한 전문가이며, 많은 사람들이 그의 조언을 참고하고 있습니다. 어떤 도움이 필요하신가요? 세이노와 관련된 질문이 있으시면 편안하게 물어보세요!"
|
59 |
chatbot = gr.Chatbot(label="채팅창", value=[(None, initial_greeting)])
|
60 |
-
msg = gr.Textbox(label="입력")
|
61 |
-
clear = gr.Button("초기화")
|
62 |
|
63 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
64 |
clear.click(lambda: None, None, chatbot, queue=False)
|
|
|
1 |
import gradio as gr
|
2 |
+
import chromadb
|
3 |
+
from langchain import PromptTemplate
|
4 |
from langchain.chains import RetrievalQAWithSourcesChain
|
5 |
from langchain.chat_models import ChatOpenAI
|
6 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
7 |
from langchain.prompts.chat import (
|
8 |
ChatPromptTemplate,
|
|
|
9 |
SystemMessagePromptTemplate,
|
10 |
)
|
11 |
from langchain.vectorstores import Chroma
|
|
|
13 |
from constants import persist_directory
|
14 |
|
15 |
embedding = OpenAIEmbeddings()
|
16 |
+
client = chromadb.PersistentClient(path=persist_directory)
|
17 |
+
vector_store = Chroma(client=client, embedding_function=embedding)
|
18 |
+
retriever = vector_store.as_retriever(search_kwargs={"k": 5})
|
19 |
|
20 |
+
# llm = ChatOpenAI(model_name="gpt-3.5-turbo-16k", temperature=0)
|
21 |
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
|
22 |
|
23 |
+
system_template = """Your name is Sayno. You act like a successful rich man. Never reveal that it is an AI.
|
24 |
+
Given the following extracted parts of a long document and a question, create a final answer with references ("SOURCES").
|
25 |
+
If you don't know the answer, just say that you don't know. Don't try to make up an answer.
|
26 |
+
!IMPORTANT: ALWAYS return a "SOURCES" part in your answer.
|
27 |
+
!IMPORTANT: Be sure to answer in Korean.
|
|
|
28 |
|
29 |
+
[EXAMPLE]
|
30 |
+
QUESTION: Which state/country's law governs the interpretation of the contract?
|
31 |
+
=========
|
32 |
+
Content: This Agreement is governed by English law and the parties submit to the exclusive jurisdiction of the English courts in relation to any dispute (contractual or non-contractual) concerning this Agreement save that either party may apply to any court for an injunction or other relief to protect its Intellectual Property Rights.
|
33 |
+
Source: 28-pl
|
34 |
+
Content: No Waiver. Failure or delay in exercising any right or remedy under this Agreement shall not constitute a waiver of such (or any other) right or remedy.\n\n11.7 Severability. The invalidity, illegality or unenforceability of any term (or part of a term) of this Agreement shall not affect the continuation in force of the remainder of the term (if any) and this Agreement.\n\n11.8 No Agency. Except as expressly stated otherwise, nothing in this Agreement shall create an agency, partnership or joint venture of any kind between the parties.\n\n11.9 No Third-Party Beneficiaries.
|
35 |
+
Source: 30-pl
|
36 |
+
Content: (b) if Google believes, in good faith, that the Distributor has violated or caused Google to violate any Anti-Bribery Laws (as defined in Clause 8.5) or that such a violation is reasonably likely to occur,
|
37 |
+
Source: 4-pl
|
38 |
+
=========
|
39 |
+
FINAL ANSWER: This Agreement is governed by English law.
|
40 |
+
SOURCES: 28-pl
|
41 |
|
42 |
+
QUESTION: {question}
|
43 |
+
=========
|
44 |
+
{summaries}
|
45 |
+
=========
|
46 |
+
FINAL ANSWER:
|
47 |
+
"""
|
48 |
|
49 |
+
prompt = ChatPromptTemplate.from_messages(
|
50 |
+
[
|
51 |
+
SystemMessagePromptTemplate.from_template(system_template),
|
52 |
+
]
|
53 |
+
)
|
54 |
+
|
55 |
+
document_prompt = PromptTemplate(
|
56 |
+
template="Content: {page_content}\nSource: {source}, {page} page",
|
57 |
+
input_variables=["page_content", "source", "page"],
|
58 |
+
)
|
59 |
|
60 |
+
chain_type_kwargs = {"prompt": prompt, "document_prompt": document_prompt}
|
61 |
|
62 |
chain = RetrievalQAWithSourcesChain.from_chain_type(
|
63 |
llm=llm,
|
|
|
72 |
|
73 |
def respond(message, chat_history):
|
74 |
result = chain(message)
|
75 |
+
bot_message = f'{result["answer"]}<br>- 출처: {result["sources"]}'
|
76 |
chat_history.append((message, bot_message))
|
77 |
return "", chat_history
|
78 |
|
|
|
81 |
gr.Markdown("# 안녕하세요. 세이노와 대화해보세요.")
|
82 |
initial_greeting = "안녕하세요!\n저는 세이노처럼 경험과 지식을 갖춘 인공지능 ChatGPT입니다. 세이노는 사업, 경영, 투자에 대한 전문가이며, 많은 사람들이 그의 조언을 참고하고 있습니다. 어떤 도움이 필요하신가요? 세이노와 관련된 질문이 있으시면 편안하게 물어보세요!"
|
83 |
chatbot = gr.Chatbot(label="채팅창", value=[(None, initial_greeting)])
|
84 |
+
msg = gr.Textbox(label="입력")
|
85 |
+
clear = gr.Button("초기화")
|
86 |
|
87 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
88 |
clear.click(lambda: None, None, chatbot, queue=False)
|