feat: Generate problem support for title variable#2310
Conversation
|
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
| [HumanMessage(content=prompt.replace('{data}', paragraph.content).replace('{title}', paragraph.title))]) | ||
| if (res.content is None) or (len(res.content) == 0): | ||
| return | ||
| problems = res.content.split('\n') |
There was a problem hiding this comment.
The given code has two main issues:
-
Multiple
replacefunctions: The originalprompt.replace('{data}', paragraph.content)replaces all occurrences of the placeholder{data}with the paragraph's content. However, if the inputpromptalso contains a placeholder for'{title}', this line will replace both placeholders unnecessarily.# Incorrect usage due to double replacement Prompt = "I want you to generate problems based on {data} ({title})." Paragraph_content = "Sample Content" Title = "Sample Title" prompt_with_title = Prompt.replace('{data}', Paragraph_content) # This replaces '{data}' final_prompt = prompt_with_title.replace('{title}', Title) # This replaces '{title}' again
Instead, separate replacements should be done to avoid unnecessary replacements and make it clearer that each variable serves a specific purpose when generating text.
-
String formatting in Python: If there is an intention to format strings using variables from
paragraph.title, consider using named parameters instead of keyword arguments when creating objects likeHumanMessage. In many languages like Python and some others, using named parameters can help improve readability by clearly indicating which part of the string corresponds to which parameter value.HumanMessage(content=prompt.replace('{data}', paragraph.content))
becomes:
HumanMessage(content='{originalPrompt}'.format(originalPrompt=prompt.format(data=paragraph.content)))
Here’s how these improvements could look after addressing the first issue alone:
Modified Code Block
try:
ListenerManagement.update_status(QuerySet(Paragraph).filter(id=paragraph.id), TaskType.GENERATE_PROBLEM,
State.STARTED)
prompts_list = [
{"message": prompt.replace('{data}', paragraph.content)},
{"message": prompt.replace('{title}', paragraph.title)}
]
for prompt_dict in prompts_list:
res = llm_model.invoke([HumanMessage(**prompt_dict)])
if (res.content is None) or (len(res.content) == 0):
return
problems = res.content.split('\n')These changes ensure that each message generated uses only relevant placeholders defined in its respective dictionary key.
feat: Generate problem support for title variable