Skip to content

Commit

Permalink
Developer message type introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbanda committed Jan 3, 2025
1 parent 81cf970 commit 5dea3a0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.cequence.openaiscala.domain.{
AssistantMessage,
BaseMessage,
ChatRole,
DeveloperMessage,
ImageURLContent,
MessageSpec,
SystemMessage,
Expand Down Expand Up @@ -98,7 +99,8 @@ package object impl {
messages: Seq[BaseMessage]
): Option[Content] = {
val contents = messages.collect {
case SystemMessage(content, _) => content
case SystemMessage(content, _) => content
case DeveloperMessage(content, _) => content
// legacy message type
case MessageSpec(role, content, _) if role == ChatRole.System =>
content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,19 @@ object JsonFormats {

implicit lazy val chatRoleFormat: Format[ChatRole] = enumFormat[ChatRole](
ChatRole.User,
ChatRole.Developer,
ChatRole.System,
ChatRole.Assistant,
ChatRole.Function,
ChatRole.Tool
)

implicit lazy val contentWrites: Writes[Content] = Writes[Content] {
_ match {
case c: TextContent =>
Json.obj("type" -> "text", "text" -> c.text)
case c: TextContent =>
Json.obj("type" -> "text", "text" -> c.text)

case c: ImageURLContent =>
Json.obj("type" -> "image_url", "image_url" -> Json.obj("url" -> c.url))
}
case c: ImageURLContent =>
Json.obj("type" -> "image_url", "image_url" -> Json.obj("url" -> c.url))
}

implicit lazy val contentReads: Reads[Content] = Reads[Content] { (json: JsValue) =>
Expand All @@ -93,6 +92,7 @@ object JsonFormats {
Json.format[FunctionCallSpec]

implicit val systemMessageFormat: Format[SystemMessage] = Json.format[SystemMessage]
implicit val developerMessageFormat: Format[DeveloperMessage] = Json.format[DeveloperMessage]
implicit val userMessageFormat: Format[UserMessage] = Json.format[UserMessage]
implicit val userSeqMessageFormat: Format[UserSeqMessage] = Json.format[UserSeqMessage]

Expand Down Expand Up @@ -292,6 +292,8 @@ object JsonFormats {
val json = message match {
case m: SystemMessage => toJson(m)

case m: DeveloperMessage => toJson(m)

case m: UserMessage => toJson(m)

case m: UserSeqMessage => Json.obj("content" -> toJson(m.content))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ final case class SystemMessage(
def withName(name: String): SystemMessage = this.copy(name = Some(name))
}

// new system message for o1 models
final case class DeveloperMessage(
// The contents of the message.
content: String,

// An optional name for the participant. Provides the model information to differentiate between participants of the same role.
// May contain a-z, A-Z, 0-9, and underscores, with a maximum length of 64 characters.
name: Option[String] = None
) extends BaseMessage {
override val role = ChatRole.Developer
override val nameOpt = name

def withName(name: String): DeveloperMessage = this.copy(name = Some(name))
}

final case class UserMessage(
// The contents of the message.
content: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ sealed trait ThreadAndRunRole
object ChatRole {
case object User extends ChatRole with ThreadAndRunRole
case object System extends ChatRole
case object Developer extends ChatRole
case object Assistant extends ChatRole with ThreadAndRunRole
@Deprecated
case object Function extends ChatRole
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ object ChatCompletionInputAdapterForFireworksAI
private val handleSystemMessages = (messages: Seq[BaseMessage]) => {
val nonSystemMessages = messages.map {
case SystemMessage(content, _) => UserMessage(s"System: ${content}")
case DeveloperMessage(content, _) => UserMessage(s"System: ${content}")
case x: BaseMessage => x
}

Expand Down

0 comments on commit 5dea3a0

Please sign in to comment.