ray commited on
Commit
4969145
1 Parent(s): 4efc36b

switch to load from vector store from qdrant cloud

Browse files
Files changed (4) hide show
  1. app.py +100 -21
  2. chatbot.py +28 -8
  3. qdrant.py +7 -2
  4. service_provider_config.py +2 -2
app.py CHANGED
@@ -20,16 +20,21 @@ from llama_index import set_global_service_context
20
 
21
  from service_provider_config import get_service_provider_config
22
 
23
-
24
  # initial service setup
25
  px.launch_app()
26
  llama_index.set_global_handler("arize_phoenix")
27
-
28
- load_dotenv()
29
  openai.api_key = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
30
  CHUNK_SIZE = 1024
31
  LLM, EMBED_MODEL = get_service_provider_config(
32
- service_provider=ServiceProvider.OPENAI)
33
  service_context = ServiceContext.from_defaults(
34
  chunk_size=CHUNK_SIZE,
35
  llm=LLM,
@@ -69,15 +74,15 @@ class AwesumIndexBuilder(IndexBuilder):
69
  pipeline = IngestionPipeline(
70
  transformations=[
71
  SentenceSplitter(),
72
- EMBED_MODEL,
73
  ],
74
  vector_store=self.vector_store,
75
  )
76
- pipeline.run(documents=self.documents)
77
  self.index = VectorStoreIndex.from_vector_store(self.vector_store)
78
 
79
 
80
- class AwesumCareChatbot(Chatbot):
81
  DENIED_ANSWER_PROMPT = ""
82
  SYSTEM_PROMPT = ""
83
  CHAT_EXAMPLES = [
@@ -126,13 +131,48 @@ class AwesumCareChatbot(Chatbot):
126
  # text_qa_template=CHAT_TEXT_QA_PROMPT)
127
  super()._setup_chat_engine()
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  # gpt-3.5-turbo-1106, gpt-4-1106-preview
131
- awesum_chatbot = AwesumCareChatbot(model_name=ChatbotVersion.CHATGPT_35.value,
132
- index_builder=AwesumIndexBuilder(
133
- vdb_collection_name="demo-v0",
134
- is_load_from_vector_store=False)
135
- )
 
 
 
 
 
 
 
 
 
 
 
136
 
137
 
138
  def vote(data: gr.LikeData):
@@ -145,23 +185,62 @@ def vote(data: gr.LikeData):
145
  chatbot = gr.Chatbot()
146
 
147
  with gr.Blocks() as demo:
 
148
  gr.Markdown("# Awesum Care demo")
149
 
150
- with gr.Tab("With awesum care data prepared"):
151
- gr.ChatInterface(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  awesum_chatbot.stream_chat,
153
- chatbot=chatbot,
154
  examples=awesum_chatbot.CHAT_EXAMPLES,
155
  )
156
  chatbot.like(vote, None, None)
157
 
158
- with gr.Tab("With Initial System Prompt (a.k.a. prompt wrapper)"):
159
- gr.ChatInterface(
160
- awesum_chatbot.predict_with_prompt_wrapper, examples=awesum_chatbot.CHAT_EXAMPLES)
161
 
162
  with gr.Tab("Vanilla ChatGPT without modification"):
163
- gr.ChatInterface(awesum_chatbot.predict_vanilla_chatgpt,
164
- examples=awesum_chatbot.CHAT_EXAMPLES)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
  demo.queue()
167
- demo.launch()
 
20
 
21
  from service_provider_config import get_service_provider_config
22
 
23
+ load_dotenv()
24
  # initial service setup
25
  px.launch_app()
26
  llama_index.set_global_handler("arize_phoenix")
27
+ # llama_index.set_global_handler("wandb", run_args={"project": "llamaindex"})
 
28
  openai.api_key = os.getenv("OPENAI_API_KEY")
29
+
30
+ IS_LOAD_FROM_VECTOR_STORE = True
31
+ VDB_COLLECTION_NAME = "demo-v0"
32
+ MODEL_NAME = ChatbotVersion.CHATGPT_35.value
33
+
34
+
35
  CHUNK_SIZE = 1024
36
  LLM, EMBED_MODEL = get_service_provider_config(
37
+ service_provider=ServiceProvider.OPENAI, model_name=MODEL_NAME)
38
  service_context = ServiceContext.from_defaults(
39
  chunk_size=CHUNK_SIZE,
40
  llm=LLM,
 
74
  pipeline = IngestionPipeline(
75
  transformations=[
76
  SentenceSplitter(),
77
+ self.embed_model,
78
  ],
79
  vector_store=self.vector_store,
80
  )
81
+ pipeline.run(documents=self.documents, show_progress=True)
82
  self.index = VectorStoreIndex.from_vector_store(self.vector_store)
83
 
84
 
85
+ class AwesumCareToolChatbot(Chatbot):
86
  DENIED_ANSWER_PROMPT = ""
87
  SYSTEM_PROMPT = ""
88
  CHAT_EXAMPLES = [
 
131
  # text_qa_template=CHAT_TEXT_QA_PROMPT)
132
  super()._setup_chat_engine()
133
 
134
+ class AweSumCareContextChatbot(AwesumCareToolChatbot):
135
+ def _setup_query_engine(self):
136
+ pass
137
+ def _setup_tools(self):
138
+ pass
139
+ def _setup_chat_engine(self):
140
+ self.chat_engine = self.index.as_chat_engine(
141
+ chat_mode=ChatMode.CONTEXT,
142
+ similarity_top_k=5,
143
+ text_qa_template=CHAT_TEXT_QA_PROMPT)
144
+
145
+ class AweSumCareSimpleChatbot(AwesumCareToolChatbot):
146
+ def _setup_query_engine(self):
147
+ pass
148
+ def _setup_tools(self):
149
+ pass
150
+ def _setup_chat_engine(self):
151
+ self.chat_engine = self.index.as_chat_engine(
152
+ chat_mode=ChatMode.SIMPLE)
153
+
154
+ model_name = MODEL_NAME
155
+ index_builder = AwesumIndexBuilder(vdb_collection_name=VDB_COLLECTION_NAME,
156
+ embed_model=EMBED_MODEL,
157
+ is_load_from_vector_store=IS_LOAD_FROM_VECTOR_STORE)
158
 
159
  # gpt-3.5-turbo-1106, gpt-4-1106-preview
160
+ awesum_chatbot = AwesumCareToolChatbot(model_name=model_name, index_builder=index_builder)
161
+ awesum_chatbot_context = AweSumCareContextChatbot(model_name=model_name, index_builder=index_builder)
162
+ awesum_chatbot_simple = AweSumCareSimpleChatbot(model_name=model_name, index_builder=index_builder)
163
+
164
+
165
+ def service_setup(model_name):
166
+ CHUNK_SIZE = 1024
167
+ LLM, EMBED_MODEL = get_service_provider_config(
168
+ service_provider=ServiceProvider.OPENAI, model_name=model_name)
169
+ service_context = ServiceContext.from_defaults(
170
+ chunk_size=CHUNK_SIZE,
171
+ llm=LLM,
172
+ embed_model=EMBED_MODEL,
173
+ )
174
+ set_global_service_context(service_context)
175
+ return LLM, EMBED_MODEL
176
 
177
 
178
  def vote(data: gr.LikeData):
 
185
  chatbot = gr.Chatbot()
186
 
187
  with gr.Blocks() as demo:
188
+
189
  gr.Markdown("# Awesum Care demo")
190
 
191
+ # with gr.Row():
192
+ # model_selector = gr.Radio(
193
+ # value=ChatbotVersion.CHATGPT_35.value,
194
+ # choices=[ChatbotVersion.CHATGPT_35.value, ChatbotVersion.CHATGPT_4.value],
195
+ # label="Select Chatbot Model (To be implemented)"
196
+ # )
197
+
198
+ with gr.Tab("With context aware"):
199
+ context_interface = gr.ChatInterface(
200
+ awesum_chatbot_context.stream_chat,
201
+ examples=awesum_chatbot.CHAT_EXAMPLES,
202
+ )
203
+ chatbot.like(vote, None, None)
204
+
205
+ with gr.Tab("With function calling as tool to retrieve"):
206
+ function_call_interface = gr.ChatInterface(
207
  awesum_chatbot.stream_chat,
 
208
  examples=awesum_chatbot.CHAT_EXAMPLES,
209
  )
210
  chatbot.like(vote, None, None)
211
 
 
 
 
212
 
213
  with gr.Tab("Vanilla ChatGPT without modification"):
214
+ vanilla_interface = gr.ChatInterface(
215
+ awesum_chatbot_simple.stream_chat,
216
+ examples=awesum_chatbot.CHAT_EXAMPLES)
217
+
218
+ # @model_selector.change(inputs=[model_selector, chatbot], outputs=[context_interface, function_call_interface, vanilla_interface])
219
+ # def switch_model(model_name, my_chatbot):
220
+ # print(model_name)
221
+ # print(my_chatbot.config())
222
+ # LLM, EMBED_MODEL = service_setup(model_name)
223
+ # # global awesum_chatbot, awesum_chatbot_context, awesum_chatbot_simple
224
+ # # Logic to switch models - create new instances of the chatbots with the selected model
225
+ # index_builder = AwesumIndexBuilder(vdb_collection_name=VDB_COLLECTION_NAME,
226
+ # embed_model=EMBED_MODEL,
227
+ # is_load_from_vector_store=IS_LOAD_FROM_VECTOR_STORE)
228
+ # awesum_chatbot = AwesumCareToolChatbot(model_name=model_name, index_builder=index_builder, llm=LLM)
229
+ # awesum_chatbot_context = AweSumCareContextChatbot(model_name=model_name, index_builder=index_builder)
230
+ # awesum_chatbot_simple = AweSumCareSimpleChatbot(model_name=model_name, index_builder=index_builder)
231
+ # # return awesum_chatbot.stream_chat, awesum_chatbot_context.stream_chat, awesum_chatbot_simple.stream_chat
232
+
233
+ # new_context_interface = gr.ChatInterface(
234
+ # awesum_chatbot_context.stream_chat,
235
+ # )
236
+ # new_function_call_interface = gr.ChatInterface(
237
+ # awesum_chatbot.stream_chat,
238
+ # )
239
+ # new_vanilla_interface = gr.ChatInterface(
240
+ # awesum_chatbot_simple.stream_chat,
241
+ # )
242
+ # return new_context_interface, new_function_call_interface, new_vanilla_interface
243
+
244
 
245
  demo.queue()
246
+ demo.launch(share=False)
chatbot.py CHANGED
@@ -1,4 +1,5 @@
1
  from enum import Enum
 
2
  from typing import List
3
  import os
4
  import re
@@ -8,15 +9,16 @@ from openai import OpenAI
8
  import phoenix as px
9
  import llama_index
10
  from llama_index import OpenAIEmbedding
11
- from llama_index.llms import ChatMessage, MessageRole, OpenAI
12
 
13
  load_dotenv()
14
 
15
 
16
  class IndexBuilder:
17
- def __init__(self, vdb_collection_name, is_load_from_vector_store=False):
18
  self.documents = None
19
  self.vdb_collection_name = vdb_collection_name
 
20
  self.index = None
21
  self.is_load_from_vector_store = is_load_from_vector_store
22
  self.build_index()
@@ -52,9 +54,10 @@ class Chatbot:
52
  DENIED_ANSWER_PROMPT = ""
53
  CHAT_EXAMPLES = []
54
 
55
- def __init__(self, model_name, index_builder: IndexBuilder):
56
  self.model_name = model_name
57
  self.index_builder = index_builder
 
58
 
59
  self.documents = None
60
  self.index = None
@@ -63,8 +66,23 @@ class Chatbot:
63
  self.vector_store = None
64
  self.tools = None
65
 
 
66
  self._setup_chatbot()
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  def _setup_chatbot(self):
69
  # self._setup_observer()
70
  self._setup_index()
@@ -97,8 +115,8 @@ class Chatbot:
97
  print("Setup chat engine...")
98
 
99
  def stream_chat(self, message, history):
100
- print(history)
101
- print(self.convert_to_chat_messages(history))
102
  response = self.chat_engine.stream_chat(
103
  message, chat_history=self.convert_to_chat_messages(history)
104
  )
@@ -145,15 +163,17 @@ class Chatbot:
145
  {"role": "assistant", "content": assistant})
146
  history_openai_format.append({"role": "user", "content": message})
147
 
148
- import openai
149
- print(openai.__version__)
150
  stream = openai_client.chat.completions.create(
151
  model=self.model_name,
152
  messages=history_openai_format,
153
  temperature=1.0,
154
  stream=True)
 
 
155
  for part in stream:
156
- yield part.choices[0].delta.content or ""
 
 
157
  # partial_message = ""
158
  # for chunk in response:
159
  # if len(chunk["choices"][0]["delta"]) != 0:
 
1
  from enum import Enum
2
+ import logging
3
  from typing import List
4
  import os
5
  import re
 
9
  import phoenix as px
10
  import llama_index
11
  from llama_index import OpenAIEmbedding
12
+ from llama_index.llms import ChatMessage, MessageRole
13
 
14
  load_dotenv()
15
 
16
 
17
  class IndexBuilder:
18
+ def __init__(self, vdb_collection_name, embed_model, is_load_from_vector_store=False):
19
  self.documents = None
20
  self.vdb_collection_name = vdb_collection_name
21
+ self.embed_model = embed_model
22
  self.index = None
23
  self.is_load_from_vector_store = is_load_from_vector_store
24
  self.build_index()
 
54
  DENIED_ANSWER_PROMPT = ""
55
  CHAT_EXAMPLES = []
56
 
57
+ def __init__(self, model_name, index_builder: IndexBuilder, llm=None):
58
  self.model_name = model_name
59
  self.index_builder = index_builder
60
+ self.llm = llm
61
 
62
  self.documents = None
63
  self.index = None
 
66
  self.vector_store = None
67
  self.tools = None
68
 
69
+ self._setup_logger()
70
  self._setup_chatbot()
71
 
72
+
73
+ def _setup_logger(self):
74
+ logs_dir = 'logs'
75
+ if not os.path.exists(logs_dir):
76
+ os.makedirs(logs_dir) # Step 3: Create logs directory
77
+
78
+ logging.basicConfig(
79
+ filename=os.path.join(logs_dir, 'chatbot.log'),
80
+ filemode='a',
81
+ format='%(asctime)s - %(levelname)s - %(message)s',
82
+ level=logging.INFO
83
+ )
84
+ self.logger = logging.getLogger(__name__)
85
+
86
  def _setup_chatbot(self):
87
  # self._setup_observer()
88
  self._setup_index()
 
115
  print("Setup chat engine...")
116
 
117
  def stream_chat(self, message, history):
118
+ self.logger.info(history)
119
+ self.logger.info(self.convert_to_chat_messages(history))
120
  response = self.chat_engine.stream_chat(
121
  message, chat_history=self.convert_to_chat_messages(history)
122
  )
 
163
  {"role": "assistant", "content": assistant})
164
  history_openai_format.append({"role": "user", "content": message})
165
 
 
 
166
  stream = openai_client.chat.completions.create(
167
  model=self.model_name,
168
  messages=history_openai_format,
169
  temperature=1.0,
170
  stream=True)
171
+
172
+ partial_message = ""
173
  for part in stream:
174
+ partial_message += part.choices[0].delta.content or ""
175
+ yield partial_message
176
+ # yield part.choices[0].delta.content or ""
177
  # partial_message = ""
178
  # for chunk in response:
179
  # if len(chunk["choices"][0]["delta"]) != 0:
qdrant.py CHANGED
@@ -1,5 +1,10 @@
 
 
1
 
2
- import qdrant_client
3
 
 
4
 
5
- client = qdrant_client.QdrantClient(path="/tmp/total_qdrant/")
 
 
 
 
1
+ # import qdrant_client
2
+ from qdrant_client import QdrantClient
3
 
 
4
 
5
+ # client = qdrant_client.QdrantClient(path="/tmp/total_qdrant/")
6
 
7
+ client = QdrantClient(
8
+ url="https://dc059edb-e5bc-43ff-bb72-756cc610d6d1.us-east4-0.gcp.cloud.qdrant.io",
9
+ api_key="th0__GAZkpGRP0QSL2QwB0g_vr0ATvY-sd2Gre5VBUk8-vExdsYsYQ"
10
+ )
service_provider_config.py CHANGED
@@ -7,11 +7,11 @@ from schemas import ServiceProvider, ChatbotVersion
7
 
8
  load_dotenv()
9
 
10
- def get_service_provider_config(service_provider: ServiceProvider):
11
  if service_provider == ServiceProvider.AZURE:
12
  return get_azure_openai_config()
13
  if service_provider == ServiceProvider.OPENAI:
14
- llm = OpenAI(model=ChatbotVersion.CHATGPT_35)
15
  embed_model = OpenAIEmbedding()
16
  return llm, embed_model
17
 
 
7
 
8
  load_dotenv()
9
 
10
+ def get_service_provider_config(service_provider: ServiceProvider, model_name: str=ChatbotVersion.CHATGPT_35.value):
11
  if service_provider == ServiceProvider.AZURE:
12
  return get_azure_openai_config()
13
  if service_provider == ServiceProvider.OPENAI:
14
+ llm = OpenAI(model=model_name)
15
  embed_model = OpenAIEmbedding()
16
  return llm, embed_model
17