Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE]Get response headers from messages api #380

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package com.aallam.openai.client
import com.aallam.openai.api.BetaOpenAI
import com.aallam.openai.api.core.RequestOptions
import com.aallam.openai.api.core.SortOrder
import com.aallam.openai.api.file.FileId
import com.aallam.openai.api.message.Message
import com.aallam.openai.api.message.MessageId
import com.aallam.openai.api.message.MessageRequest
import com.aallam.openai.api.thread.ThreadId
import io.ktor.http.*

/**
* Create messages within threads
Expand Down Expand Up @@ -85,4 +85,29 @@ public interface Messages {
before: MessageId? = null,
requestOptions: RequestOptions? = null
): List<Message>

/**
* Returns a list of messages for a given thread with response headers.
*
* @param threadId the identifier of the thread
* @param limit a limit on the number of objects to be returned.
* The Limit can range between 1 and 100, and the default is 20.
* @param order sort order by the `created_at` timestamp of the objects.
* @param after a cursor for use in pagination. [after] is an object ID that defines your place in the list.
* For instance, if you make a list request and receive 100 objects, ending with `obj_foo`, your subsequent call
* can include `after = MessageId("obj_foo")` in order to fetch the next page of the list.
* @param before a cursor for use in pagination. [before] is an object ID that defines your place in the list.
* For instance, if you make a list request and receive 100 objects, ending with `obj_foo`, your subsequent call
* can include `before = MessageId("obj_foo")` in order to fetch the previous page of the list.
* @param requestOptions request options.
*/
@BetaOpenAI
public suspend fun messagesWithHeaders(
threadId: ThreadId,
limit: Int? = null,
order: SortOrder? = null,
after: MessageId? = null,
before: MessageId? = null,
requestOptions: RequestOptions? = null
): Pair<Headers, List<Message>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.aallam.openai.client.internal.extension.beta
import com.aallam.openai.client.internal.extension.requestOptions
import com.aallam.openai.client.internal.http.HttpRequester
import com.aallam.openai.client.internal.http.perform
import com.aallam.openai.client.internal.http.performGetHeaders
import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.http.*
Expand Down Expand Up @@ -74,7 +75,29 @@ internal class MessagesApi(val requester: HttpRequester) : Messages {
before: MessageId?,
requestOptions: RequestOptions?
): PaginatedList<Message> {
return requester.perform {
return requester.perform{
it.get {
url(path = "${ApiPath.Threads}/${threadId.id}/messages") {
limit?.let { value -> parameter("limit", value) }
order?.let { value -> parameter("order", value.order) }
before?.let { value -> parameter("before", value.id) }
after?.let { value -> parameter("after", value.id) }
}
beta("assistants", 2)
requestOptions(requestOptions)
}.body()
}
}

override suspend fun messagesWithHeaders(
threadId: ThreadId,
limit: Int?,
order: SortOrder?,
after: MessageId?,
before: MessageId?,
requestOptions: RequestOptions?
): Pair<Headers, PaginatedList<Message>> {
return requester.performGetHeaders{
it.get {
url(path = "${ApiPath.Threads}/${threadId.id}/messages") {
limit?.let { value -> parameter("limit", value) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.aallam.openai.client.Closeable
import io.ktor.client.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.reflect.*

/**
Expand All @@ -16,6 +17,11 @@ internal interface HttpRequester : Closeable {
*/
suspend fun <T : Any> perform(info: TypeInfo, block: suspend (HttpClient) -> HttpResponse): T

/**
* Perform an HTTP request and get a result with headers
*/
suspend fun <T : Any> performGetHeaders(info: TypeInfo, block: suspend (HttpClient) -> HttpResponse): Pair<Headers, T>

/**
* Perform an HTTP request and get a result.
*
Expand All @@ -33,3 +39,10 @@ internal interface HttpRequester : Closeable {
internal suspend inline fun <reified T> HttpRequester.perform(noinline block: suspend (HttpClient) -> HttpResponse): T {
return perform(typeInfo<T>(), block)
}

/**
* Perform an HTTP request and get a result with headers
*/
internal suspend inline fun <reified T> HttpRequester.performGetHeaders(noinline block: suspend (HttpClient) -> HttpResponse): Pair<Headers, T> {
return performGetHeaders(typeInfo<T>(), block)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.ktor.client.network.sockets.*
import io.ktor.client.plugins.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.util.reflect.*
import io.ktor.utils.io.errors.*
import kotlinx.coroutines.CancellationException
Expand Down Expand Up @@ -35,6 +36,15 @@ internal class HttpTransport(private val httpClient: HttpClient) : HttpRequester
}
}

override suspend fun <T : Any> performGetHeaders(info: TypeInfo, block: suspend (HttpClient) -> HttpResponse): Pair<Headers, T> {
try {
val response = block(httpClient)
return Pair(response.headers,response.body(info))
} catch (e: Exception) {
throw handleException(e)
}
}

override fun close() {
httpClient.close()
}
Expand Down