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

Commit

Permalink
Improve yaml gen prompt to ensure only single yaml object response
Browse files Browse the repository at this point in the history
  • Loading branch information
bparees committed Nov 10, 2023
1 parent 7f95bc6 commit 17243bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion modules/task_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def process_tasks(self, conversation, tasklist, original_query, **kwargs):
else:
verbose = False

settings_string = f"conversation: {conversation}, query: {original_query},model: {model}, verbose: {verbose}"
settings_string = f"conversation: {conversation}, tasklist: {tasklist}, query: {original_query},model: {model}, verbose: {verbose}"
self.logger.info(
conversation
+ " call settings: "
Expand Down
37 changes: 24 additions & 13 deletions modules/yaml_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ def generate_yaml(self, conversation, string, **kwargs):

bare_llm = get_watsonx_predictor(model=model)

prompt_instructions = PromptTemplate.from_template("{string}\n")
prompt_instructions = PromptTemplate.from_template(
"""Instructions:
- Produce only a yaml response to the user request
- Do not augment the response with markdown or other formatting beyond standard yaml formatting
- Only provide a single yaml object containg a single resource type in your response, do not provide multiple yaml objects
User Request: {string}
"""
)


llm_chain = LLMChain(llm=bare_llm, verbose=verbose, prompt=prompt_instructions)

task_query = prompt_instructions.format(string=string)
Expand All @@ -56,18 +66,19 @@ def generate_yaml(self, conversation, string, **kwargs):
response = llm_chain(inputs={"string": string})

# https://stackoverflow.com/a/63082323/2328066
regex = r"(?:\n+|\A)?(?P<code_block>\`{3,}yaml\n+[\S\s]*\`{3,})"

match = re.search(regex, response["text"])

if match:
clean_response = match.group("code_block")
self.logger.info(conversation + " generated yaml: " + clean_response)
return clean_response
else:
# TODO: need to do the right thing here - raise an exception?
self.logger.error(conversation + " could not parse response:\n"+ response["text"])
return "some failure"
# regex = r"(?:\n+|\A)?(?P<code_block>\`{3,}yaml\n+[\S\s]*\`{3,})"

# match = re.search(regex, response["text"])

# if match:
# clean_response = match.group("code_block")
# self.logger.info(conversation + " generated yaml: " + clean_response)
# return clean_response
# else:
# # TODO: need to do the right thing here - raise an exception?
# self.logger.error(conversation + " could not parse response:\n"+ response["text"])
# return "some failure"
return response["text"]

if __name__ == "__main__":
"""to execute, from the repo root, use python -m modules.yaml_generator"""
Expand Down

0 comments on commit 17243bc

Please sign in to comment.