Skip to content

Commit

Permalink
Refine Azure API
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddenton committed Sep 7, 2024
1 parent 9e74692 commit 7fc8147
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 20 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ changes with their rationale when appropriate. Given version `A.B.C.D`, breaking

### 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.
- **http4k-connect-ai-openai*** - [Breaking] Tightened up types for completion requests.
- **http4k-connect-ai-azure*** - [Breaking] Tightened up types for completion requests.

### v5.23.0.0
- **http4k-connect-*** - Upgrade dependencies including Kotlin to 2.0.20
Expand Down
2 changes: 1 addition & 1 deletion ai/azure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ val client = AzureAI.Http(

// all operations return a Result monad of the API type
val result: Result<Sequence<CompletionResponse>, RemoteFailure> = client
.chatCompletion(ModelName.of("Meta-Llama-3.1-70B-Instruct"), listOf(Message(User, "good afternoon")), 1000, true)
.chatCompletion(ModelName.of("Meta-Llama-3.1-70B-Instruct"), listOf(Message.User("good afternoon"))), 1000, true)

println(result.orThrow().toList())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ package org.http4k.connect.azure.action

import org.http4k.connect.Http4kConnectAction
import org.http4k.connect.azure.AzureAIMoshi.autoBody
import org.http4k.connect.model.ModelName
import org.http4k.connect.azure.CompletionId
import org.http4k.connect.azure.ObjectType
import org.http4k.connect.azure.User
import org.http4k.connect.azure.action.Detail.auto
import org.http4k.connect.model.FinishReason
import org.http4k.connect.model.ModelName
import org.http4k.connect.model.Role
import org.http4k.connect.model.Timestamp
import org.http4k.core.Method
Expand Down Expand Up @@ -52,6 +52,14 @@ data class ChatCompletion(
tool_choice = null
)

constructor(model: ModelName, message: Message, max_tokens: Int = 16, stream: Boolean = true) : this(
model,
listOf(message),
max_tokens,
stream = stream,
tool_choice = null
)

init {
require(tools == null || tools.isNotEmpty()) { "Tools cannot be empty" }
}
Expand Down Expand Up @@ -79,18 +87,40 @@ sealed class ResponseFormat {

@JsonSerializable
data class Message(
val role: Role?,
val content: List<MessageContent>,
val role: Role,
val content: List<MessageContent>?,
val name: User? = null,
val tool_calls: List<ToolCall>? = null
) {
@Deprecated("Use companion functions")
constructor(
role: Role,
text: String,
name: User? = null,
tool_calls: List<ToolCall>? = null
) :
this(role, listOf(MessageContent(ContentType.text, text)), name, 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) =
Assistant(listOf(MessageContent(ContentType.text, content)), name)

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

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

}

@JsonSerializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ interface AzureAIContract {
val responses = azureAi.chatCompletion(
ModelName.of("Meta-Llama-3-8B-Instruct"),
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 @@ -63,11 +63,8 @@ interface AzureAIContract {
val responses = azureAi.chatCompletion(
ModelName.of("Meta-Llama-3-8B-Instruct"),
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 @@ -23,9 +23,7 @@ fun main() {
println(
azureAi.chatCompletion(
ModelName.of("Meta-Llama-3.1-70B-Instruct"),
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/azure/fake/src/examples/kotlin/using the fake.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun main() {
azureAi
.chatCompletion(
ModelName.of("Meta-Llama-3.1-70B-Instruct"),
listOf(Message(User, "good afternoon")), 1000, true
listOf(Message.User("good afternoon")), 1000, true
)
.onFailure { error(it) }
.toList()
Expand Down
2 changes: 1 addition & 1 deletion ai/azure/fake/src/examples/kotlin/using_connect_client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fun main() {

// all operations return a Result monad of the API type
val result: Result<Sequence<CompletionResponse>, RemoteFailure> = client
.chatCompletion(ModelName.of("Meta-Llama-3.1-70B-Instruct"), listOf(Message(User, "good afternoon")), 1000, true)
.chatCompletion(ModelName.of("Meta-Llama-3.1-70B-Instruct"), listOf(Message.User("good afternoon")), 1000, true)

println(result.orThrow().toList())
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ fun interface ChatCompletionGenerator : (ModelCompletion) -> List<Choice> {
val ChatCompletionGenerator.Companion.ReverseInput
get() = ChatCompletionGenerator { req ->
req.content().flatMap { m ->
m.content.mapIndexed { i, content ->
m.content?.mapIndexed { i, content ->
Choice(i, ChoiceDetail(Role.System, content.text?.reversed() ?: "", null), null, FinishReason.stop)
}
} ?: emptyList()
}
}

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

private fun ModelCompletion.choices(msg: String) = (if (stream) msg.split(" ").map { "$it " } else listOf(msg))
Expand Down

0 comments on commit 7fc8147

Please sign in to comment.