BhanuPrakashSamoju commited on
Commit
72c8535
·
1 Parent(s): 1209a38

Update Index.py

Browse files
Files changed (1) hide show
  1. Index.py +79 -1
Index.py CHANGED
@@ -1,4 +1,5 @@
1
  from fastapi import FastAPI
 
2
 
3
  # from transformers import pipeline
4
  from txtai.embeddings import Embeddings
@@ -21,6 +22,8 @@ import os
21
  app = FastAPI(docs_url="/")
22
  # app = FastAPI()
23
 
 
 
24
  # pipe = pipeline("text2text-generation", model="google/flan-t5-small")
25
 
26
 
@@ -224,6 +227,79 @@ def _search(query, extractor, question=None):
224
  return extractor([("answer", query, _prompt(question), False)])[0][1]
225
 
226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  @app.get("/rag")
228
  def rag(domain: str, question: str):
229
  print()
@@ -252,6 +328,8 @@ def rag(domain: str, question: str):
252
  text = _prompt(question)
253
  text += "\n" + "\n".join(x["text"]for x in embeddings.search(question))
254
  print("Look here for context --> ")
255
- print(text)
 
 
256
 
257
  return {"question": question, "answer": answer}
 
1
  from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
 
4
  # from transformers import pipeline
5
  from txtai.embeddings import Embeddings
 
22
  app = FastAPI(docs_url="/")
23
  # app = FastAPI()
24
 
25
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = "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"
26
+
27
  # pipe = pipeline("text2text-generation", model="google/flan-t5-small")
28
 
29
 
 
227
  return extractor([("answer", query, _prompt(question), False)])[0][1]
228
 
229
 
230
+ class ModelOutputEvaluate(BaseModel):
231
+ question: str
232
+ answer: str
233
+ domain: str
234
+ context: str
235
+
236
+ class BasePromptContext:
237
+ def __init__(self):
238
+ self.variables_list = ["question","answer","context"]
239
+ self.base_template = """Please act as an impartial judge and evaluate the quality of the provided answer which attempts to answer the provided question based on a provided context.
240
+ And you'll need to submit your grading for the correctness, comprehensiveness and readability of the answer, using JSON format with the 2 items in parenthesis:
241
+ ("score": [your score number for the correctness of the answer], "reasoning": [your one line step by step reasoning about the correctness of the answer])
242
+ Below is your grading rubric:
243
+ - Correctness: If the answer correctly answer the question, below are the details for different scores:
244
+ - Score 0: the answer is completely incorrect, doesn’t mention anything about the question or is completely contrary to the correct answer.
245
+ - For example, when asked “How to terminate a databricks cluster”, the answer is empty string, or content that’s completely irrelevant, or sorry I don’t know the answer.
246
+ - Score 4: the answer provides some relevance to the question and answer one aspect of the question correctly.
247
+ - Example:
248
+ - Question: How to terminate a databricks cluster
249
+ - Answer: Databricks cluster is a cloud-based computing environment that allows users to process big data and run distributed data processing tasks efficiently.
250
+ - Or answer: In the Databricks workspace, navigate to the "Clusters" tab. And then this is a hard question that I need to think more about it
251
+ - Score 7: the answer mostly answer the question but is missing or hallucinating on one critical aspect.
252
+ - Example:
253
+ - Question: How to terminate a databricks cluster”
254
+ - Answer: “In the Databricks workspace, navigate to the "Clusters" tab.
255
+ Find the cluster you want to terminate from the list of active clusters.
256
+ And then you’ll find a button to terminate all clusters at once”
257
+ - Score 10: the answer correctly answer the question and not missing any major aspect
258
+ - Example:
259
+ - Question: How to terminate a databricks cluster
260
+ - Answer: In the Databricks workspace, navigate to the "Clusters" tab.
261
+ Find the cluster you want to terminate from the list of active clusters.
262
+ Click on the down-arrow next to the cluster name to open the cluster details.
263
+ Click on the "Terminate" button. A confirmation dialog will appear. Click "Terminate" again to confirm the action.”
264
+ Provided question:
265
+ {question}
266
+ Provided answer:
267
+ {answer}
268
+ Provided context:
269
+ {context}
270
+ Please provide your grading for the correctness and explain you gave the particular grading"""
271
+
272
+
273
+ class Evaluater:
274
+ def __init__(self, item: ModelOutputEvaluate):
275
+ self.question = item.question
276
+ self.answer = item.answer
277
+ self.domain = item.domain
278
+ self.context = item.context
279
+ self.llm=HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":1, "max_length":1000000})
280
+
281
+ def get_prompt_template(self):
282
+ prompt = BasePromptContext()
283
+ template = prompt.base_template
284
+ varialbles = prompt.variables_list
285
+ eval_template = PromptTemplate(input_variables=varialbles, template=template)
286
+ return eval_template
287
+
288
+ def evaluate(self):
289
+ prompt = self.get_prompt_template().format(question = self.question, answer = self.answer, context = self.context)
290
+ score = self.llm(prompt)
291
+ return score
292
+
293
+ # Create extractor instance
294
+ @app.post("/evaluate/")
295
+ async def create_evaluation_scenario(item: ModelOutputEvaluate):
296
+ output = {
297
+ "input": item,
298
+ "score" : Evaluater(item).evaluate()
299
+ }
300
+ return output
301
+
302
+
303
  @app.get("/rag")
304
  def rag(domain: str, question: str):
305
  print()
 
328
  text = _prompt(question)
329
  text += "\n" + "\n".join(x["text"]for x in embeddings.search(question))
330
  print("Look here for context --> ")
331
+ for i in embeddings.search(question):
332
+ print("------------------------------")
333
+ print(text)
334
 
335
  return {"question": question, "answer": answer}