1
1
package openai
2
2
3
3
import (
4
+ "bufio"
4
5
"context"
5
6
"encoding/json"
6
7
"fmt"
@@ -45,6 +46,42 @@ func NewOrgClient(authToken, org string) *Client {
45
46
return NewClientWithConfig (config )
46
47
}
47
48
49
+ type requestOptions struct {
50
+ body any
51
+ header http.Header
52
+ }
53
+
54
+ type requestOption func (* requestOptions )
55
+
56
+ func withBody (body any ) requestOption {
57
+ return func (args * requestOptions ) {
58
+ args .body = body
59
+ }
60
+ }
61
+
62
+ func withContentType (contentType string ) requestOption {
63
+ return func (args * requestOptions ) {
64
+ args .header .Set ("Content-Type" , contentType )
65
+ }
66
+ }
67
+
68
+ func (c * Client ) newRequest (ctx context.Context , method , url string , setters ... requestOption ) (* http.Request , error ) {
69
+ // Default Options
70
+ args := & requestOptions {
71
+ body : nil ,
72
+ header : make (http.Header ),
73
+ }
74
+ for _ , setter := range setters {
75
+ setter (args )
76
+ }
77
+ req , err := c .requestBuilder .Build (ctx , method , url , args .body , args .header )
78
+ if err != nil {
79
+ return nil , err
80
+ }
81
+ c .setCommonHeaders (req )
82
+ return req , nil
83
+ }
84
+
48
85
func (c * Client ) sendRequest (req * http.Request , v any ) error {
49
86
req .Header .Set ("Accept" , "application/json; charset=utf-8" )
50
87
@@ -55,8 +92,6 @@ func (c *Client) sendRequest(req *http.Request, v any) error {
55
92
req .Header .Set ("Content-Type" , "application/json; charset=utf-8" )
56
93
}
57
94
58
- c .setCommonHeaders (req )
59
-
60
95
res , err := c .config .HTTPClient .Do (req )
61
96
if err != nil {
62
97
return err
@@ -71,6 +106,41 @@ func (c *Client) sendRequest(req *http.Request, v any) error {
71
106
return decodeResponse (res .Body , v )
72
107
}
73
108
109
+ func (c * Client ) sendRequestRaw (req * http.Request ) (body io.ReadCloser , err error ) {
110
+ resp , err := c .config .HTTPClient .Do (req )
111
+ if err != nil {
112
+ return
113
+ }
114
+
115
+ if isFailureStatusCode (resp ) {
116
+ err = c .handleErrorResp (resp )
117
+ return
118
+ }
119
+ return resp .Body , nil
120
+ }
121
+
122
+ func sendRequestStream [T streamable ](client * Client , req * http.Request ) (* streamReader [T ], error ) {
123
+ req .Header .Set ("Content-Type" , "application/json" )
124
+ req .Header .Set ("Accept" , "text/event-stream" )
125
+ req .Header .Set ("Cache-Control" , "no-cache" )
126
+ req .Header .Set ("Connection" , "keep-alive" )
127
+
128
+ resp , err := client .config .HTTPClient .Do (req ) //nolint:bodyclose // body is closed in stream.Close()
129
+ if err != nil {
130
+ return new (streamReader [T ]), err
131
+ }
132
+ if isFailureStatusCode (resp ) {
133
+ return new (streamReader [T ]), client .handleErrorResp (resp )
134
+ }
135
+ return & streamReader [T ]{
136
+ emptyMessagesLimit : client .config .EmptyMessagesLimit ,
137
+ reader : bufio .NewReader (resp .Body ),
138
+ response : resp ,
139
+ errAccumulator : utils .NewErrorAccumulator (),
140
+ unmarshaler : & utils.JSONUnmarshaler {},
141
+ }, nil
142
+ }
143
+
74
144
func (c * Client ) setCommonHeaders (req * http.Request ) {
75
145
// https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference#authentication
76
146
// Azure API Key authentication
@@ -138,26 +208,6 @@ func (c *Client) fullURL(suffix string, args ...any) string {
138
208
return fmt .Sprintf ("%s%s" , c .config .BaseURL , suffix )
139
209
}
140
210
141
- func (c * Client ) newStreamRequest (
142
- ctx context.Context ,
143
- method string ,
144
- urlSuffix string ,
145
- body any ,
146
- model string ) (* http.Request , error ) {
147
- req , err := c .requestBuilder .Build (ctx , method , c .fullURL (urlSuffix , model ), body )
148
- if err != nil {
149
- return nil , err
150
- }
151
-
152
- req .Header .Set ("Content-Type" , "application/json" )
153
- req .Header .Set ("Accept" , "text/event-stream" )
154
- req .Header .Set ("Cache-Control" , "no-cache" )
155
- req .Header .Set ("Connection" , "keep-alive" )
156
-
157
- c .setCommonHeaders (req )
158
- return req , nil
159
- }
160
-
161
211
func (c * Client ) handleErrorResp (resp * http.Response ) error {
162
212
var errRes ErrorResponse
163
213
err := json .NewDecoder (resp .Body ).Decode (& errRes )
0 commit comments