Skip to content

Commit cee1aaf

Browse files
feat(client): make it easy to roundtrip messages (#148)
1 parent 11b068e commit cee1aaf

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

openai-java-core/src/main/kotlin/com/openai/models/ChatCompletionCreateParams.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,10 @@ constructor(
14131413
)
14141414
)
14151415

1416+
/** Messages sent by the model in response to user messages. */
1417+
fun addMessage(chatCompletionAssistantMessageParam: ChatCompletionMessage) =
1418+
addMessage(chatCompletionAssistantMessageParam.toParam())
1419+
14161420
/**
14171421
* A list of messages comprising the conversation so far. Depending on the
14181422
* [model](https://platform.openai.com/docs/models) you use, different message types
@@ -2558,6 +2562,11 @@ constructor(
25582562
body.addMessage(chatCompletionAssistantMessageParam)
25592563
}
25602564

2565+
/** Messages sent by the model in response to user messages. */
2566+
fun addMessage(chatCompletionAssistantMessageParam: ChatCompletionMessage) = apply {
2567+
body.addMessage(chatCompletionAssistantMessageParam)
2568+
}
2569+
25612570
/**
25622571
* A list of messages comprising the conversation so far. Depending on the
25632572
* [model](https://platform.openai.com/docs/models) you use, different message types

openai-java-core/src/main/kotlin/com/openai/models/ChatCompletionMessage.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,29 @@ private constructor(
103103
@ExcludeMissing
104104
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties
105105

106+
fun toParam(): ChatCompletionAssistantMessageParam =
107+
ChatCompletionAssistantMessageParam.builder()
108+
.role(_role().map { ChatCompletionAssistantMessageParam.Role.of(it.toString()) })
109+
.audio(
110+
_audio().map {
111+
ChatCompletionAssistantMessageParam.Audio.builder().id(it._id()).build()
112+
}
113+
)
114+
.content(
115+
_content().map { ChatCompletionAssistantMessageParam.Content.ofTextContent(it) }
116+
)
117+
.functionCall(
118+
_functionCall().map {
119+
ChatCompletionAssistantMessageParam.FunctionCall.builder()
120+
.arguments(it._arguments())
121+
.name(it._name())
122+
.build()
123+
}
124+
)
125+
.refusal(_refusal())
126+
.toolCalls(_toolCalls())
127+
.build()
128+
106129
private var validated: Boolean = false
107130

108131
fun validate(): ChatCompletionMessage = apply {

0 commit comments

Comments
 (0)