Skip to content
This repository was archived by the owner on Nov 13, 2023. It is now read-only.

Commit 5bf31ef

Browse files
authored
Merge pull request #9 from thoraxe/extend-question-validator
enhances the question validator
2 parents 7b0c770 + a5c794c commit 5bf31ef

File tree

2 files changed

+62
-25
lines changed

2 files changed

+62
-25
lines changed

modules/question_validator.py

+22-10
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,32 @@ def validate_question(self, conversation, query, **kwargs):
5454
- You are a question classifying tool
5555
- You are an expert in kubernetes and openshift
5656
- Your job is to determine if a question is about kubernetes or openshift and to provide a one word response
57-
- If a question is not about kubernetes or openshift, answer with only the word "INVALID"
58-
- If a question is about kubernetes or openshift, answer with only the word "VALID"
59-
- Do not provide explanation, only respond with the single chosen word
57+
- If a question is not about kubernetes or openshift, answer with only the word INVALID
58+
- If a question is about kubernetes or openshift, answer with the word VALID
59+
- If a question is not about creating kubernetes or openshift yaml, answer with the word NOYAML
60+
- If a question is about creating kubernetes or openshift yaml, add the word YAML
61+
- Use a comma to separate the words
62+
- Do not provide explanation, only respond with the chosen words
6063
6164
Example Question:
6265
Can you make me lunch with ham and cheese?
6366
Example Response:
64-
INVALID
67+
INVALID,NOYAML
6568
6669
Example Question:
6770
Why is the sky blue?
6871
Example Response:
69-
INVALID
72+
INVALID,NOYAML
7073
7174
Example Question:
7275
Can you help configure my cluster to automatically scale?
7376
Example Response:
74-
VALID
77+
VALID,NOYAML
78+
79+
Example Question:
80+
please give me a vertical pod autoscaler configuration to manage my frontend deployment automatically. Don't update the workload if there are less than 2 pods running.
81+
Example Response:
82+
VALID,YAML
7583
7684
Question:
7785
{query}
@@ -82,19 +90,23 @@ def validate_question(self, conversation, query, **kwargs):
8290
self.logger.info(conversation + " Validating query")
8391
self.logger.info(conversation + " usng model: " + model)
8492

85-
bare_llm = get_watsonx_predictor(model=model, min_new_tokens=1, max_new_tokens=2)
93+
bare_llm = get_watsonx_predictor(model=model, min_new_tokens=1, max_new_tokens=4)
8694
llm_chain = LLMChain(llm=bare_llm, prompt=prompt_instructions, verbose=verbose)
8795

8896
task_query = prompt_instructions.format(query=query)
8997

9098
self.logger.info(conversation + " task query: " + task_query)
9199

92100
response = llm_chain(inputs={"query": query})
101+
clean_response = str(response['text']).strip()
93102

94-
self.logger.info(conversation + " response: " + str(response))
103+
self.logger.info(conversation + " response: " + clean_response)
95104

96-
# should only return "VALID" or "INVALID"
97-
return response['text'].strip()
105+
# will return an array:
106+
# [INVALID,NOYAML]
107+
# [VALID,NOYAML]
108+
# [VALID,YAML]
109+
return clean_response.split(",")
98110

99111
if __name__ == "__main__":
100112
"""to execute, from the repo root, use python -m modules.question_validator.py"""

ols.py

+40-15
Original file line numberDiff line numberDiff line change
@@ -120,25 +120,50 @@ def ols2_request(llm_request: LLMRequest):
120120
question_validator = QuestionValidator()
121121

122122
is_valid = question_validator.validate_question(conversation, llm_request.query)
123-
if is_valid == "INVALID":
123+
if is_valid[0] == "INVALID":
124+
logging.info(conversation + " question was determined to not be k8s/ocp, so rejecting")
124125
llm_response.response = ("Sorry, I can only answer questions about "
125126
"OpenShift and Kubernetes. This does not look "
126127
"like something I know how to handle.")
127128
raise HTTPException(status_code=422, detail=llm_response.dict())
128-
if is_valid == "VALID":
129-
# the LLM thought the question was valid, so pass it to the YAML generator
130-
yaml_generator = YamlGenerator()
131-
generated_yaml = yaml_generator.generate_yaml(conversation, llm_request.query)
132-
133-
# TODO: raise an exception on a failure of the yaml generator
134-
135-
# filter/clean/lint the YAML response
136-
137-
# RAG for supporting documentation
138-
139-
# generate a user-friendly response to wrap the YAML and/or the supporting information
140-
llm_response.response = generated_yaml
141-
return llm_response
129+
if is_valid[0] == "VALID":
130+
logging.info(conversation + " question is about k8s/ocp")
131+
# the LLM thought the question was valid, so decide if it's about YAML or not
132+
133+
if is_valid[1] == "NOYAML":
134+
logging.info(conversation + " question is not about yaml, so send for generic info")
135+
llm_response.response = "Documentation-based response here"
136+
return llm_response
137+
elif is_valid[1] == "YAML":
138+
logging.info(conversation + " question is about yaml, so send to the YAML generator")
139+
yaml_generator = YamlGenerator()
140+
generated_yaml = yaml_generator.generate_yaml(conversation, llm_request.query)
141+
142+
if generated_yaml == "some failure":
143+
# we didn't get any kind of yaml markdown block back from the model
144+
llm_response.response = (
145+
"Sorry, something bad happened internally. Please try again."
146+
)
147+
raise HTTPException(status_code=500, detail=llm_response.dict())
148+
149+
# we got some kind of valid yaml back from the yaml generator, so proceed
150+
151+
# filter/clean/lint the YAML response
152+
153+
# RAG for supporting documentation
154+
155+
# generate a user-friendly response to wrap the YAML and/or the supporting information
156+
llm_response.response = generated_yaml
157+
158+
159+
return llm_response
160+
else:
161+
# something weird happened, so generate an internal error
162+
# something bad happened with the validation
163+
llm_response.response = (
164+
"Sorry, something bad happened internally. Please try again."
165+
)
166+
raise HTTPException(status_code=500, detail=llm_response.dict())
142167
else:
143168
# something bad happened with the validation
144169
llm_response.response = (

0 commit comments

Comments
 (0)