BhanuPrakashSamoju
commited on
Commit
•
538a92e
1
Parent(s):
78ed8c3
Update main.py
Browse files
main.py
CHANGED
@@ -1,5 +1,17 @@
|
|
1 |
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# NOTE - we configure docs_url to serve the interactive Docs at the root path
|
5 |
# of the app. This way, we can use the docs as a landing page for the app on Spaces.
|
@@ -8,16 +20,82 @@ app = FastAPI(docs_url="/")
|
|
8 |
class ModelOutputEvaluate(BaseModel):
|
9 |
question: str
|
10 |
answer: str
|
|
|
11 |
context: str
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Create extractor instance
|
16 |
@app.post("/evaluate/")
|
17 |
async def create_evaluation_scenario(item: ModelOutputEvaluate):
|
18 |
output = {
|
19 |
"input": item,
|
20 |
-
"score" :
|
21 |
}
|
22 |
return output
|
23 |
# def evaluate(question: str):
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
3 |
+
from langchain.embeddings import HuggingFaceEmbeddings #for using HugginFace models
|
4 |
+
from langchain.chains.question_answering import load_qa_chain
|
5 |
+
from langchain.chains.question_answering import load_qa_chain
|
6 |
+
from langchain import HuggingFaceHub
|
7 |
+
from langchain import PromptTemplate
|
8 |
+
|
9 |
+
|
10 |
+
import os
|
11 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "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"
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
|
16 |
# NOTE - we configure docs_url to serve the interactive Docs at the root path
|
17 |
# of the app. This way, we can use the docs as a landing page for the app on Spaces.
|
|
|
20 |
class ModelOutputEvaluate(BaseModel):
|
21 |
question: str
|
22 |
answer: str
|
23 |
+
domain: str
|
24 |
context: str
|
25 |
+
|
26 |
+
class BasePromptContext:
|
27 |
+
def __init__(self):
|
28 |
+
self.variables_list = ["question","answer","context"]
|
29 |
+
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.
|
30 |
+
|
31 |
+
And you'll need to submit your grading for the correctness, comprehensiveness and readability of the answer, using the following format:
|
32 |
+
Reasoning for correctness: [your one line step by step reasoning about the correctness of the answer]
|
33 |
+
Score for correctness: [your score number for the correctness of the answer]
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
Below is your grading rubric:
|
38 |
+
|
39 |
+
- Correctness: If the answer correctly answer the question, below are the details for different scores:
|
40 |
+
- Score 0: the answer is completely incorrect, doesn’t mention anything about the question or is completely contrary to the correct answer.
|
41 |
+
- 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.
|
42 |
+
- Score 4: the answer provides some relevance to the question and answer one aspect of the question correctly.
|
43 |
+
- Example:
|
44 |
+
- Question: How to terminate a databricks cluster
|
45 |
+
- Answer: Databricks cluster is a cloud-based computing environment that allows users to process big data and run distributed data processing tasks efficiently.
|
46 |
+
- 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
|
47 |
+
- Score 7: the answer mostly answer the question but is missing or hallucinating on one critical aspect.
|
48 |
+
- Example:
|
49 |
+
- Question: How to terminate a databricks cluster”
|
50 |
+
- Answer: “In the Databricks workspace, navigate to the "Clusters" tab.
|
51 |
+
Find the cluster you want to terminate from the list of active clusters.
|
52 |
+
And then you’ll find a button to terminate all clusters at once”
|
53 |
+
- Score 10: the answer correctly answer the question and not missing any major aspect
|
54 |
+
- Example:
|
55 |
+
- Question: How to terminate a databricks cluster
|
56 |
+
- Answer: In the Databricks workspace, navigate to the "Clusters" tab.
|
57 |
+
Find the cluster you want to terminate from the list of active clusters.
|
58 |
+
Click on the down-arrow next to the cluster name to open the cluster details.
|
59 |
+
Click on the "Terminate" button. A confirmation dialog will appear. Click "Terminate" again to confirm the action.”
|
60 |
+
|
61 |
+
Provided question:
|
62 |
+
{question}
|
63 |
+
|
64 |
+
Provided answer:
|
65 |
+
{answer}
|
66 |
+
|
67 |
+
Provided context:
|
68 |
+
{context}
|
69 |
+
|
70 |
+
Please provide your grading for the correctness""".format(question = question, answer = answer, context = context)
|
71 |
+
|
72 |
+
|
73 |
+
class Evaluater:
|
74 |
+
def __init__(self, item: ModelOutputEvaluate):
|
75 |
+
self.question = item.question
|
76 |
+
self.answer = item.answer
|
77 |
+
self.domain = item.domain
|
78 |
+
self.context = item.context
|
79 |
+
|
80 |
+
def get_prompt_template(self):
|
81 |
+
prompt = BasePromptContext()
|
82 |
+
template = prompt.base_template
|
83 |
+
varialbles = prompt.variable_list
|
84 |
+
eval_template = PromptTemplate(input_variables=varialbles, template=template)
|
85 |
+
return eval_template
|
86 |
+
|
87 |
+
def evaluate(self):
|
88 |
+
llm=HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":1, "max_length":1000000})
|
89 |
+
prompt = self.get_prompt_template().format(question = self.question, answer = self.answer, context = self.context)
|
90 |
+
score = llm(prompt)
|
91 |
+
return score
|
92 |
|
93 |
# Create extractor instance
|
94 |
@app.post("/evaluate/")
|
95 |
async def create_evaluation_scenario(item: ModelOutputEvaluate):
|
96 |
output = {
|
97 |
"input": item,
|
98 |
+
"score" : Evaluater(item).evaluate()
|
99 |
}
|
100 |
return output
|
101 |
# def evaluate(question: str):
|