Skip to content

Commit

Permalink
Merge branch 'main' into feature/tool-json-config
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscodr authored Aug 22, 2024
2 parents 498314e + 30280d3 commit c540c83
Show file tree
Hide file tree
Showing 12 changed files with 17,002 additions and 13,331 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ interface PromptBuilder {
ChatCompletionRequestMessage.CaseChatCompletionRequestAssistantMessage(
ChatCompletionRequestAssistantMessage(
role = ChatCompletionRequestAssistantMessage.Role.assistant,
content = value
content = ChatCompletionRequestAssistantMessageContent.CaseString(value)
)
)

Expand All @@ -104,7 +104,7 @@ interface PromptBuilder {
ChatCompletionRequestMessage.CaseChatCompletionRequestToolMessage(
ChatCompletionRequestToolMessage(
role = ChatCompletionRequestToolMessage.Role.tool,
content = value,
content = ChatCompletionRequestToolMessageContent.CaseString(value),
toolCallId = toolCallId
)
)
Expand All @@ -113,7 +113,7 @@ interface PromptBuilder {
ChatCompletionRequestMessage.CaseChatCompletionRequestSystemMessage(
ChatCompletionRequestSystemMessage(
role = ChatCompletionRequestSystemMessage.Role.system,
content = value
content = ChatCompletionRequestSystemMessageContent.CaseString(value)
)
)

Expand All @@ -122,24 +122,25 @@ interface PromptBuilder {
ChatCompletionRequestUserMessage(
role = ChatCompletionRequestUserMessage.Role.user,
content =
ChatCompletionRequestUserMessageContent.CaseChatCompletionRequestMessageContentParts(
listOf(
ChatCompletionRequestMessageContentPart
.CaseChatCompletionRequestMessageContentPartImage(
ChatCompletionRequestMessageContentPartImage(
type = ChatCompletionRequestMessageContentPartImage.Type.image_url,
imageUrl = ChatCompletionRequestMessageContentPartImageImageUrl(url)
ChatCompletionRequestUserMessageContent
.CaseChatCompletionRequestUserMessageContentParts(
listOf(
ChatCompletionRequestUserMessageContentPart
.CaseChatCompletionRequestMessageContentPartImage(
ChatCompletionRequestMessageContentPartImage(
type = ChatCompletionRequestMessageContentPartImage.Type.image_url,
imageUrl = ChatCompletionRequestMessageContentPartImageImageUrl(url)
)
),
ChatCompletionRequestUserMessageContentPart
.CaseChatCompletionRequestMessageContentPartText(
ChatCompletionRequestMessageContentPartText(
type = ChatCompletionRequestMessageContentPartText.Type.text,
text = text
)
)
),
ChatCompletionRequestMessageContentPart
.CaseChatCompletionRequestMessageContentPartText(
ChatCompletionRequestMessageContentPartText(
type = ChatCompletionRequestMessageContentPartText.Type.text,
text = text
)
)
)
)
)
)
)
}
Expand All @@ -150,20 +151,53 @@ fun ChatCompletionRequestMessage.contentAsString(): String =
is ChatCompletionRequestMessage.CaseChatCompletionRequestUserMessage ->
when (val content = value.content) {
is ChatCompletionRequestUserMessageContent.CaseString -> content.value
is ChatCompletionRequestUserMessageContent.CaseChatCompletionRequestMessageContentParts ->
is ChatCompletionRequestUserMessageContent.CaseChatCompletionRequestUserMessageContentParts ->
content.value.joinToString {
when (it) {
is ChatCompletionRequestMessageContentPart.CaseChatCompletionRequestMessageContentPartImage ->
is ChatCompletionRequestUserMessageContentPart.CaseChatCompletionRequestMessageContentPartImage ->
it.value.imageUrl.url
is ChatCompletionRequestMessageContentPart.CaseChatCompletionRequestMessageContentPartText ->
is ChatCompletionRequestUserMessageContentPart.CaseChatCompletionRequestMessageContentPartText ->
it.value.text
}
}
}
is ChatCompletionRequestMessage.CaseChatCompletionRequestAssistantMessage ->
when (val content = value.content) {
is ChatCompletionRequestAssistantMessageContent.CaseString -> content.value
is ChatCompletionRequestAssistantMessageContent.CaseChatCompletionRequestAssistantMessageContentParts ->
content.value.joinToString {
when (it) {
is ChatCompletionRequestAssistantMessageContentPart.CaseChatCompletionRequestMessageContentPartText ->
it.value.text
is ChatCompletionRequestAssistantMessageContentPart.CaseChatCompletionRequestMessageContentPartRefusal ->
it.value.refusal
}
}
null -> ""
}
is ChatCompletionRequestMessage.CaseChatCompletionRequestToolMessage ->
when (val content = value.content) {
is ChatCompletionRequestToolMessageContent.CaseString -> content.value
is ChatCompletionRequestToolMessageContent.CaseChatCompletionRequestToolMessageContentParts ->
content.value.joinToString {
when (it) {
is ChatCompletionRequestToolMessageContentPart.CaseChatCompletionRequestMessageContentPartText ->
it.value.text
}
}
}
is ChatCompletionRequestMessage.CaseChatCompletionRequestAssistantMessage -> value.content ?: ""
is ChatCompletionRequestMessage.CaseChatCompletionRequestToolMessage -> value.content
is ChatCompletionRequestMessage.CaseChatCompletionRequestFunctionMessage -> value.content ?: ""
is ChatCompletionRequestMessage.CaseChatCompletionRequestSystemMessage -> value.content
is ChatCompletionRequestMessage.CaseChatCompletionRequestSystemMessage ->
when (val content = value.content) {
is ChatCompletionRequestSystemMessageContent.CaseString -> content.value
is ChatCompletionRequestSystemMessageContent.CaseChatCompletionRequestSystemMessageContentParts ->
content.value.joinToString {
when (it) {
is ChatCompletionRequestSystemMessageContentPart.CaseChatCompletionRequestMessageContentPartText ->
it.value.text
}
}
}
}

internal fun ChatCompletionRequestMessage.completionRole(): ChatCompletionRole =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.xebia.functional.xef.store

import com.xebia.functional.openai.generated.model.*
import com.xebia.functional.xef.prompt.completionRole
import kotlinx.serialization.json.Json

sealed class MemorizedMessage {
val role: ChatCompletionRole
Expand All @@ -19,7 +20,14 @@ sealed class MemorizedMessage {
ChatCompletionRequestAssistantMessage(
role = ChatCompletionRequestAssistantMessage.Role.assistant,
// TODO: Find a new strategy to save the tool calls as content
content = message.content ?: message.toolCalls?.firstOrNull()?.toString()
content =
ChatCompletionRequestAssistantMessageContent.CaseString(
message.content
?: message.toolCalls?.firstOrNull()?.let {
Json.Default.encodeToString(ChatCompletionMessageToolCall.serializer(), it)
}
?: ""
),
)
)
}
Expand All @@ -35,7 +43,7 @@ fun memorizedMessage(role: ChatCompletionRole, content: String): MemorizedMessag
MemorizedMessage.Request(
ChatCompletionRequestMessage.CaseChatCompletionRequestSystemMessage(
ChatCompletionRequestSystemMessage(
content = content,
content = ChatCompletionRequestSystemMessageContent.CaseString(content),
role = ChatCompletionRequestSystemMessage.Role.system
)
)
Expand All @@ -53,6 +61,7 @@ fun memorizedMessage(role: ChatCompletionRole, content: String): MemorizedMessag
MemorizedMessage.Response(
ChatCompletionResponseMessage(
content = content,
refusal = null,
role = ChatCompletionResponseMessage.Role.assistant
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class ConversationSpec :
val aiFirstMessage =
ChatCompletionRequestAssistantMessage(
role = ChatCompletionRequestAssistantMessage.Role.assistant,
content = firstResponse
content = ChatCompletionRequestAssistantMessageContent.CaseString(firstResponse)
)

val secondPrompt = Prompt(model) { +user("question 3") }
Expand All @@ -155,7 +155,7 @@ class ConversationSpec :
val aiSecondMessage =
ChatCompletionRequestAssistantMessage(
role = ChatCompletionRequestAssistantMessage.Role.assistant,
content = secondResponse
content = ChatCompletionRequestAssistantMessageContent.CaseString(secondResponse)
)

val thirdPrompt =
Expand All @@ -168,7 +168,7 @@ class ConversationSpec :
val aiThirdMessage =
ChatCompletionRequestAssistantMessage(
role = ChatCompletionRequestAssistantMessage.Role.assistant,
content = secondResponse
content = ChatCompletionRequestAssistantMessageContent.CaseString(secondResponse)
)

val memories = vectorStore.memories(model, conversationId, 10000)
Expand Down Expand Up @@ -424,7 +424,7 @@ private fun chatCompletionRequestMessages(it: Map.Entry<String, String>) =
ChatCompletionRequestMessage.CaseChatCompletionRequestAssistantMessage(
ChatCompletionRequestAssistantMessage(
role = ChatCompletionRequestAssistantMessage.Role.assistant,
content = it.value
content = ChatCompletionRequestAssistantMessageContent.CaseString(it.value)
)
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class TestChatApi(private val responses: Map<String, String> = emptyMap()) : Cha
content =
responses[createChatCompletionRequest.messages.last().contentAsString()]
?: "fake-content",
refusal = null,
toolCalls =
listOf(
ChatCompletionMessageToolCall(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.xebia.functional.xef.store

import arrow.atomic.AtomicInt
import com.xebia.functional.openai.generated.model.ChatCompletionRequestAssistantMessage
import com.xebia.functional.openai.generated.model.ChatCompletionRequestMessage
import com.xebia.functional.openai.generated.model.ChatCompletionRequestUserMessage
import com.xebia.functional.openai.generated.model.ChatCompletionRequestUserMessageContent
import com.xebia.functional.openai.generated.model.*

class MemoryData {
val defaultConversationId = ConversationId("default-id")
Expand All @@ -28,7 +25,10 @@ class MemoryData {
val m2 =
ChatCompletionRequestAssistantMessage(
role = ChatCompletionRequestAssistantMessage.Role.assistant,
content = "Response $it${append?.let { ": $it" } ?: ""}"
content =
ChatCompletionRequestAssistantMessageContent.CaseString(
"Response $it${append?.let { ": $it" } ?: ""}"
)
)
listOf(
Memory(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ class OpenTelemetryAssistantState(private val tracer: Tracer) {
span.setAttribute("openai.assistant.message.id", id)
status?.let { span.setAttribute("openai.assistant.message.status", it.name) }
when (val inner = content.firstOrNull()) {
is MessageObjectContentInner.CaseMessageContentImageFileObject -> {
is MessageObjectContentInner.CaseMessageContentImageFileObject ->
span.setAttribute("openai.assistant.message.content", inner.value.imageFile.fileId)
}
is MessageObjectContentInner.CaseMessageContentTextObject -> {
is MessageObjectContentInner.CaseMessageContentTextObject ->
span.setAttribute("openai.assistant.message.content", inner.value.text.value)
}
is MessageObjectContentInner.CaseMessageContentImageUrlObject ->
span.setAttribute("openai.assistant.message.content", inner.value.imageUrl.url)
is MessageObjectContentInner.CaseMessageContentRefusalObject ->
span.setAttribute("openai.assistant.message.content", inner.value.refusal)
null -> {}
}
}
Expand Down
7 changes: 2 additions & 5 deletions integrations/postgresql/src/test/kotlin/xef/MemoryData.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package xef

import arrow.atomic.AtomicInt
import com.xebia.functional.openai.generated.model.ChatCompletionRequestAssistantMessage
import com.xebia.functional.openai.generated.model.ChatCompletionRequestMessage
import com.xebia.functional.openai.generated.model.ChatCompletionRequestUserMessage
import com.xebia.functional.openai.generated.model.ChatCompletionRequestUserMessageContent
import com.xebia.functional.openai.generated.model.*
import com.xebia.functional.xef.store.ConversationId
import com.xebia.functional.xef.store.MemorizedMessage
import com.xebia.functional.xef.store.Memory
Expand All @@ -29,7 +26,7 @@ class MemoryData {
val m2 = ChatCompletionRequestMessage.CaseChatCompletionRequestAssistantMessage(
ChatCompletionRequestAssistantMessage(
role = ChatCompletionRequestAssistantMessage.Role.assistant,
content = "Answer $it${append?.let { ": $it" } ?: ""}"
content = ChatCompletionRequestAssistantMessageContent.CaseString("Answer $it${append?.let { ": $it" } ?: ""}")
)
)
listOf(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.xebia.functional.openai.generated.model

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*

/**
* @param name The name of the response format. Must be a-z, A-Z, 0-9, or contain underscores and
* dashes, with a maximum length of 64.
* @param description A description of what the response format is for, used by the model to
* determine how to respond in the format.
* @param schema The schema for the response format, described as a JSON Schema object.
* @param strict Whether to enable strict schema adherence when generating the output. If set to
* true, the model will always follow the exact schema defined in the `schema` field. Only a
* subset of JSON Schema is supported when `strict` is `true`. To learn more, read the
* [Structured Outputs guide](/docs/guides/structured-outputs).
*/
@Serializable
data class ResponseFormatJsonSchemaJsonSchema(
/* The name of the response format. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64. */
@SerialName(value = "name") val name: kotlin.String,
/* A description of what the response format is for, used by the model to determine how to respond in the format. */
@SerialName(value = "description") val description: kotlin.String? = null,
/* The schema for the response format, described as a JSON Schema object. */
@SerialName(value = "schema") val schema: JsonObject? = null,
/* Whether to enable strict schema adherence when generating the output. If set to true, the model will always follow the exact schema defined in the `schema` field. Only a subset of JSON Schema is supported when `strict` is `true`. To learn more, read the [Structured Outputs guide](/docs/guides/structured-outputs). */
@SerialName(value = "strict") val strict: kotlin.Boolean? = false
) {}
1 change: 1 addition & 0 deletions openai-client/generator/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
**/settings.gradle
**/CreateAssistantRequestToolResourcesFileSearch.kt
**/CreateThreadRequestToolResourcesFileSearch.kt
**/ResponseFormatJsonSchemaJsonSchema.kt
2 changes: 1 addition & 1 deletion openai-client/generator/config/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fun {{classname}}(client: HttpClient, config: Config): {{apiPackage}}.{{classnam
{{/headerParams}}
contentType(ContentType.Application.Json)
{{#queryParams}}
parameter("{{baseName}}", {{#isContainer}}toMultiValue(this, "{{collectionFormat}}"){{/isContainer}}{{^isContainer}}{{{paramName}}}{{/isContainer}})
parameter("{{baseName}}", {{#isContainer}}{{{paramName}}}{{/isContainer}}{{^isContainer}}{{{paramName}}}{{/isContainer}})
{{/queryParams}}
url { path("{{#lambda.dropslash}}{{path}}{{/lambda.dropslash}}"{{#pathParams}}.replace("{" + "{{baseName}}" + "}", {{#isContainer}}{{paramName}}.joinToString(","){{/isContainer}}{{^isContainer}}"${{{paramName}}}"{{/isContainer}}){{/pathParams}}) }
setBody({{#hasBodyParam}}{{#bodyParam}}{{#isArray}}{{operationIdCamelCase}}Request({{{paramName}}}{{^isList}}.asList(){{/isList}}){{/isArray}}{{^isArray}}{{#isMap}}{{operationIdCamelCase}}Request({{{paramName}}}){{/isMap}}{{^isMap}}{{{paramName}}}{{/isMap}}{{/isArray}}{{/bodyParam}}{{/hasBodyParam}}
Expand Down
2 changes: 1 addition & 1 deletion openai-client/generator/config/openai-api-commit
Original file line number Diff line number Diff line change
@@ -1 +1 @@
df5699f5c48fe455d9d036198e77fdb637edee29
3d5576596e5fe1cd3b88ddcd407dd1c5f3594f02
Loading

0 comments on commit c540c83

Please sign in to comment.