Skip to content

Commit

Permalink
OpenAIService - new functions/endpoints - modifyAssistant
Browse files Browse the repository at this point in the history
  • Loading branch information
branislav-burdiliak committed Feb 15, 2024
1 parent a80220d commit e6c0ecb
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package io.cequence.openaiscala.service

import akka.stream.scaladsl.Source
import akka.util.ByteString
import play.api.libs.json.{JsArray, JsObject, JsValue, Json}
import io.cequence.openaiscala.JsonUtil.JsonOps
import io.cequence.openaiscala.JsonFormats._
import io.cequence.openaiscala.JsonUtil.JsonOps
import io.cequence.openaiscala.OpenAIScalaClientException
import io.cequence.openaiscala.domain.settings._
import io.cequence.openaiscala.domain.response._
import io.cequence.openaiscala.domain.settings._
import io.cequence.openaiscala.domain.{
AssistantTool,
BaseMessage,
ChatRole,
FileId,
FunctionSpec,
SortOrder,
Thread,
Expand All @@ -21,6 +19,7 @@ import io.cequence.openaiscala.domain.{
ThreadMessageFile,
ToolSpec
}
import play.api.libs.json.{JsObject, JsValue, Json}

import java.io.File
import scala.concurrent.Future
Expand Down Expand Up @@ -770,16 +769,45 @@ private trait OpenAIServiceImpl extends OpenAICoreServiceImpl with OpenAIService
* Retrieves an AssistantFile.
*
* @param assistantId
* The ID of the assistant who the file belongs to.
* The ID of the assistant who the file belongs to.
* @param fileId
* The ID of the file we're getting.
* The ID of the file we're getting.
*/
override def retrieveAssistantFile(assistantId: String, fileId: String): Future[Option[AssistantFile]] =
override def retrieveAssistantFile(
assistantId: String,
fileId: String
): Future[Option[AssistantFile]] =
execGETWithStatus(
EndPoint.assistants,
Some(s"$assistantId/files/$fileId")
).map { response =>
handleNotFoundAndError(response).map(_.asSafe[AssistantFile])
}

override def modifyAssistant(
assistantId: String,
model: Option[String],
name: Option[String],
description: Option[String],
instructions: Option[String],
tools: Seq[AssistantTool],
fileIds: Seq[String],
metadata: Map[String, String]
): Future[Option[Assistant]] =
execPOSTWithStatus(
EndPoint.assistants,
endPointParam = Some(assistantId),
bodyParams = jsonBodyParams(
Param.model -> model,
Param.name -> name,
Param.description -> description,
Param.instructions -> instructions,
Param.tools -> Some(Json.toJson(tools)),
Param.file_ids -> (if (fileIds.nonEmpty) Some(fileIds) else None),
Param.metadata -> (if (metadata.nonEmpty) Some(metadata) else None)
)
).map { response =>
handleNotFoundAndError(response).map(_.asSafe[Assistant])
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object AssistantTool {
// FIXME: description should be optional in request and mandatory in response
description: String,
name: String,
parameters: Option[String]
parameters: Option[String] // TODO: check representation
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,10 @@ trait OpenAIService extends OpenAICoreService {
* @param fileId
* A File ID (with purpose="assistants") that the assistant should use. Useful for tools
* like `retrieval` and `code_interpreter` that can access files.
* @return
* @see
* <a
* href="https://platform.openai.com/docs/api-reference/assistants/createAssistantFile">OpenAI
* Doc</a>
*/
def createAssistantFile(
assistantId: String,
Expand All @@ -786,6 +789,10 @@ trait OpenAIService extends OpenAICoreService {
* 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.
* @see
* <a
* href="https://platform.openai.com/docs/api-reference/assistants/listAssistants">OpenAI
* Doc</a>
*/
def listAssistants(
limit: Option[Int] = None, // TODO: default 20 or None?
Expand Down Expand Up @@ -817,6 +824,9 @@ trait OpenAIService extends OpenAICoreService {
* 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.
* <a
* href="https://platform.openai.com/docs/api-reference/assistants/listAssistantFiles">OpenAI
* Doc</a>
*/
def listAssistantFiles(
assistantId: String,
Expand All @@ -831,6 +841,9 @@ trait OpenAIService extends OpenAICoreService {
*
* @param assistantId
* The ID of the assistant to retrieve.
* <a
* href="https://platform.openai.com/docs/api-reference/assistants/retrieveAssistant">OpenAI
* Doc</a>
*/
def retrieveAssistant(assistantId: String): Future[Option[Assistant]]

Expand All @@ -841,7 +854,49 @@ trait OpenAIService extends OpenAICoreService {
* The ID of the assistant who the file belongs to.
* @param fileId
* The ID of the file we're getting.
* <a
* href="https://platform.openai.com/docs/api-reference/assistants/retrieveAssistantFile">OpenAI
* Doc</a>
*/
def retrieveAssistantFile(assistantId: String, fileId: String): Future[Option[AssistantFile]]

/**
* Modifies an assistant.
*
* @param assistantId
* @param model
* ID of the model to use. You can use the List models API to see all of your available models,
* or see our Model overview for descriptions of them.
* @param name
* The name of the assistant. The maximum length is 256 characters.
* @param description
* The description of the assistant. The maximum length is 512 characters.
* @param instructions
* The system instructions that the assistant uses. The maximum length is 32768 characters.
* @param tools
* A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types
* code_interpreter, retrieval, or function.
* @param fileIds
* A list of File IDs attached to this assistant. There can be a maximum of 20 files attached to the assistant.
* Files are ordered by their creation date in ascending order. If a file was previously attached to the list but
* does not show up in the list, it will be deleted from the assistant.
* @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 maxium of 512 characters long.
* <a
* href="https://platform.openai.com/docs/api-reference/assistants/modifyAssistant">OpenAI
* Doc</a>
*/
def modifyAssistant(
assistantId: String,
model: Option[String] = None,
name: Option[String] = None,
description: Option[String] = None,
instructions: Option[String] = None,
tools: Seq[AssistantTool] = Seq.empty[AssistantTool],
fileIds: Seq[String] = Seq.empty,
metadata: Map[String, String] = Map.empty
): Future[Option[Assistant]]

}
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,35 @@ trait OpenAIServiceWrapper extends OpenAIService {
override def retrieveAssistant(assistantId: String): Future[Option[Assistant]] =
wrap(_.retrieveAssistant(assistantId))

override def retrieveAssistantFile(assistantId: String, fileId: String): Future[Option[AssistantFile]] =
override def retrieveAssistantFile(
assistantId: String,
fileId: String
): Future[Option[AssistantFile]] =
wrap(_.retrieveAssistantFile(assistantId, fileId))

override def modifyAssistant(
assistantId: String,
model: Option[String],
name: Option[String],
description: Option[String],
instructions: Option[String],
tools: Seq[AssistantTool],
fileIds: Seq[String],
metadata: Map[String, String]
): Future[Option[Assistant]] =
wrap(
_.modifyAssistant(
assistantId,
model,
name,
description,
instructions,
tools,
fileIds,
metadata
)
)

protected def wrap[T](
fun: OpenAIService => Future[T]
): Future[T]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.cequence.openaiscala.examples

object ModifyAssistant extends Example {

override protected def run =
for {
thread <- service.modifyAssistant(
assistantId = "asst_Btbc2h7dqyDU52g1KfBa2XE8",
metadata = Map("user_id" -> "986415", "due_date" -> "2028-08-01")
)
} yield {
println(thread)
}
}

0 comments on commit e6c0ecb

Please sign in to comment.