File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
main/scala/sttp/openai/requests/completions/chat
test/scala/sttp/openai/requests/completions/chat Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package sttp.openai.requests.completions.chat
2
2
3
3
import sttp .model .sse .ServerSentEvent
4
4
import sttp .openai .json .SnakePickle
5
+ import sttp .openai .requests .completions .Usage
5
6
6
7
object ChatChunkRequestResponseData {
7
8
@@ -39,7 +40,8 @@ object ChatChunkRequestResponseData {
39
40
created : Int ,
40
41
model : String ,
41
42
`object` : String ,
42
- systemFingerprint : Option [String ] = None
43
+ systemFingerprint : Option [String ] = None ,
44
+ usage : Option [Usage ] = None
43
45
)
44
46
45
47
object ChatChunkResponse {
Original file line number Diff line number Diff line change @@ -81,4 +81,46 @@ class ChatChunkDataSpec extends AnyFlatSpec with Matchers with EitherValues {
81
81
// then
82
82
serializedJson shouldBe jsonRequest
83
83
}
84
+
85
+ " Given chat chunk with usage data" should " properly deserialize usage field" in {
86
+ import ChatChunkRequestResponseData ._
87
+ import sttp .openai .requests .completions .Usage
88
+
89
+ // given
90
+ val jsonWithUsage = """ {
91
+ | "id": "chatcmpl-usage",
92
+ | "object": "chat.completion.chunk",
93
+ | "created": 1681725687,
94
+ | "model": "gpt-4",
95
+ | "system_fingerprint": "fp_123",
96
+ | "choices": [],
97
+ | "usage": {
98
+ | "prompt_tokens": 25,
99
+ | "completion_tokens": 12,
100
+ | "total_tokens": 37
101
+ | }
102
+ |}""" .stripMargin
103
+
104
+ val expectedResponse = ChatChunkResponse (
105
+ id = " chatcmpl-usage" ,
106
+ `object` = " chat.completion.chunk" ,
107
+ created = 1681725687 ,
108
+ model = " gpt-4" ,
109
+ choices = Seq .empty,
110
+ systemFingerprint = Some (" fp_123" ),
111
+ usage = Some (
112
+ Usage (
113
+ promptTokens = 25 ,
114
+ completionTokens = 12 ,
115
+ totalTokens = 37
116
+ )
117
+ )
118
+ )
119
+
120
+ // when
121
+ val givenResponse : Either [Exception , ChatChunkResponse ] = JsonUtils .deserializeJsonSnake[ChatChunkResponse ].apply(jsonWithUsage)
122
+
123
+ // then
124
+ givenResponse.value shouldBe expectedResponse
125
+ }
84
126
}
You can’t perform that action at this time.
0 commit comments