Spaces:
Runtime error
Runtime error
aisyahhrazak
commited on
Commit
•
9536c0f
1
Parent(s):
04a5735
add custom prompt
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import pinecone
|
4 |
-
from llama_index import GPTIndexMemory, GPTPineconeIndex
|
5 |
from langchain.agents import Tool
|
6 |
from langchain.chains.conversation.memory import ConversationBufferMemory
|
7 |
from langchain import OpenAI
|
@@ -15,16 +15,31 @@ pinecone.init(api_key=PINECONE_API_KEY, environment="us-east1-gcp")
|
|
15 |
pindex=pinecone.Index("sejarah")
|
16 |
indexed_pinecone=GPTPineconeIndex([], pinecone_index=pindex)
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
tools = [
|
19 |
Tool(
|
20 |
name = "GPT Index",
|
21 |
-
func=lambda q: str(indexed_pinecone.query(q)),
|
22 |
-
description="
|
23 |
return_direct=True
|
24 |
)
|
25 |
]
|
26 |
memory = GPTIndexMemory(index=indexed_pinecone, memory_key="chat_history", query_kwargs={"response_mode": "compact"})
|
27 |
-
llm=OpenAI(temperature=0, model_name="
|
28 |
agent_chain = initialize_agent(tools, llm, agent="conversational-react-description", memory=memory, verbose=True)
|
29 |
|
30 |
def predict(input, history=[]):
|
@@ -34,7 +49,7 @@ def predict(input, history=[]):
|
|
34 |
# response = [response]
|
35 |
# return response, response
|
36 |
return response, response
|
37 |
-
|
38 |
with gr.Blocks() as demo:
|
39 |
chatbot = gr.Chatbot()
|
40 |
state = gr.State([])
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import pinecone
|
4 |
+
from llama_index import GPTIndexMemory, GPTPineconeIndex,QuestionAnswerPrompt
|
5 |
from langchain.agents import Tool
|
6 |
from langchain.chains.conversation.memory import ConversationBufferMemory
|
7 |
from langchain import OpenAI
|
|
|
15 |
pindex=pinecone.Index("sejarah")
|
16 |
indexed_pinecone=GPTPineconeIndex([], pinecone_index=pindex)
|
17 |
|
18 |
+
|
19 |
+
QA_PROMPT_TMPL = (
|
20 |
+
"Answer each question truthfully using the Malaysia's Form 1 History textbook data provided."
|
21 |
+
"Your answers should be concise and straight to the point.For questions that are open-ended, which require subjective judgment or opinion, you may not find a definitive answer in the textbook."
|
22 |
+
"However, you should still address the question's directive based on the data's context. Ideally, your answer should provide 3 points that support your response. You are encouraged to better provide positive suggestions for concepts that are less ethical.Please keep in mind that the scope of the data provided is limited to the content covered in the Malaysia's Form 1 History textbook."
|
23 |
+
"If you do not know the answer please say 'I don't know, Please rephrase the question'\n"
|
24 |
+
"---------------------\n Context:\n"
|
25 |
+
"{context_str}"
|
26 |
+
"\n---------------------\n"
|
27 |
+
"Given this information, please answer the question: {query_str}\n"
|
28 |
+
)
|
29 |
+
|
30 |
+
|
31 |
+
QA_PROMPT = QuestionAnswerPrompt(QA_PROMPT_TMPL)
|
32 |
+
|
33 |
tools = [
|
34 |
Tool(
|
35 |
name = "GPT Index",
|
36 |
+
func=lambda q: str(indexed_pinecone.query(q,text_qa_template=QA_PROMPT)),
|
37 |
+
description="Asking questions to Pandai AI",
|
38 |
return_direct=True
|
39 |
)
|
40 |
]
|
41 |
memory = GPTIndexMemory(index=indexed_pinecone, memory_key="chat_history", query_kwargs={"response_mode": "compact"})
|
42 |
+
llm=OpenAI(temperature=0, model_name="text-davinci-003",max_tokens=1000)
|
43 |
agent_chain = initialize_agent(tools, llm, agent="conversational-react-description", memory=memory, verbose=True)
|
44 |
|
45 |
def predict(input, history=[]):
|
|
|
49 |
# response = [response]
|
50 |
# return response, response
|
51 |
return response, response
|
52 |
+
|
53 |
with gr.Blocks() as demo:
|
54 |
chatbot = gr.Chatbot()
|
55 |
state = gr.State([])
|