Oriol Ganduxé Pregona commited on
Commit
f5c4253
·
1 Parent(s): 7d0f65e

Layout changes. Adding next textbox to allow specific index fetching.

Browse files
Files changed (1) hide show
  1. code_qa_app.py +58 -53
code_qa_app.py CHANGED
@@ -11,6 +11,8 @@ from langchain.vectorstores import Chroma
11
  from langchain.chat_models import ChatOpenAI
12
  from typing import List
13
  from langchain.vectorstores import Chroma
 
 
14
  import panel as pn
15
 
16
  # UI configuration
@@ -46,7 +48,14 @@ openaikey = pn.widgets.PasswordInput(
46
  width=300
47
  )
48
 
49
- prompt = pn.widgets.TextEditor(
 
 
 
 
 
 
 
50
  value="",
51
  placeholder="Enter your questions here...",
52
  height=200,
@@ -68,21 +77,21 @@ select_chain_type = pn.widgets.RadioButtonGroup(
68
  options=['stuff', 'map_reduce', "refine", "map_rerank"]
69
  )
70
 
71
- # class HardcodedRetriever(BaseRetriever):
72
- # def __init__(self, documents: [Document]):
73
- # self.documents = documents
74
 
75
- # def get_relevant_documents(self, query: str) -> List[Document]:
76
- # # Ignoring query
77
- # return self.documents
78
 
79
- # async def aget_relevant_documents(self, query: str) -> List[Document]:
80
- # # Ignoring query
81
- # return self.documents
82
 
83
 
84
  # Question answering function
85
- def qa(url_repo, branch, query, chain_type, k):
86
  # load data
87
  loader = GitLoader(
88
  clone_url=url_repo,
@@ -102,44 +111,32 @@ def qa(url_repo, branch, query, chain_type, k):
102
  # create the vectorstore to use as the index
103
  db = Chroma.from_documents(texts, embeddings)
104
 
105
- # expose this index in a retriever interface
106
- retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
107
-
108
- # create a chain to answer questions
109
- qa = RetrievalQA.from_chain_type(
110
- llm=OpenAI(),
111
- chain_type=chain_type,
112
- retriever=retriever,
113
- return_source_documents=True
114
- )
115
-
116
- result = qa({"query": query})
117
- print(result['result'])
118
- return result
119
 
120
  # Question answering function
121
- def qa_cached_tuist(query, chain_type, k):
122
 
123
- # select which embeddings to use
124
- embeddings = OpenAIEmbeddings()
125
 
126
- # create the vectorstore to use as the index
127
- persist_directory = "tuist_cached_db/"
128
- db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
129
 
130
- # llm=OpenAI()
131
- llm = ChatOpenAI(
132
- model_name = "gpt-3.5-turbo",
133
- temperature=0.7,
134
- max_tokens=1000)
135
 
136
- # You can test how an empty retriever makes our LLM ignorant about the context
137
- # hardcodedRetriever = HardcodedRetriever([])
 
 
 
138
 
139
- # qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=hardcodedRetriever)
 
 
140
 
141
- # expose this index in a retriever interface
142
- retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
 
 
 
143
 
144
  # create a chain to answer questions
145
  qa = RetrievalQA.from_chain_type(
@@ -149,24 +146,27 @@ def qa_cached_tuist(query, chain_type, k):
149
  return_source_documents=True
150
  )
151
 
152
- result = qa({"query": query})
153
  print(result['result'])
154
  return result
155
 
 
 
156
  convos = [] # store all panel objects in a list
157
 
158
  def qa_result(_):
159
  os.environ["OPENAI_API_KEY"] = openaikey.value
160
 
161
- prompt_text = prompt.value
 
162
  if prompt_text:
163
 
164
  convos.clear()
165
 
166
  if use_cached_repo.value:
167
- result = qa_cached_tuist(query=prompt_text, chain_type=select_chain_type.value, k=select_k.value)
168
  else:
169
- result = qa(url_repo=url_repo.value, branch=branch_repo.value, query=prompt_text, chain_type=select_chain_type.value, k=select_k.value)
170
 
171
  convos.extend([
172
  pn.Row(
@@ -207,12 +207,8 @@ def toggle_github_inputs(event):
207
 
208
  use_cached_repo.param.watch(toggle_github_inputs, 'value')
209
 
210
- widgets = pn.Row(
211
- pn.Column(
212
- prompt,
213
- run_button,
214
- margin=5
215
- ),
216
  pn.Card(
217
  "Chain type:",
218
  pn.Column(
@@ -220,6 +216,15 @@ widgets = pn.Row(
220
  select_k
221
  ),
222
  title="Advanced settings", margin=10
 
 
 
 
 
 
 
 
 
223
  ), width=600
224
  )
225
 
@@ -232,8 +237,8 @@ pn.Column(
232
  3) Type a question and click "Run".\n
233
 
234
  """),
235
- pn.Column(github_inputs, openaikey),
236
- widgets,
237
  output,
238
 
239
  ).servable()
 
11
  from langchain.chat_models import ChatOpenAI
12
  from typing import List
13
  from langchain.vectorstores import Chroma
14
+ from langchain.docstore.document import Document
15
+ from langchain.schema import BaseRetriever
16
  import panel as pn
17
 
18
  # UI configuration
 
48
  width=300
49
  )
50
 
51
+ context_texteditor = pn.widgets.TextEditor(
52
+ value="",
53
+ placeholder="(Optional) Enter your index query here...",
54
+ height=200,
55
+ toolbar=False
56
+ )
57
+
58
+ prompt_texteditor = pn.widgets.TextEditor(
59
  value="",
60
  placeholder="Enter your questions here...",
61
  height=200,
 
77
  options=['stuff', 'map_reduce', "refine", "map_rerank"]
78
  )
79
 
80
+ class FixedRetriever(BaseRetriever):
81
+ def __init__(self, documents: [Document]):
82
+ self.documents = documents
83
 
84
+ def get_relevant_documents(self, query: str) -> List[Document]:
85
+ # Ignoring query
86
+ return self.documents
87
 
88
+ async def aget_relevant_documents(self, query: str) -> List[Document]:
89
+ # Ignoring query
90
+ return self.documents
91
 
92
 
93
  # Question answering function
94
+ def qa_fetching_repo(url_repo, branch, context_query, prompt_query, chain_type, k):
95
  # load data
96
  loader = GitLoader(
97
  clone_url=url_repo,
 
111
  # create the vectorstore to use as the index
112
  db = Chroma.from_documents(texts, embeddings)
113
 
114
+ return qa(db, context_query, prompt_query, chain_type, k)
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  # Question answering function
117
+ def qa_cached_tuist(context_query, prompt_query, chain_type, k):
118
 
119
+ db = Chroma(persist_directory="tuist_cached_db/", embedding_function=OpenAIEmbeddings())
 
120
 
121
+ return qa(db, context_query, prompt_query, chain_type, k)
 
 
122
 
123
+ def qa(db, context_query, prompt_query, chain_type, k):
 
 
 
 
124
 
125
+ if context_query.strip():
126
+ docs = db.similarity_search(context_query, k=k)
127
+ retriever = FixedRetriever(docs)
128
+ # You can test how an empty retriever makes our LLM ignorant about the context
129
+ # retriever = FixedRetriever([])
130
 
131
+ else:
132
+ # expose this index in a retriever interface
133
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
134
 
135
+ llm=OpenAI()
136
+ # llm = ChatOpenAI(
137
+ # model_name = "gpt-3.5-turbo",
138
+ # temperature=0.7,
139
+ # max_tokens=1000)
140
 
141
  # create a chain to answer questions
142
  qa = RetrievalQA.from_chain_type(
 
146
  return_source_documents=True
147
  )
148
 
149
+ result = qa({"query": prompt_query})
150
  print(result['result'])
151
  return result
152
 
153
+
154
+
155
  convos = [] # store all panel objects in a list
156
 
157
  def qa_result(_):
158
  os.environ["OPENAI_API_KEY"] = openaikey.value
159
 
160
+ prompt_text = prompt_texteditor.value
161
+ context_text = context_texteditor.value
162
  if prompt_text:
163
 
164
  convos.clear()
165
 
166
  if use_cached_repo.value:
167
+ result = qa_cached_tuist(context_query=context_text, prompt_query=prompt_text, chain_type=select_chain_type.value, k=select_k.value)
168
  else:
169
+ result = qa_fetching_repo(url_repo=url_repo.value, branch=branch_repo.value, context_query=context_text, prompt_query=prompt_text, chain_type=select_chain_type.value, k=select_k.value)
170
 
171
  convos.extend([
172
  pn.Row(
 
207
 
208
  use_cached_repo.param.watch(toggle_github_inputs, 'value')
209
 
210
+ setup = pn.Row(
211
+ pn.Column(github_inputs, openaikey),
 
 
 
 
212
  pn.Card(
213
  "Chain type:",
214
  pn.Column(
 
216
  select_k
217
  ),
218
  title="Advanced settings", margin=10
219
+ )
220
+ )
221
+
222
+ queries = pn.Row(
223
+ pn.Column(
224
+ context_texteditor,
225
+ prompt_texteditor,
226
+ run_button,
227
+ margin=5
228
  ), width=600
229
  )
230
 
 
237
  3) Type a question and click "Run".\n
238
 
239
  """),
240
+ setup,
241
+ queries,
242
  output,
243
 
244
  ).servable()