bstraehle commited on
Commit
33156e9
1 Parent(s): eb6ca55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -17
app.py CHANGED
@@ -47,10 +47,8 @@ template = """If you don't know the answer, just say that you don't know, don't
47
  llm_template = "Answer the question at the end. " + template + "Question: {question} Helpful Answer: "
48
  rag_template = "Use the following pieces of context to answer the question at the end. " + template + "{context} Question: {question} Helpful Answer: "
49
 
50
- LLM_CHAIN_PROMPT = PromptTemplate(input_variables = ["question"],
51
- template = llm_template)
52
- RAG_CHAIN_PROMPT = PromptTemplate(input_variables = ["context", "question"],
53
- template = rag_template)
54
 
55
  CHROMA_DIR = "/data/chroma"
56
  YOUTUBE_DIR = "/data/youtube"
@@ -118,10 +116,10 @@ def rag_chain(llm, prompt, db):
118
  completion = rag_chain({"query": prompt})
119
  return completion
120
 
121
- def wandb_trace(rag_option, prompt, prompt_template, result, completion, chain_name, status_msg, start_time_ms, end_time_ms):
122
  wandb.init(project = "openai-llm-rag")
123
  trace = Trace(
124
- name = chain_name,
125
  kind = "chain",
126
  status_code = "SUCCESS" if (str(status_msg) == "") else "ERROR",
127
  status_message = str(status_msg),
@@ -134,8 +132,8 @@ def wandb_trace(rag_option, prompt, prompt_template, result, completion, chain_n
134
  },
135
  start_time_ms = start_time_ms,
136
  end_time_ms = end_time_ms,
137
- inputs = {"rag_option": rag_option, "prompt": str(prompt), "prompt_template": str(prompt_template)},
138
- outputs = {"result": str(result), "completion": str(completion)},
139
  #model_dict = {"llm": str(llm)}
140
  )
141
  trace.log("test")
@@ -150,8 +148,6 @@ def invoke(openai_api_key, rag_option, prompt):
150
  raise gr.Error("Prompt is required.")
151
  completion = ""
152
  result = ""
153
- prompt_template = ""
154
- chain_name = ""
155
  status_msg = ""
156
  try:
157
  start_time_ms = round(time.time() * 1000)
@@ -164,26 +160,20 @@ def invoke(openai_api_key, rag_option, prompt):
164
  db = document_retrieval_chroma(llm, prompt)
165
  completion = rag_chain(llm, prompt, db)
166
  result = completion["result"]
167
- prompt_template = rag_template
168
- chain_name = "RetrievalQA"
169
  elif (rag_option == "MongoDB"):
170
  #splits = document_loading_splitting()
171
  #document_storage_mongodb(splits)
172
  db = document_retrieval_mongodb(llm, prompt)
173
  completion = rag_chain(llm, prompt, db)
174
  result = completion["result"]
175
- prompt_template = rag_template
176
- chain_name = "RetrievalQA"
177
  else:
178
  result = llm_chain(llm, prompt)
179
- prompt_template = llm_template
180
- chain_name = "LLMChain"
181
  except Exception as e:
182
  status_msg = e
183
  raise gr.Error(e)
184
  finally:
185
  end_time_ms = round(time.time() * 1000)
186
- wandb_trace(rag_option, prompt, prompt_template, result, completion, chain_name, status_msg, start_time_ms, end_time_ms)
187
  return result
188
 
189
  gr.close_all()
 
47
  llm_template = "Answer the question at the end. " + template + "Question: {question} Helpful Answer: "
48
  rag_template = "Use the following pieces of context to answer the question at the end. " + template + "{context} Question: {question} Helpful Answer: "
49
 
50
+ LLM_CHAIN_PROMPT = PromptTemplate(input_variables = ["question"], template = llm_template)
51
+ RAG_CHAIN_PROMPT = PromptTemplate(input_variables = ["context", "question"], template = rag_template)
 
 
52
 
53
  CHROMA_DIR = "/data/chroma"
54
  YOUTUBE_DIR = "/data/youtube"
 
116
  completion = rag_chain({"query": prompt})
117
  return completion
118
 
119
+ def wandb_trace(rag_option, prompt, completion, status_msg, start_time_ms, end_time_ms):
120
  wandb.init(project = "openai-llm-rag")
121
  trace = Trace(
122
+ name = "LLMChain" if (rag_option == "Off") else "RetrievalQA",
123
  kind = "chain",
124
  status_code = "SUCCESS" if (str(status_msg) == "") else "ERROR",
125
  status_message = str(status_msg),
 
132
  },
133
  start_time_ms = start_time_ms,
134
  end_time_ms = end_time_ms,
135
+ inputs = {"rag_option": rag_option, "prompt": str(prompt), "prompt_template": str(llm_template if (rag_option == "Off") else rag_template)},
136
+ outputs = {"completion": str(completion)},
137
  #model_dict = {"llm": str(llm)}
138
  )
139
  trace.log("test")
 
148
  raise gr.Error("Prompt is required.")
149
  completion = ""
150
  result = ""
 
 
151
  status_msg = ""
152
  try:
153
  start_time_ms = round(time.time() * 1000)
 
160
  db = document_retrieval_chroma(llm, prompt)
161
  completion = rag_chain(llm, prompt, db)
162
  result = completion["result"]
 
 
163
  elif (rag_option == "MongoDB"):
164
  #splits = document_loading_splitting()
165
  #document_storage_mongodb(splits)
166
  db = document_retrieval_mongodb(llm, prompt)
167
  completion = rag_chain(llm, prompt, db)
168
  result = completion["result"]
 
 
169
  else:
170
  result = llm_chain(llm, prompt)
 
 
171
  except Exception as e:
172
  status_msg = e
173
  raise gr.Error(e)
174
  finally:
175
  end_time_ms = round(time.time() * 1000)
176
+ wandb_trace(rag_option, prompt, completion, status_msg, start_time_ms, end_time_ms)
177
  return result
178
 
179
  gr.close_all()