Skip to content

Commit 2ab9f32

Browse files
author
Peter Dolukhanov
committed
Added a Developer message, as used by OpenAI replacing the system message.
Signed-off-by: Peter Dolukhanov <[email protected]>
1 parent 687dea5 commit 2ab9f32

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright 2023-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.chat.messages;
18+
19+
import org.springframework.core.io.Resource;
20+
import org.springframework.lang.NonNull;
21+
import org.springframework.lang.Nullable;
22+
import org.springframework.util.StringUtils;
23+
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
import java.util.Objects;
27+
28+
/**
29+
* A message of the type 'developer' passed as input. The developer message gives
30+
* instructions or requests from developers using the API. This role typically provides
31+
* detailed instructions for the model to follow, such as specific actions or formats.
32+
*/
33+
public class DeveloperMessage extends AbstractMessage {
34+
35+
public DeveloperMessage(String textContent) {
36+
this(textContent, Map.of());
37+
}
38+
39+
public DeveloperMessage(Resource resource) {
40+
this(MessageUtils.readResource(resource), Map.of());
41+
}
42+
43+
private DeveloperMessage(String textContent, Map<String, Object> metadata) {
44+
super(MessageType.DEVELOPER, textContent, metadata);
45+
}
46+
47+
@Override
48+
@NonNull
49+
public String getText() {
50+
return this.textContent;
51+
}
52+
53+
@Override
54+
public boolean equals(Object o) {
55+
if (this == o) {
56+
return true;
57+
}
58+
if (!(o instanceof DeveloperMessage that)) {
59+
return false;
60+
}
61+
if (!super.equals(o)) {
62+
return false;
63+
}
64+
return Objects.equals(this.textContent, that.textContent);
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return Objects.hash(super.hashCode(), this.textContent);
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return "DeveloperMessage{" + "textContent='" + this.textContent + '\'' + ", messageType=" + this.messageType
75+
+ ", metadata=" + this.metadata + '}';
76+
}
77+
78+
public DeveloperMessage copy() {
79+
return new DeveloperMessage(getText(), Map.copyOf(this.metadata));
80+
}
81+
82+
public Builder mutate() {
83+
return new Builder().text(this.textContent).metadata(this.metadata);
84+
}
85+
86+
public static Builder builder() {
87+
return new Builder();
88+
}
89+
90+
public static class Builder {
91+
92+
@Nullable
93+
private String textContent;
94+
95+
@Nullable
96+
private Resource resource;
97+
98+
private Map<String, Object> metadata = new HashMap<>();
99+
100+
public Builder text(String textContent) {
101+
this.textContent = textContent;
102+
return this;
103+
}
104+
105+
public Builder text(Resource resource) {
106+
this.resource = resource;
107+
return this;
108+
}
109+
110+
public Builder metadata(Map<String, Object> metadata) {
111+
this.metadata = metadata;
112+
return this;
113+
}
114+
115+
public DeveloperMessage build() {
116+
if (StringUtils.hasText(textContent) && resource != null) {
117+
throw new IllegalArgumentException("textContent and resource cannot be set at the same time");
118+
}
119+
else if (resource != null) {
120+
this.textContent = MessageUtils.readResource(resource);
121+
}
122+
return new DeveloperMessage(this.textContent, this.metadata);
123+
}
124+
125+
}
126+
127+
}

spring-ai-model/src/main/java/org/springframework/ai/chat/messages/MessageType.java

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ public enum MessageType {
4444
*/
4545
SYSTEM("system"),
4646

47+
/**
48+
* A {@link Message} of type {@literal developer} passed as input {@link Message}
49+
* Messages containing instructions or requests from developers using the API.
50+
* @see DeveloperMessage
51+
*/
52+
DEVELOPER("developer"),
53+
4754
/**
4855
* A {@link Message} of type {@literal function} passed as input {@link Message
4956
* Messages} with function content in a chat application.

0 commit comments

Comments
 (0)