Skip to content

Commit

Permalink
Refine OpenAI API
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Sep 7, 2024
1 parent 5c3670e commit 9e74692
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 20 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
This list is not intended to be all-encompassing - it will document major and breaking API
changes with their rationale when appropriate. Given version `A.B.C.D`, breaking changes are to be expected in version number increments where changes in the `A` or `B` sections:

### v5.24.0.0
- **http4k-connect-*** - Upgrade dependencies including Kotlin to 2.0.20
- **http4k-connect-ai-openai*** - [Breaking] tightened up types for completion requests.

### v5.23.0.0
- **http4k-connect-*** - Upgrade dependencies including Kotlin to 2.0.20
- **http4k-connect-ai-azure*** - Add new operations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ data class ChatCompletion(
stream = stream
)

constructor(model: ModelName, message: Message, max_tokens: Int = 16, stream: Boolean = true) : this(
model,
listOf(message),
max_tokens = max_tokens,
temperature = 1.0,
top_p = 1.0,
n = 1,
stop = null,
presence_penalty = 0.0,
frequency_penalty = 0.0,
logit_bias = null,
user = null,
stream = stream
)

init {
require(tools == null || tools.isNotEmpty()) { "Tools cannot be empty" }
}
Expand Down Expand Up @@ -92,21 +107,42 @@ sealed class ResponseFormat {
data class JsonSchema(val strict: Boolean, val json_schema: Map<String, Any>) : ResponseFormat()
}


@JsonSerializable
data class Message(
val role: Role?,
val content: List<MessageContent>,
val content: List<MessageContent>? = null,
val name: User? = null,
val refusal: String? = null,
val tool_calls: List<ToolCall>? = null
) {
@Deprecated("Use relevant companion constructor instead")
constructor(
role: Role,
text: String,
name: User? = null,
tool_calls: List<ToolCall>? = null
) :
this(role, listOf(MessageContent(ContentType.text, text)), name, tool_calls)
this(role, listOf(MessageContent(ContentType.text, text)), name, null, tool_calls)

companion object {
fun User(content: String, name: User? = null) = User(listOf(MessageContent(ContentType.text, content)), name)
fun User(content: List<MessageContent>, name: User? = null) = Message(Role.User, content, name, null)

fun System(content: String, name: User? = null) =
System(listOf(MessageContent(ContentType.text, content)), name)

fun System(content: List<MessageContent>, name: User? = null) = Message(Role.System, content, name)

fun Assistant(content: String, name: User? = null, refusal: String? = null) =
Assistant(listOf(MessageContent(ContentType.text, content)), name, refusal)

fun Assistant(content: List<MessageContent>, name: User? = null, refusal: String? = null) =
Message(Role.Assistant, content, name, refusal)

@JvmName("AssistantToolCalls")
fun Assistant(tool_calls: List<ToolCall>, name: User? = null, refusal: String? = null) =
Message(Role.Assistant, null, name, refusal, tool_calls)
}
}

@JsonSerializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ interface OpenAIContract {
val responses = openAi.chatCompletion(
ModelName.GPT3_5,
listOf(
Message(System, "You are Leonardo Da Vinci"),
Message(User, "What is your favourite colour?")
Message.System("You are Leonardo Da Vinci"),
Message.User("What is your favourite colour?")
),
1000,
stream = false
Expand All @@ -55,8 +55,8 @@ interface OpenAIContract {
val responses = openAi.chatCompletion(
ModelName.GPT3_5,
listOf(
Message(System, "You are Leonardo Da Vinci"),
Message(User, "What is your favourite colour?")
Message.System("You are Leonardo Da Vinci"),
Message.User("What is your favourite colour?")
),
1000,
stream = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fun main() {
openai.chatCompletion(
ModelName.GPT3_5,
listOf(
Message(User, "Explain pythagoras's theorem to a 5 year old child"),
Message.User("Explain pythagoras's theorem to a 5 year old child")
),
1000,
false
Expand Down
2 changes: 1 addition & 1 deletion ai/openai/fake/src/examples/kotlin/using the fake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fun main() {

// get a chat completion
openai
.chatCompletion(ModelName.GPT3_5, listOf(Message(User, "good afternoon")), 1000, true)
.chatCompletion(ModelName.GPT3_5, listOf(Message.User("good afternoon")), 1000, true)
.onFailure { error(it) }
.toList()
.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ fun interface ChatCompletionGenerator : (ChatCompletion) -> List<Choice> {
val ChatCompletionGenerator.Companion.ReverseInput
get() = ChatCompletionGenerator { req ->
req.messages.flatMap { m ->
m.content.mapIndexed { i, content ->
m.content?.mapIndexed { i, content ->
Choice(i, ChoiceDetail(Role.System, content.text?.reversed() ?: "", null), null, stop)
}
} ?: emptyList()
}
}

Expand All @@ -39,7 +39,7 @@ fun ChatCompletionGenerator.Companion.LoremIpsum(random: Random = Random(0)) = C
*/
val ChatCompletionGenerator.Companion.Echo
get() = ChatCompletionGenerator { req ->
req.choices(req.messages.first { it.role == Role.User }.content.first().text ?: "")
req.choices(req.messages.first { it.role == Role.User }.content?.first()?.text ?: "")
}

private fun ChatCompletion.choices(msg: String) = (if (stream) msg.split(" ").map { "$it " } else listOf(msg))
Expand Down
17 changes: 9 additions & 8 deletions versions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

plugin.com.github.kt3k.coveralls=2.12.2

plugin.com.google.devtools.ksp=2.0.20-1.0.24
plugin.com.google.devtools.ksp=2.0.20-1.0.25

plugin.de.fayard.buildSrcLibs=0.60.5

plugin.com.github.davidmc24.gradle.plugin.avro=1.9.1

version.com.google.devtools.ksp..symbol-processing-api=2.0.20-1.0.24
version.com.google.devtools.ksp..symbol-processing-api=2.0.20-1.0.25

version.com.amazonaws..aws-lambda-java-events=3.13.0

Expand All @@ -29,9 +29,9 @@ version.com.zaxxer..HikariCP=5.1.0

version.dev.forkhandles..forkhandles-bom=2.20.0.0

version.dev.langchain4j..langchain4j=0.33.0
version.dev.langchain4j..langchain4j=0.34.0

version.http4k=5.29.0.0
version.http4k=5.30.0.0

version.joda-time..joda-time=2.12.7

Expand All @@ -55,6 +55,7 @@ version.jetbrains.exposed=0.41.1
## # available=0.51.1
## # available=0.52.0
## # available=0.53.0
## # available=0.54.0

version.org.apache.avro..avro=1.12.0

Expand Down Expand Up @@ -86,12 +87,12 @@ version.se.ansman.kotshi..api=3.0.0

version.se.ansman.kotshi..compiler=3.0.0

version.software.amazon.awssdk..bom=2.27.12
version.software.amazon.awssdk..bom=2.27.21

version.com.nimbusds..nimbus-jose-jwt=9.40

version.dev.langchain4j..langchain4j-core=0.33.0
version.dev.langchain4j..langchain4j-core=0.34.0

version.software.amazon.awssdk..s3=2.27.12
version.software.amazon.awssdk..s3=2.27.21

version.software.amazon.awssdk..sqs=2.27.12
version.software.amazon.awssdk..sqs=2.27.21

0 comments on commit 9e74692

Please sign in to comment.