Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@nkaenzig
Copy link
Collaborator

Closes #921

  • Adds a prompt template that uses an XML style answer format
  • Refactors extraction post processes by introducing a ExtractDiscreteAnswerFromStructuredOutput abstract base class which is implemented by both the JSON & XML extractors.
  • Minor update to JsonMultipleChoicePromptTemplate removing the reason key from the requested JSON format and allowing the models to provide their reasoning outside the structured JSON answer template.

Example

template = XmlMultipleChoicePromptTemplate(use_option_letters=True, enable_cot=True)
prompt = template.render(
    question="What is the capital of France?",
    context=["France is a country in Europe.", "It is known for the Eiffel Tower."],
    answer_options=["Berlin", "Madrid", "Paris", "Rome"],
    example_answer="C",
    preamble="Please answer the following question:",
)
print(prompt)

results in this prompt:

Please answer the following question:

Question: What is the capital of France?

Context:
- France is a country in Europe.
- It is known for the Eiffel Tower.

IMPORTANT: Provide your final answer within <answer></answer> tags.
Think step-by-step before giving your final answer.
The answer must be the letter (e.g., "A", "B", "C", ...)
corresponding to your chosen option from the list below:

A. Berlin
B. Madrid
C. Paris
D. Rome

Example Answer:
Your explanation for why you chose this answer can go here... <answer>C</answer>

Answer:

@nkaenzig nkaenzig linked an issue Oct 21, 2025 that may be closed by this pull request
@nkaenzig nkaenzig marked this pull request as ready for review October 21, 2025 09:05
@nkaenzig nkaenzig self-assigned this Oct 21, 2025
Copy link
Collaborator

@MaxFeucht MaxFeucht left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, some comments on alignment between templates

IMPORTANT: Respond with a valid JSON object where the "{{ answer_key }}" key
contains your answer, and "{{ reason_key }}" should contain a brief
explanation for why the provided answer was chosen.
IMPORTANT: Respond with a valid JSON object where the "{{ answer_key }}" key contains your answer.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
IMPORTANT: Respond with a valid JSON object where the "{{ answer_key }}" key contains your answer.
IMPORTANT: Respond with a valid JSON object with a single "{{ answer_key }}" key that contains your answer.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the raw template it says

IMPORTANT: You must provide your reasoning first, then end your response with only your final answer
  • I suggest we either explicitly mention here to reason first as well, or we leave it out in both cases. I'd be in favor of keeping it everywhere if CoT is enabled and use the phrasing here if not.

So with enable_cot:

IMPORTANT: You must provide your reasoning first, then end your response with a valid JSON object with a single "{{ answer_key }}" key that contains your answer.

Without:

IMPORTANT: Respond with a valid JSON object with a single "{{ answer_key }}" key that contains your answer.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we keep the phrasing "reasoning" in general, since this can also apply to non-CoT responses. What do you think? Also related to the comment below

Comment on lines 43 to +46
Example JSON Answer:
Your explanation for why you chose this answer can go here...
{{ '{' }}
"{{ answer_key }}": "{{ example_answer }}",
"{{ reason_key }}": "{{ example_reason }}"
"{{ answer_key }}": "{{ example_answer }}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

Example Answer:
Your reasoning for why you chose this answer can go here...
{{ '{' }}
     "{{ answer_key }}": "{{ example_answer }}"
{{ '}' }}

{{ context }}
{% endif %}
IMPORTANT: Provide your final answer within <{{ answer_key }}></{{ answer_key }}> tags.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, let's align the instruction across templates

{{ answer_options }}
Example Answer:
Your explanation for why you chose this answer can go here... <{{ answer_key }}>{{ example_answer }}</{{ answer_key }}>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And same here with the phrasing of "reasoning"

self.answer_key = answer_key

@override
def render(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this and the JSON template render func are the same, perhaps we can unify that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add answer template using xml response format

2 participants