Skip to content

Commit

Permalink
wip: add runs and messages api
Browse files Browse the repository at this point in the history
  • Loading branch information
aallam committed Nov 16, 2023
1 parent 9a125a8 commit 38429ab
Show file tree
Hide file tree
Showing 25 changed files with 1,149 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ public interface Assistants {
* and [SortOrder.Descending] for descending order.
* @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="obj_foo"` in order to fetch the next page of the list.
* include `after = AssistantId("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="obj_foo"` in order to fetch the previous page of the list.
* include `before = AssistantId("obj_foo")` in order to fetch the previous page of the list.
*/
@BetaOpenAI
public suspend fun assistants(
limit: Int? = null,
order: SortOrder? = null,
after: String? = null,
before: String? = null,
after: AssistantId? = null,
before: AssistantId? = null,
): List<Assistant>

/**
Expand Down Expand Up @@ -104,17 +104,17 @@ public interface Assistants {
* and [SortOrder.Descending] for descending order.
* @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="obj_foo"` in order to fetch the next page of the list.
* include `after = FileId("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="obj_foo"` in order to fetch the previous page of the list.
* include `before = FileId("obj_foo")` in order to fetch the previous page of the list.
*/
@BetaOpenAI
public suspend fun files(
id: AssistantId,
limit: Int? = null,
order: SortOrder? = null,
after: String? = null,
before: String? = null,
after: FileId? = null,
before: FileId? = null,
): List<AssistantFile>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.aallam.openai.client

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.MessageFile
import com.aallam.openai.api.message.MessageId
import com.aallam.openai.api.message.MessageRequest
import com.aallam.openai.api.thread.ThreadId

/**
* Create messages within threads
*/
public interface Messages {

/**
* Create a message.
*
* @param threadId the identifier of the thread
* @param message message creation request
*/
public suspend fun message(threadId: ThreadId, message: MessageRequest): Message

/**
* Retrieve a message.
*
* @param threadId the identifier of the thread
* @param messageId the identifier of the message
*/
public suspend fun message(threadId: ThreadId, messageId: MessageId): Message

/**
* Modify a message.
*
* @param threadId the identifier of the thread
* @param messageId the identifier of the message
* @param metadata set of 16 key-value pairs that can be attached to an object.
* This can be useful for storing additional information about the object in a structured format.
* Keys can be a maximum of 64 characters long, and values can be a maximum of 512 characters long.
*/
public suspend fun message(threadId: ThreadId, messageId: MessageId, metadata: Map<String, String>? = null): Message

/**
* Returns a list of messages for a given thread.
*
* @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.
*/
public suspend fun messages(
threadId: ThreadId,
limit: Int? = null,
order: SortOrder? = null,
after: MessageId? = null,
before: MessageId? = null,
): List<Message>

/**
* Retrieves a message file.
*
* @param threadId the ID of the thread to which the message and File belong
* @param messageId the ID of the message the file belongs to
* @param fileId the ID of the file being retrieved
*/
public suspend fun messageFile(threadId: ThreadId, messageId: MessageId, fileId: FileId): MessageFile

/**
* Returns a list of message files.
*
* @param threadId the ID of the thread to which the message and File belong to
* @param messageId the ID of the message the file belongs to
* @param limit a limit on the number of objects to be returned
* @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 = FileId("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 = FileId("obj_foo")` in order to fetch the previous page of the list.
*/
public suspend fun messageFiles(
threadId: ThreadId,
messageId: MessageId,
limit: Int? = null,
order: SortOrder? = null,
after: FileId? = null,
before: FileId? = null,
)
}
111 changes: 111 additions & 0 deletions openai-client/src/commonMain/kotlin/com.aallam.openai.client/Runs.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.aallam.openai.client

import com.aallam.openai.api.core.SortOrder
import com.aallam.openai.api.core.Status
import com.aallam.openai.api.run.*
import com.aallam.openai.api.thread.ThreadId

/**
* Represents an execution run on a thread.
*/
public interface Runs {

/**
* Create a run.
*
* @param threadId The ID of the thread to run
* @param request request for a run
*
*/
public suspend fun createRun(threadId: ThreadId, request: RunRequest): Run

/**
* Retrieves a run.
*
* @param threadId The ID of the thread to run
* @param runId The ID of the run to retrieve
*/
public suspend fun retrieveRun(threadId: ThreadId, runId: String): Run

/**
* Retrieves a run.
*
* @param threadId the ID of the thread to run
* @param runId the ID of the run to retrieve
* @param metadata set of 16 key-value pairs that can be attached to an object.
* This can be useful for storing additional information about the object in a structured format.
* Keys can be a maximum of 64 characters long, and values can be a maximum of 512 characters long.
*/
public suspend fun modifyRun(threadId: ThreadId, runId: String, metadata: Map<String, String>? = null): Run

/**
* Returns a list of runs belonging to a thread.
*
* @param threadId the ID of the thread the run belongs to
* @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 = RunId("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 = RunId("obj_foo")` in order to fetch the previous page of the list.
*/
public suspend fun listRuns(
threadId: ThreadId,
order: SortOrder? = null,
after: RunId? = null,
before: RunId? = null,
): List<Run>

/**
* When a run has the status: [Status.RequiresAction] and required action is [RequiredAction.SubmitToolOutputs],
* this endpoint can be used to submit the outputs from the tool calls once they're all completed.
* All outputs must be submitted in a single request.
*/
public suspend fun submitToolOutput(threadId: ThreadId, runId: String, output: List<ToolOutput>): Run

/**
* Cancels a run that is [Status.InProgress].
*
* @param threadId the ID of the thread to which this run belongs
* @param runId the ID of the run to cancel
*/
public suspend fun cancel(threadId: ThreadId, runId: String): Run

/**
* Create a thread and run it in one request.
*
* @param request request for a thread run
*/
public suspend fun createThreadRun(request: RunRequest): Run

/**
* Retrieves a run step.
*
* @param threadId the ID of the thread to which the run and run step belongs
* @param runId the ID of the run to which the run step belongs
* @param stepId the ID of the run step to retrieve
*/
public suspend fun getRunStep(threadId: ThreadId, runId: RunId, stepId: RunStepId): RunStep

/**
* Retrieves a run step.
*
* @param threadId the ID of the thread to which the run and run step belongs
* @param runId the ID of the run to which the run step belongs
* @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 = RunId("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 = RunId("obj_foo")` in order to fetch the previous page of the list.
*/
public suspend fun listRunSteps(
threadId: ThreadId,
runId: RunId,
order: SortOrder? = null,
after: RunStepId? = null,
before: RunStepId? = null,
): List<RunStep>
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ internal class OpenAIApi(
FineTuning by FineTuningApi(requester),
Assistants by AssistantsApi(requester),
Threads by ThreadsApi(requester),
Runs by RunsApi(requester),
Messages by MessagesApi(requester),
Closeable by requester
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.aallam.openai.client.internal.api

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.MessageFile
import com.aallam.openai.api.message.MessageId
import com.aallam.openai.api.message.MessageRequest
import com.aallam.openai.api.thread.ThreadId
import com.aallam.openai.client.Messages
import com.aallam.openai.client.internal.http.HttpRequester

internal class MessagesApi(requester: HttpRequester) : Messages {
override suspend fun message(threadId: ThreadId, message: MessageRequest): Message {
TODO("Not yet implemented")
}

override suspend fun message(threadId: ThreadId, messageId: MessageId): Message {
TODO("Not yet implemented")
}

override suspend fun message(threadId: ThreadId, messageId: MessageId, metadata: Map<String, String>?): Message {
TODO("Not yet implemented")
}

override suspend fun messages(
threadId: ThreadId,
limit: Int?,
order: SortOrder?,
after: MessageId?,
before: MessageId?
): List<Message> {
TODO("Not yet implemented")
}

override suspend fun messageFile(threadId: ThreadId, messageId: MessageId, fileId: FileId): MessageFile {
TODO("Not yet implemented")
}

override suspend fun messageFiles(
threadId: ThreadId,
messageId: MessageId,
limit: Int?,
order: SortOrder?,
after: FileId?,
before: FileId?
) {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.aallam.openai.client.internal.api

import com.aallam.openai.api.core.SortOrder
import com.aallam.openai.api.run.*
import com.aallam.openai.api.thread.ThreadId
import com.aallam.openai.client.Runs
import com.aallam.openai.client.internal.http.HttpRequester

internal class RunsApi(requester: HttpRequester) : Runs {
override suspend fun createRun(threadId: ThreadId, request: RunRequest): Run {
TODO("Not yet implemented")
}

override suspend fun retrieveRun(threadId: ThreadId, runId: String): Run {
TODO("Not yet implemented")
}

override suspend fun modifyRun(threadId: ThreadId, runId: String, metadata: Map<String, String>?): Run {
TODO("Not yet implemented")
}

override suspend fun listRuns(threadId: ThreadId, order: SortOrder?, after: RunId?, before: RunId?): List<Run> {
TODO("Not yet implemented")
}

override suspend fun submitToolOutput(threadId: ThreadId, runId: String, output: List<ToolOutput>): Run {
TODO("Not yet implemented")
}

override suspend fun cancel(threadId: ThreadId, runId: String): Run {
TODO("Not yet implemented")
}

override suspend fun createThreadRun(request: RunRequest): Run {
TODO("Not yet implemented")
}

override suspend fun getRunStep(threadId: ThreadId, runId: RunId, stepId: RunStepId): RunStep {
TODO("Not yet implemented")
}

override suspend fun listRunSteps(
threadId: ThreadId,
runId: RunId,
order: SortOrder?,
after: RunStepId?,
before: RunStepId?
): List<RunStep> {
TODO("Not yet implemented")
}
}
Loading

0 comments on commit 38429ab

Please sign in to comment.