Spaces:
Runtime error
Runtime error
ray
commited on
Commit
·
e252ee6
1
Parent(s):
338119e
update(chat interface): some refactoring and resize of chatbot after gradio update
Browse files
app.py
CHANGED
|
@@ -47,9 +47,6 @@ set_global_service_context(service_context)
|
|
| 47 |
class AwesumIndexBuilder(IndexBuilder):
|
| 48 |
def _load_doucments(self):
|
| 49 |
directory = "./awesumcare_data/awesumcare_manual_data"
|
| 50 |
-
# all_files = glob.glob(os.path.join(directory, '*.md'))
|
| 51 |
-
# faq_files = [f for f in all_files if 'FAQ' in os.path.basename(f)]
|
| 52 |
-
# print(faq_files)
|
| 53 |
dir_reader = SimpleDirectoryReader(directory, file_extractor={
|
| 54 |
".pdf": UnstructuredReader(),
|
| 55 |
".docx": UnstructuredReader(),
|
|
@@ -57,7 +54,6 @@ class AwesumIndexBuilder(IndexBuilder):
|
|
| 57 |
".md": MarkdownReader()
|
| 58 |
},
|
| 59 |
recursive=True,
|
| 60 |
-
# input_files=faq_files,
|
| 61 |
exclude=["*.png", "*.pptx", "*.docx", "*.pdf"],
|
| 62 |
file_metadata=default_file_metadata_func)
|
| 63 |
|
|
@@ -80,7 +76,6 @@ class AwesumIndexBuilder(IndexBuilder):
|
|
| 80 |
return
|
| 81 |
pipeline = IngestionPipeline(
|
| 82 |
transformations=[
|
| 83 |
-
# SentenceSplitter(),
|
| 84 |
self.embed_model,
|
| 85 |
],
|
| 86 |
vector_store=self.vector_store,
|
|
@@ -107,13 +102,6 @@ class AwesumCareToolChatbot(Chatbot):
|
|
| 107 |
def _setup_index(self):
|
| 108 |
super()._setup_index()
|
| 109 |
|
| 110 |
-
# def _setup_index(self):
|
| 111 |
-
# self.index = VectorStoreIndex.from_documents(
|
| 112 |
-
# self.documents,
|
| 113 |
-
# service_context=self.service_context
|
| 114 |
-
# )
|
| 115 |
-
# super()._setup_index()
|
| 116 |
-
|
| 117 |
def _setup_query_engine(self):
|
| 118 |
super()._setup_query_engine()
|
| 119 |
self.query_engine = self.index.as_query_engine(
|
|
@@ -126,7 +114,6 @@ class AwesumCareToolChatbot(Chatbot):
|
|
| 126 |
return super()._setup_tools()
|
| 127 |
|
| 128 |
def _setup_chat_engine(self):
|
| 129 |
-
# testing #
|
| 130 |
from llama_index.agent import OpenAIAgent
|
| 131 |
self.chat_engine = OpenAIAgent.from_tools(
|
| 132 |
tools=[self.tools],
|
|
@@ -135,11 +122,6 @@ class AwesumCareToolChatbot(Chatbot):
|
|
| 135 |
verbose=True
|
| 136 |
)
|
| 137 |
print("set up agent as chat engine")
|
| 138 |
-
# testing #
|
| 139 |
-
# self.chat_engine = self.index.as_chat_engine(
|
| 140 |
-
# chat_mode=ChatMode.BEST,
|
| 141 |
-
# similarity_top_k=5,
|
| 142 |
-
# text_qa_template=CHAT_TEXT_QA_PROMPT)
|
| 143 |
super()._setup_chat_engine()
|
| 144 |
|
| 145 |
class AweSumCareContextChatbot(AwesumCareToolChatbot):
|
|
@@ -186,84 +168,30 @@ def service_setup(model_name):
|
|
| 186 |
set_global_service_context(service_context)
|
| 187 |
return LLM, EMBED_MODEL
|
| 188 |
|
|
|
|
| 189 |
|
| 190 |
def vote(data: gr.LikeData):
|
| 191 |
if data.liked:
|
| 192 |
-
|
| 193 |
else:
|
| 194 |
-
|
| 195 |
-
|
| 196 |
|
| 197 |
-
chatbot = gr.Chatbot()
|
| 198 |
|
| 199 |
-
with gr.Blocks() as demo:
|
| 200 |
|
| 201 |
gr.Markdown("# Awesum Care demo")
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
# with gr.Row():
|
| 206 |
-
# model_selector = gr.Radio(
|
| 207 |
-
# value=ChatbotVersion.CHATGPT_35.value,
|
| 208 |
-
# choices=[ChatbotVersion.CHATGPT_35.value, ChatbotVersion.CHATGPT_4.value],
|
| 209 |
-
# label="Select Chatbot Model (To be implemented)"
|
| 210 |
-
# )
|
| 211 |
-
|
| 212 |
with gr.Tab("With relevant context sent to system prompt"):
|
| 213 |
context_interface = gr.ChatInterface(
|
| 214 |
awesum_chatbot_context.stream_chat,
|
| 215 |
examples=awesum_chatbot.CHAT_EXAMPLES,
|
|
|
|
| 216 |
)
|
| 217 |
-
chatbot.like(vote, None, None)
|
| 218 |
-
|
| 219 |
-
# with gr.Tab("With function calling as tool to retrieve"):
|
| 220 |
-
# function_call_interface = gr.ChatInterface(
|
| 221 |
-
# awesum_chatbot.stream_chat,
|
| 222 |
-
# examples=awesum_chatbot.CHAT_EXAMPLES,
|
| 223 |
-
# )
|
| 224 |
-
# chatbot.like(vote, None, None)
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
# with gr.Tab("Vanilla ChatGPT without modification"):
|
| 228 |
-
# vanilla_interface = gr.ChatInterface(
|
| 229 |
-
# awesum_chatbot_simple.stream_chat,
|
| 230 |
-
# examples=awesum_chatbot.CHAT_EXAMPLES)
|
| 231 |
-
|
| 232 |
-
gr.Markdown("instructions:\n"
|
| 233 |
-
"\nUsing model gpt-4-preview-1106, the most advanced model now in the market.\n"
|
| 234 |
-
"\n(Note that it can be much slower than gpt-3.5, openai's api can be unstable sometimes.)\n"
|
| 235 |
-
# "\nThree Tabs:\n"
|
| 236 |
-
# "1. Relevant context: retreiving relevant documents and send to ChatGPT.\n"
|
| 237 |
-
# "2. Give tools to chatgpt to retrieve context: the most advanced, slowest (>30s to use the tools, before answering).\n"
|
| 238 |
-
# "3. Vanilla ChatGPT: self-explanatory.\n"
|
| 239 |
-
)
|
| 240 |
-
# @model_selector.change(inputs=[model_selector, chatbot], outputs=[context_interface, function_call_interface, vanilla_interface])
|
| 241 |
-
# def switch_model(model_name, my_chatbot):
|
| 242 |
-
# print(model_name)
|
| 243 |
-
# print(my_chatbot.config())
|
| 244 |
-
# LLM, EMBED_MODEL = service_setup(model_name)
|
| 245 |
-
# # global awesum_chatbot, awesum_chatbot_context, awesum_chatbot_simple
|
| 246 |
-
# # Logic to switch models - create new instances of the chatbots with the selected model
|
| 247 |
-
# index_builder = AwesumIndexBuilder(vdb_collection_name=VDB_COLLECTION_NAME,
|
| 248 |
-
# embed_model=EMBED_MODEL,
|
| 249 |
-
# is_load_from_vector_store=IS_LOAD_FROM_VECTOR_STORE)
|
| 250 |
-
# awesum_chatbot = AwesumCareToolChatbot(model_name=model_name, index_builder=index_builder, llm=LLM)
|
| 251 |
-
# awesum_chatbot_context = AweSumCareContextChatbot(model_name=model_name, index_builder=index_builder)
|
| 252 |
-
# awesum_chatbot_simple = AweSumCareSimpleChatbot(model_name=model_name, index_builder=index_builder)
|
| 253 |
-
# # return awesum_chatbot.stream_chat, awesum_chatbot_context.stream_chat, awesum_chatbot_simple.stream_chat
|
| 254 |
-
|
| 255 |
-
# new_context_interface = gr.ChatInterface(
|
| 256 |
-
# awesum_chatbot_context.stream_chat,
|
| 257 |
-
# )
|
| 258 |
-
# new_function_call_interface = gr.ChatInterface(
|
| 259 |
-
# awesum_chatbot.stream_chat,
|
| 260 |
-
# )
|
| 261 |
-
# new_vanilla_interface = gr.ChatInterface(
|
| 262 |
-
# awesum_chatbot_simple.stream_chat,
|
| 263 |
-
# )
|
| 264 |
-
# return new_context_interface, new_function_call_interface, new_vanilla_interface
|
| 265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
|
| 267 |
demo.queue()
|
| 268 |
demo.launch(share=False, auth=("demo", os.getenv("PASSWORD")))
|
| 269 |
-
|
|
|
|
| 47 |
class AwesumIndexBuilder(IndexBuilder):
|
| 48 |
def _load_doucments(self):
|
| 49 |
directory = "./awesumcare_data/awesumcare_manual_data"
|
|
|
|
|
|
|
|
|
|
| 50 |
dir_reader = SimpleDirectoryReader(directory, file_extractor={
|
| 51 |
".pdf": UnstructuredReader(),
|
| 52 |
".docx": UnstructuredReader(),
|
|
|
|
| 54 |
".md": MarkdownReader()
|
| 55 |
},
|
| 56 |
recursive=True,
|
|
|
|
| 57 |
exclude=["*.png", "*.pptx", "*.docx", "*.pdf"],
|
| 58 |
file_metadata=default_file_metadata_func)
|
| 59 |
|
|
|
|
| 76 |
return
|
| 77 |
pipeline = IngestionPipeline(
|
| 78 |
transformations=[
|
|
|
|
| 79 |
self.embed_model,
|
| 80 |
],
|
| 81 |
vector_store=self.vector_store,
|
|
|
|
| 102 |
def _setup_index(self):
|
| 103 |
super()._setup_index()
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
def _setup_query_engine(self):
|
| 106 |
super()._setup_query_engine()
|
| 107 |
self.query_engine = self.index.as_query_engine(
|
|
|
|
| 114 |
return super()._setup_tools()
|
| 115 |
|
| 116 |
def _setup_chat_engine(self):
|
|
|
|
| 117 |
from llama_index.agent import OpenAIAgent
|
| 118 |
self.chat_engine = OpenAIAgent.from_tools(
|
| 119 |
tools=[self.tools],
|
|
|
|
| 122 |
verbose=True
|
| 123 |
)
|
| 124 |
print("set up agent as chat engine")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
super()._setup_chat_engine()
|
| 126 |
|
| 127 |
class AweSumCareContextChatbot(AwesumCareToolChatbot):
|
|
|
|
| 168 |
set_global_service_context(service_context)
|
| 169 |
return LLM, EMBED_MODEL
|
| 170 |
|
| 171 |
+
chatbot = gr.Chatbot(height=360)
|
| 172 |
|
| 173 |
def vote(data: gr.LikeData):
|
| 174 |
if data.liked:
|
| 175 |
+
print("You upvoted this response: " + data.value)
|
| 176 |
else:
|
| 177 |
+
print("You downvoted this response: " + data.value)
|
|
|
|
| 178 |
|
|
|
|
| 179 |
|
| 180 |
+
with gr.Blocks(fill_height=True) as demo:
|
| 181 |
|
| 182 |
gr.Markdown("# Awesum Care demo")
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
with gr.Tab("With relevant context sent to system prompt"):
|
| 185 |
context_interface = gr.ChatInterface(
|
| 186 |
awesum_chatbot_context.stream_chat,
|
| 187 |
examples=awesum_chatbot.CHAT_EXAMPLES,
|
| 188 |
+
chatbot=chatbot
|
| 189 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
# gr.Markdown("instructions:\n"
|
| 192 |
+
# "\nUsing model gpt-4-preview-1106, the most advanced model now in the market.\n"
|
| 193 |
+
# "\n(Note that it can be much slower than gpt-3.5, openai's api can be unstable sometimes.)\n"
|
| 194 |
+
# )
|
| 195 |
|
| 196 |
demo.queue()
|
| 197 |
demo.launch(share=False, auth=("demo", os.getenv("PASSWORD")))
|
|
|