Skip to content

Commit

Permalink
Fixed structured output parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian-Winter committed Jun 6, 2024
1 parent 08018a5 commit f7b087d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/llama_cpp_agent/llm_output_settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,11 @@ def add_all_current_functions_to_heartbeat_list(self, excluded: list[str] = None
def handle_structured_output(self, llm_output: str, prompt_suffix: str = None):
if self.output_raw_json_string:
return llm_output

if prompt_suffix:
llm_output = llm_output.replace(prompt_suffix, "", 1)


if (
self.output_type is LlmStructuredOutputType.function_calling
or self.output_type is LlmStructuredOutputType.parallel_function_calling
Expand All @@ -627,7 +630,7 @@ def handle_structured_output(self, llm_output: str, prompt_suffix: str = None):

return self.handle_function_call(output)
elif self.output_type == LlmStructuredOutputType.object_instance:
output = json.loads(llm_output)
output = parse_json_response(llm_output)
output = self.clean_keys(output)
model_name = output[self.output_model_name_field_name]
model_attributes = output[self.output_model_attributes_field_name]
Expand All @@ -636,7 +639,7 @@ def handle_structured_output(self, llm_output: str, prompt_suffix: str = None):
return model(**model_attributes)

elif self.output_type == LlmStructuredOutputType.list_of_objects:
output = json.loads(llm_output)
output = parse_json_response(llm_output)
output = self.clean_keys(output)
models = []
for out in output:
Expand Down
20 changes: 9 additions & 11 deletions src/llama_cpp_agent/prompt_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,21 @@
{after_system_instructions}
Your task is to output a JSON object containing the content of a specific model, based on the system instructions provided. Your output should be structured in the following way:
{thoughts_and_reasoning}
The {model_field_name} field should contain the name of the specific model that you are outputting, based on the system instructions.
The {fields_field_name} field should contain the actual fields and content of the model you are outputting, filled out according to the system instructions.
Here are the output models you can choose from:
Your output should be structured as JSON and represent one of the following output models:
<output_models>
{output_models}
</output_models>
Please read the system instructions and output models carefully before proceeding.'''
You JSON output should have the following fields:
{thoughts_and_reasoning}
The '{model_field_name}' field should contain the name of the specific model that you are outputting, based on the system instructions.
The '{fields_field_name}' field should contain the actual fields and content of the model you are outputting, filled out according to the system instructions.'''

thoughts_and_reasoning_structured_output = "The {thoughts_and_reasoning_field_name} field should contain your step-by-step reasoning and decision making process as you work through the task. Explain how you are interpreting the instructions and planning your response."
thoughts_and_reasoning_structured_output = "The '{thoughts_and_reasoning_field_name}' field should contain your step-by-step reasoning and decision making process as you work through the task. Explain how you are interpreting the instructions and planning your response."
structured_output_templater = PromptTemplate.from_string(structured_output_template)
structured_output_thoughts_and_reasoning_templater = PromptTemplate.from_string(thoughts_and_reasoning_structured_output)
general_summarizing_system_prompt = """Your task is to summarize and extract relevant information from a text based on a specific query.
Expand Down

0 comments on commit f7b087d

Please sign in to comment.