pd4consultingmyles commited on
Commit
c4555db
·
1 Parent(s): 0305625

update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -40
app.py CHANGED
@@ -1,11 +1,6 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- """
4
- Created on Tue Apr 25 04:44:31 2023
5
-
6
- @author: mai2125
7
- """
8
-
9
  from gpt_index import (
10
  SimpleWebPageReader,
11
  WikipediaReader,
@@ -15,38 +10,68 @@ from gpt_index import (
15
  QuestionAnswerPrompt,
16
  PromptHelper
17
  )
18
- from langchain.chat_models import ChatOpenAI
19
- from langchain.prompts.chat import ChatPromptTemplate, SystemMessagePromptTemplate
20
- import gradio as gr
21
- import sys
22
- import os
23
- # openai api key
24
- os.environ["OPENAI_API_KEY"] = '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'
25
 
26
- def chatbot(input_text):
27
- # preset prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- query_str = "What state is Atlanta in?"
30
- QA_PROMPT_TMPL = (
31
- "You are an AI specialized in Atlanta.\n"
32
- "Refuse to answer queries specifically that do not related to Atlanta.\n"
33
- "If a query does not involve Atlanta, assume that it is related to Atlanta."
34
- "We have provided context information below. \n"
35
- "---------------------\n"
36
- "{context_str}"
37
- "\n---------------------\n"
38
- "Given this information, please answer the question: {query_str}\n"
39
  )
40
- QA_PROMPT = QuestionAnswerPrompt(QA_PROMPT_TMPL)
41
- # Takes in the input from the user to deliver responses
42
- index = GPTSimpleVectorIndex.load_from_disk('index.json')
43
- response = index.query(input_text, text_qa_template = QA_PROMPT)
44
- return response.response
45
-
46
- # creates the interface for the user to interact with the chatbot
47
- iface = gr.Interface(fn=chatbot,
48
- inputs=gr.components.Textbox(lines=7, label="Enter your text"),
49
- outputs="text",
50
- title="PD4 Chatbot on Atlanta Wiki")
51
-
52
- iface.launch()
 
1
+ import time
2
+ import gradio as gr
3
+ from gpt_index import RefinePrompt
 
 
 
 
 
4
  from gpt_index import (
5
  SimpleWebPageReader,
6
  WikipediaReader,
 
10
  QuestionAnswerPrompt,
11
  PromptHelper
12
  )
 
 
 
 
 
 
 
13
 
14
+ system_message = {"role": "system", "content": "You are an AI specialized in Atlanta."}
15
+
16
+
17
+ with gr.Blocks() as demo:
18
+ gr.Markdown(
19
+ '''
20
+ # Customized Atlanta Chatbot
21
+ This chatbot uses the Atlantaga.gov website as its custom knowledge base
22
+ '''
23
+ )
24
+ chatbot = gr.Chatbot()
25
+ msg = gr.Textbox()
26
+ clear = gr.Button("Clear")
27
+
28
+
29
+ state = gr.State([])
30
+
31
+ def user(user_message, history):
32
+ return "", history + [[user_message, None]]
33
+
34
+ def bot(history, messages_history):
35
+ user_message = history[-1][0]
36
+ bot_message, messages_history = ask_gpt(user_message, messages_history)
37
+ messages_history += [{"role": "assistant", "content": bot_message}]
38
+ history[-1][1] = bot_message
39
+ time.sleep(1)
40
+ return history, messages_history
41
+
42
+ def ask_gpt(message, messages_history):
43
+ messages_history += [{"role": "user", "content": message}]
44
+ query_str = "Who is the major of Atlanta?"
45
+ QA_PROMPT_TMPL = (
46
+ "You are an AI specialized in Atlanta.\n"
47
+ "If a query does not involve Atlanta, say you can't answer the query and make the answer related to Atlanta.\n"
48
+ #"If you cannot relate the query to Atlanta, do not answer it.\n"
49
+ "We have provided context information below. \n"
50
+ "---------------------\n"
51
+ "{context_str}"
52
+ "\n---------------------\n"
53
+ "Given this information, please answer the query: {query_str}\n"
54
+ )
55
+ QA_PROMPT = QuestionAnswerPrompt(QA_PROMPT_TMPL)
56
+
57
+
58
+ # Takes in the input from the user to deliver responses
59
+ index = GPTSimpleVectorIndex.load_from_disk('index.json')
60
+ message = ' '.join([message['content'] for message in messages_history])
61
+ response = index.query(message, response_mode="compact", text_qa_template = QA_PROMPT)
62
+ return response.response, messages_history
63
+ #return response['choices'][0]['message']['content'], messages_history
64
 
65
+
66
+ def init_history(messages_history):
67
+ messages_history = []
68
+ messages_history += [system_message]
69
+ return messages_history
70
+
71
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
72
+ bot, [chatbot, state], [chatbot, state]
 
 
73
  )
74
+
75
+ clear.click(lambda: None, None, chatbot, queue=False).success(init_history, [state], [state])
76
+
77
+ demo.launch()