Skip to content
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

Add support for the developer role in chat completion messages #117

Merged
merged 2 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions internal/apischema/openai/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// Chat message role defined by the OpenAI API.
const (
ChatMessageRoleSystem = "system"
ChatMessageRoleDeveloper = "developer"
ChatMessageRoleUser = "user"
ChatMessageRoleAssistant = "assistant"
ChatMessageRoleFunction = "function"
Expand Down Expand Up @@ -225,6 +226,13 @@ func (c *ChatCompletionMessageParamUnion) UnmarshalJSON(data []byte) error {
}
c.Value = systemMessage
c.Type = ChatMessageRoleSystem
case ChatMessageRoleDeveloper:
var developerMessage ChatCompletionDeveloperMessageParam
if err := json.Unmarshal(data, &developerMessage); err != nil {
return err
}
c.Value = developerMessage
c.Type = ChatMessageRoleDeveloper
case ChatMessageRoleTool:
var toolMessage ChatCompletionToolMessageParam
if err := json.Unmarshal(data, &toolMessage); err != nil {
Expand Down Expand Up @@ -263,6 +271,19 @@ type ChatCompletionSystemMessageParam struct {
Name string `json:"name,omitempty"`
}

// ChatCompletionDeveloperMessageParam Developer-provided instructions that the model should follow, regardless of
// messages sent by the user. With o1 models and newer, use `developer` messages
// for this purpose instead.
type ChatCompletionDeveloperMessageParam struct {
// The contents of the developer message.
Content StringOrArray `json:"content"`
// The role of the messages author, in this case `developer`.
Role string `json:"role"`
// An optional name for the participant. Provides the model information to
// differentiate between participants of the same role.
Name string `json:"name,omitempty"`
}

type ChatCompletionToolMessageParam struct {
// The contents of the tool message.
Content StringOrArray `json:"content"`
Expand Down
25 changes: 25 additions & 0 deletions internal/apischema/openai/openai_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestOpenAIChatCompletionMessageUnmarshal(t *testing.T) {
in: []byte(`{"model": "gpu-o4",
"messages": [
{"role": "system", "content": "you are a helpful assistant"},
{"role": "developer", "content": "you are a helpful dev assistant"},
{"role": "user", "content": "what do you see in this image"}]}`),
out: &ChatCompletionRequest{
Model: "gpu-o4",
Expand All @@ -33,6 +34,15 @@ func TestOpenAIChatCompletionMessageUnmarshal(t *testing.T) {
},
Type: ChatMessageRoleSystem,
},
{
Value: ChatCompletionDeveloperMessageParam{
Role: ChatMessageRoleDeveloper,
Content: StringOrArray{
Value: "you are a helpful dev assistant",
},
},
Type: ChatMessageRoleDeveloper,
},
{
Value: ChatCompletionUserMessageParam{
Role: ChatMessageRoleUser,
Expand All @@ -50,6 +60,7 @@ func TestOpenAIChatCompletionMessageUnmarshal(t *testing.T) {
in: []byte(`{"model": "gpu-o4",
"messages": [
{"role": "system", "content": [{"text": "you are a helpful assistant", "type": "text"}]},
{"role": "developer", "content": [{"text": "you are a helpful dev assistant", "type": "text"}]},
{"role": "user", "content": [{"text": "what do you see in this image", "type": "text"}]}]}`),
out: &ChatCompletionRequest{
Model: "gpu-o4",
Expand All @@ -68,6 +79,20 @@ func TestOpenAIChatCompletionMessageUnmarshal(t *testing.T) {
},
Type: ChatMessageRoleSystem,
},
{
Value: ChatCompletionDeveloperMessageParam{
Role: ChatMessageRoleDeveloper,
Content: StringOrArray{
Value: []ChatCompletionContentPartTextParam{
{
Text: "you are a helpful dev assistant",
Type: string(openai.ChatCompletionContentPartTextTypeText),
},
},
},
},
Type: ChatMessageRoleDeveloper,
},
{
Value: ChatCompletionUserMessageParam{
Role: ChatMessageRoleUser,
Expand Down
Loading