Skip to content

Commit d2591f8

Browse files
author
michal.zyga
committed
add FineTuningResponse
1 parent 28e5f76 commit d2591f8

File tree

8 files changed

+370
-134
lines changed

8 files changed

+370
-134
lines changed

core/src/main/scala/sttp/openai/requests/finetuning/FineTuningRequestBody.scala

-133
Original file line numberDiff line numberDiff line change
@@ -41,139 +41,6 @@ object FineTuningRequestBody {
4141
implicit val fineTuningRequestBodyWriter: SnakePickle.Writer[FineTuningRequestBody] = SnakePickle.macroW[FineTuningRequestBody]
4242
}
4343

44-
sealed abstract class Type(val value: String)
45-
46-
object Type {
47-
implicit def typeRW(implicit byTypeValue: Map[String, Type]): SnakePickle.ReadWriter[Type] = SnakePickle
48-
.readwriter[ujson.Value]
49-
.bimap[Type](
50-
`type` => SnakePickle.writeJs(`type`.value),
51-
jsonValue =>
52-
SnakePickle.read[ujson.Value](jsonValue) match {
53-
case Str(value) =>
54-
byTypeValue.get(value) match {
55-
case Some(t) => t
56-
case None => throw DeserializationOpenAIException(new Exception(s"Could not deserialize: $value"))
57-
}
58-
case e => throw DeserializationOpenAIException(new Exception(s"Could not deserialize: $e"))
59-
}
60-
)
61-
}
62-
63-
/** @param batchSize
64-
* Number of examples in each batch. A larger batch size means that model parameters are updated less frequently, but with lower
65-
* variance.
66-
* @param learningRateMultiplier
67-
* Scaling factor for the learning rate. A smaller learning rate may be useful to avoid overfitting.
68-
* @param nEpochs
69-
* The number of epochs to train the model for. An epoch refers to one full cycle through the training dataset.
70-
* @param beta
71-
* The beta value for the DPO method. A higher beta value will increase the weight of the penalty between the policy and reference model.
72-
*/
73-
case class Hyperparameters(
74-
batchSize: Option[Int] = None,
75-
learningRateMultiplier: Option[Float] = None,
76-
nEpochs: Option[Int] = None,
77-
beta: Option[Float] = None
78-
)
79-
80-
object Hyperparameters {
81-
implicit val hyperparametersW: SnakePickle.Writer[Hyperparameters] = SnakePickle.macroW[Hyperparameters]
82-
}
83-
84-
/** @param hyperparameters
85-
* The hyperparameters used for the fine-tuning job.
86-
*/
87-
case class Supervised(
88-
hyperparameters: Option[Hyperparameters] = None
89-
)
90-
91-
object Supervised {
92-
implicit val supervisedW: SnakePickle.Writer[Supervised] = SnakePickle.macroW[Supervised]
93-
}
94-
95-
/** @param hyperparameters
96-
* The hyperparameters used for the fine-tuning job.
97-
*/
98-
case class Dpo(
99-
hyperparameters: Option[Hyperparameters] = None
100-
)
101-
102-
object Dpo {
103-
implicit val dpoW: SnakePickle.Writer[Dpo] = SnakePickle.macroW[Dpo]
104-
}
105-
106-
/** @param `type`
107-
* The type of method. Is either supervised or dpo.
108-
* @param supervised
109-
* Configuration for the supervised fine-tuning method.
110-
* @param dpo
111-
* Configuration for the DPO fine-tuning method.
112-
*/
113-
case class Method(
114-
`type`: Option[Type] = None,
115-
supervised: Option[Supervised] = None,
116-
dpo: Option[Dpo] = None
117-
)
118-
119-
object Method {
120-
implicit val methodW: SnakePickle.Writer[Method] = SnakePickle.macroW[Method]
121-
122-
case object Supervised extends Type("supervised")
123-
124-
case object Dpo extends Type("dpo")
125-
126-
private val values: Set[Type] = Set(Supervised, Dpo)
127-
128-
implicit val byTypeValue: Map[String, Type] = values.map(`type` => `type`.value -> `type`).toMap
129-
}
130-
131-
/** @param project
132-
* The name of the project that the new run will be created under.
133-
* @param name
134-
* A display name to set for the run. If not set, we will use the Job ID as the name.
135-
* @param entity
136-
* The entity to use for the run. This allows you to set the team or username of the WandB user that you would like associated with the
137-
* run. If not set, the default entity for the registered WandB API key is used.
138-
* @param tags
139-
* A list of tags to be attached to the newly created run. These tags are passed through directly to WandB. Some default tags are
140-
* generated by OpenAI: "openai/finetune", "openai/{base-model}", "openai/{ftjob-abcdef}".
141-
*/
142-
case class Wandb(
143-
project: String,
144-
name: Option[String] = None,
145-
entity: Option[String] = None,
146-
tags: Option[Seq[String]]
147-
)
148-
149-
object Wandb {
150-
implicit val wandbW: SnakePickle.Writer[Wandb] = SnakePickle.macroW[Wandb]
151-
}
152-
153-
/** @param `type`
154-
* The type of integration to enable. Currently, only "wandb" (Weights and Biases) is supported.
155-
* @param wandb
156-
* The settings for your integration with Weights and Biases. This payload specifies the project that metrics will be sent to.
157-
* Optionally, you can set an explicit display name for your run, add tags to your run, and set a default entity (team, username, etc) to
158-
* be associated with your run.
159-
*/
160-
case class Integration(
161-
`type`: Type,
162-
wandb: Wandb
163-
)
164-
165-
object Integration {
166-
implicit val integrationW: SnakePickle.Writer[Integration] = SnakePickle.macroW[Integration]
167-
168-
case object Wandb extends Type("wandb")
169-
170-
private val values: Set[Type] = Set(Wandb)
171-
172-
private val byTypeValue = values.map(`type` => `type`.value -> `type`).toMap
173-
174-
implicit val typeRW: SnakePickle.ReadWriter[Type] = Type.typeRW(byTypeValue)
175-
}
176-
17744
sealed abstract class FineTuningModel(val value: String)
17845

17946
object FineTuningModel {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package sttp.openai.requests.finetuning
2+
3+
import sttp.openai.OpenAIExceptions.OpenAIException.DeserializationOpenAIException
4+
import sttp.openai.json.SnakePickle
5+
import ujson.Str
6+
7+
case class FineTuningResponse(
8+
id: String,
9+
createdAt: Int,
10+
error: Option[Error] = None,
11+
fineTunedModel: Option[String],
12+
finishedAt: Option[Int],
13+
hyperparameters: Option[Hyperparameters],
14+
model: String,
15+
`object`: String,
16+
organizationId: String,
17+
resultFiles: Seq[String],
18+
status: Status,
19+
trainedTokens: Option[Int] = None,
20+
trainingFile: String,
21+
validationFile: Option[String],
22+
integrations: Option[Seq[Integration]] = None,
23+
seed: Int,
24+
estimatedFinish: Option[Int] = None,
25+
method: Method
26+
)
27+
28+
object FineTuningResponse {
29+
implicit val fineTuningResponseDataReader: SnakePickle.Reader[FineTuningResponse] = SnakePickle.macroR[FineTuningResponse]
30+
}
31+
32+
/** @param code
33+
* A machine-readable error code.
34+
* @param message
35+
* A human-readable error message.
36+
* @param param
37+
* The parameter that was invalid, usually training_file or validation_file. This field will be null if the failure was not
38+
* parameter-specific.
39+
*/
40+
case class Error(
41+
code: String,
42+
message: String,
43+
param: Option[String] = None
44+
)
45+
46+
object Error {
47+
implicit val errorReader: SnakePickle.Reader[Error] = SnakePickle.macroR[Error]
48+
}
49+
50+
sealed abstract class Status(val value: String)
51+
52+
object Status {
53+
54+
implicit val statusRW: SnakePickle.Reader[Status] = SnakePickle
55+
.reader[ujson.Value]
56+
.map[Status](
57+
jsonValue =>
58+
SnakePickle.read[ujson.Value](jsonValue) match {
59+
case Str(value) => byStatusValue.getOrElse(value, CustomStatus(value))
60+
case e => throw DeserializationOpenAIException(new Exception(s"Could not deserialize: $e"))
61+
}
62+
)
63+
64+
case object ValidatingFiles extends Status("validating_files")
65+
66+
case object Queued extends Status("queued")
67+
68+
case object Running extends Status("running")
69+
70+
case object Succeeded extends Status("succeeded")
71+
72+
case object Failed extends Status("failed")
73+
74+
case object Cancelled extends Status("cancelled")
75+
76+
case class CustomStatus(customStatus: String) extends Status(customStatus)
77+
78+
private val values: Set[Status] = Set(ValidatingFiles, Queued, Running, Succeeded, Failed, Cancelled)
79+
80+
private val byStatusValue = values.map(status => status.value -> status).toMap
81+
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package sttp.openai.requests.finetuning
2+
3+
import sttp.openai.json.SnakePickle
4+
5+
/** @param batchSize
6+
* Number of examples in each batch. A larger batch size means that model parameters are updated less frequently, but with lower
7+
* variance.
8+
* @param learningRateMultiplier
9+
* Scaling factor for the learning rate. A smaller learning rate may be useful to avoid overfitting.
10+
* @param nEpochs
11+
* The number of epochs to train the model for. An epoch refers to one full cycle through the training dataset.
12+
* @param beta
13+
* The beta value for the DPO method. A higher beta value will increase the weight of the penalty between the policy and reference model.
14+
*/
15+
case class Hyperparameters(
16+
batchSize: Option[Int] = None,
17+
learningRateMultiplier: Option[Float] = None,
18+
nEpochs: Option[Int] = None,
19+
beta: Option[Float] = None
20+
)
21+
22+
object Hyperparameters {
23+
implicit val hyperparametersW: SnakePickle.ReadWriter[Hyperparameters] = SnakePickle.macroRW[Hyperparameters]
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sttp.openai.requests.finetuning
2+
3+
import sttp.openai.json.SnakePickle
4+
5+
/** @param `type`
6+
* The type of integration to enable. Currently, only "wandb" (Weights and Biases) is supported.
7+
* @param wandb
8+
* The settings for your integration with Weights and Biases. This payload specifies the project that metrics will be sent to.
9+
* Optionally, you can set an explicit display name for your run, add tags to your run, and set a default entity (team, username, etc) to
10+
* be associated with your run.
11+
*/
12+
case class Integration(
13+
`type`: Type,
14+
wandb: Wandb
15+
)
16+
17+
object Integration {
18+
implicit val integrationRW: SnakePickle.ReadWriter[Integration] = SnakePickle.macroRW[Integration]
19+
20+
case object Wandb extends Type("wandb")
21+
22+
private val values: Set[Type] = Set(Wandb)
23+
24+
private val byTypeValue = values.map(`type` => `type`.value -> `type`).toMap
25+
26+
implicit val typeRW: SnakePickle.ReadWriter[Type] = Type.typeRW(byTypeValue)
27+
}
28+
29+
/** @param project
30+
* The name of the project that the new run will be created under.
31+
* @param name
32+
* A display name to set for the run. If not set, we will use the Job ID as the name.
33+
* @param entity
34+
* The entity to use for the run. This allows you to set the team or username of the WandB user that you would like associated with the
35+
* run. If not set, the default entity for the registered WandB API key is used.
36+
* @param tags
37+
* A list of tags to be attached to the newly created run. These tags are passed through directly to WandB. Some default tags are
38+
* generated by OpenAI: "openai/finetune", "openai/{base-model}", "openai/{ftjob-abcdef}".
39+
*/
40+
case class Wandb(
41+
project: String,
42+
name: Option[String] = None,
43+
entity: Option[String] = None,
44+
tags: Option[Seq[String]]
45+
)
46+
47+
object Wandb {
48+
implicit val wandbRW: SnakePickle.ReadWriter[Wandb] = SnakePickle.macroRW[Wandb]
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sttp.openai.requests.finetuning
2+
3+
import sttp.openai.json.SnakePickle
4+
5+
/** @param `type`
6+
* The type of method. Is either supervised or dpo.
7+
* @param supervised
8+
* Configuration for the supervised fine-tuning method.
9+
* @param dpo
10+
* Configuration for the DPO fine-tuning method.
11+
*/
12+
case class Method(
13+
`type`: Option[Type] = None,
14+
supervised: Option[Supervised] = None,
15+
dpo: Option[Dpo] = None
16+
)
17+
18+
object Method {
19+
implicit val methodRW: SnakePickle.ReadWriter[Method] = SnakePickle.macroRW[Method]
20+
21+
case object Supervised extends Type("supervised")
22+
23+
case object Dpo extends Type("dpo")
24+
25+
private val values: Set[Type] = Set(Supervised, Dpo)
26+
27+
implicit val byTypeValue: Map[String, Type] = values.map(`type` => `type`.value -> `type`).toMap
28+
}
29+
30+
/** @param hyperparameters
31+
* The hyperparameters used for the fine-tuning job.
32+
*/
33+
case class Supervised(
34+
hyperparameters: Option[Hyperparameters] = None
35+
)
36+
37+
object Supervised {
38+
implicit val supervisedRW: SnakePickle.ReadWriter[Supervised] = SnakePickle.macroRW[Supervised]
39+
}
40+
41+
/** @param hyperparameters
42+
* The hyperparameters used for the fine-tuning job.
43+
*/
44+
case class Dpo(
45+
hyperparameters: Option[Hyperparameters] = None
46+
)
47+
48+
object Dpo {
49+
implicit val dpoRW: SnakePickle.ReadWriter[Dpo] = SnakePickle.macroRW[Dpo]
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package sttp.openai.requests.finetuning
2+
3+
import sttp.openai.OpenAIExceptions.OpenAIException.DeserializationOpenAIException
4+
import sttp.openai.json.SnakePickle
5+
import ujson.Str
6+
7+
abstract class Type(val value: String)
8+
9+
object Type {
10+
implicit def typeRW(implicit byTypeValue: Map[String, Type]): SnakePickle.ReadWriter[Type] = SnakePickle
11+
.readwriter[ujson.Value]
12+
.bimap[Type](
13+
`type` => SnakePickle.writeJs(`type`.value),
14+
jsonValue =>
15+
SnakePickle.read[ujson.Value](jsonValue) match {
16+
case Str(value) =>
17+
byTypeValue.get(value) match {
18+
case Some(t) => t
19+
case None => throw DeserializationOpenAIException(new Exception(s"Could not deserialize: $value"))
20+
}
21+
case e => throw DeserializationOpenAIException(new Exception(s"Could not deserialize: $e"))
22+
}
23+
)
24+
}

0 commit comments

Comments
 (0)