-
-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[Frontend] Merge developer instructions into tool block for Harmony prompt rendering #34951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a55024e
d226cf7
48e842b
fcf132f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1960,15 +1960,24 @@ def _make_request_with_harmony( | |
| ) | ||
| messages.append(sys_msg) | ||
|
|
||
| chat_messages = request.messages | ||
| merged_instructions: str | None = None | ||
| if chat_messages and chat_messages[0]["role"] in ("system", "developer"): | ||
| content = chat_messages[0].get("content") | ||
| if isinstance(content, str): | ||
| merged_instructions = content | ||
| chat_messages = chat_messages[1:] | ||
|
|
||
| # Add developer message. | ||
| if request.tools: | ||
| if request.tools or merged_instructions: | ||
| dev_msg = get_developer_message( | ||
| tools=request.tools if should_include_tools else None # type: ignore[arg-type] | ||
| instructions=merged_instructions, | ||
| tools=request.tools if should_include_tools else None, # type: ignore[arg-type] | ||
| ) | ||
| messages.append(dev_msg) | ||
|
Comment on lines
+1972
to
1977
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation causes data loss when |
||
|
|
||
| # Add user message. | ||
| messages.extend(parse_chat_inputs_to_harmony_messages(request.messages)) | ||
| messages.extend(parse_chat_inputs_to_harmony_messages(chat_messages)) | ||
|
|
||
| # Render prompt token ids. | ||
| prompt_token_ids = render_for_completion(messages) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing
["role"]directly is unsafe here.ChatCompletionMessageParamis a union that includesOpenAIHarmonyMessage(a class), and even for dict-based messages, it is safer to use.get("role")to avoid potentialKeyErrororTypeError. Given thatharmony_utils.pyexplicitly handles non-dict inputs, this part should be equally robust to prevent crashes if objects are passed programmatically.