@@ -25,32 +25,35 @@ func (c *mockDoer) Do(r *http.Request) (*http.Response, error) {
25
25
return c .do (r )
26
26
}
27
27
28
- func TestErrStatusNotOK (t * testing.T ) {
29
- tokenManager := tokenusage .NewManager ()
30
- mockClient := NewClient (& mockDoer {
28
+ var compRequest = types.CompletionRequest {
29
+ Feature : types .CompletionsFeatureChat ,
30
+ Version : types .CompletionsVersionLegacy ,
31
+ ModelConfigInfo : types.ModelConfigInfo {
32
+ Provider : modelconfigSDK.Provider {
33
+ ID : modelconfigSDK .ProviderID ("xxx-provider-id-xxx" ),
34
+ },
35
+ Model : modelconfigSDK.Model {
36
+ ModelRef : modelconfigSDK .ModelRef ("provider::apiversion::test-model" ),
37
+ },
38
+ },
39
+ Parameters : types.CompletionRequestParameters {
40
+ RequestedModel : "xxx-requested-model-xxx" ,
41
+ },
42
+ }
43
+
44
+ func NewMockClient (statusCode int , response string ) types.CompletionsClient {
45
+ return NewClient (& mockDoer {
31
46
func (r * http.Request ) (* http.Response , error ) {
32
47
return & http.Response {
33
- StatusCode : http . StatusTooManyRequests ,
34
- Body : io .NopCloser (bytes .NewReader ([]byte ("oh no, please slow down!" ))),
48
+ StatusCode : statusCode ,
49
+ Body : io .NopCloser (bytes .NewReader ([]byte (response ))),
35
50
}, nil
36
51
},
37
- }, "" , "" , * tokenManager )
52
+ }, "" , "" , * tokenusage .NewManager ())
53
+ }
38
54
39
- compRequest := types.CompletionRequest {
40
- Feature : types .CompletionsFeatureChat ,
41
- Version : types .CompletionsVersionLegacy ,
42
- ModelConfigInfo : types.ModelConfigInfo {
43
- Provider : modelconfigSDK.Provider {
44
- ID : modelconfigSDK .ProviderID ("xxx-provider-id-xxx" ),
45
- },
46
- Model : modelconfigSDK.Model {
47
- ModelRef : modelconfigSDK .ModelRef ("provider::apiversion::test-model" ),
48
- },
49
- },
50
- Parameters : types.CompletionRequestParameters {
51
- RequestedModel : "xxx-requested-model-xxx" ,
52
- },
53
- }
55
+ func TestErrStatusNotOK (t * testing.T ) {
56
+ mockClient := NewMockClient (http .StatusTooManyRequests , "oh no, please slow down!" )
54
57
55
58
t .Run ("Complete" , func (t * testing.T ) {
56
59
logger := log .Scoped ("completions" )
@@ -74,3 +77,36 @@ func TestErrStatusNotOK(t *testing.T) {
74
77
assert .True (t , ok )
75
78
})
76
79
}
80
+
81
+ func TestNonStreamingResponseParsing (t * testing.T ) {
82
+ mockClient := NewMockClient (http .StatusOK , `{
83
+ "id": "chatcmpl-9wEJ9hnLdPcCLrfdZLrRPGOz48Pmo",
84
+ "object": "chat.completion",
85
+ "created": 1723665051,
86
+ "model": "gpt-4o-mini-2024-07-18",
87
+ "choices": [
88
+ {
89
+ "index": 0,
90
+ "message": {
91
+ "role": "assistant",
92
+ "content": "yes",
93
+ "refusal": null
94
+ },
95
+ "logprobs": null,
96
+ "finish_reason": "stop"
97
+ }
98
+ ],
99
+ "usage": {
100
+ "prompt_tokens": 15,
101
+ "completion_tokens": 1,
102
+ "total_tokens": 16
103
+ },
104
+ "system_fingerprint": "fp_48196bc67a"
105
+ }` )
106
+ logger := log .Scoped ("completions" )
107
+ resp , err := mockClient .Complete (context .Background (), logger , compRequest )
108
+ require .NoError (t , err )
109
+ assert .NotNil (t , resp )
110
+ autogold .Expect (& types.CompletionResponse {Completion : "yes" , StopReason : "stop" }).Equal (t , resp )
111
+
112
+ }
0 commit comments