1
1
package openai
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"encoding/json"
6
7
"errors"
8
+ "io"
7
9
"net/http"
8
10
11
+ openai "github.com/meguminnnnnnnnn/go-openai/internal"
12
+
9
13
"github.com/meguminnnnnnnnn/go-openai/jsonschema"
10
14
)
11
15
@@ -482,6 +486,7 @@ type ChatCompletionResponse struct {
482
486
func (c * Client ) CreateChatCompletion (
483
487
ctx context.Context ,
484
488
request ChatCompletionRequest ,
489
+ opts ... ChatCompletionRequestOption ,
485
490
) (response ChatCompletionResponse , err error ) {
486
491
if request .Stream {
487
492
err = ErrChatCompletionStreamNotSupported
@@ -499,11 +504,26 @@ func (c *Client) CreateChatCompletion(
499
504
return
500
505
}
501
506
507
+ ccOpts := & chatCompletionRequestOptions {}
508
+ for _ , opt := range opts {
509
+ opt (ccOpts )
510
+ }
511
+
512
+ body := any (request )
513
+ if ccOpts .RequestBodySetter != nil {
514
+ var newBody io.Reader
515
+ newBody , err = c .getNewRequestBody (request , ccOpts .RequestBodySetter )
516
+ if err != nil {
517
+ return response , err
518
+ }
519
+ body = newBody
520
+ }
521
+
502
522
req , err := c .newRequest (
503
523
ctx ,
504
524
http .MethodPost ,
505
525
c .fullURL (urlSuffix , withModel (request .Model )),
506
- withBody (request ),
526
+ withBody (body ),
507
527
)
508
528
if err != nil {
509
529
return
@@ -512,3 +532,19 @@ func (c *Client) CreateChatCompletion(
512
532
err = c .sendRequest (req , & response )
513
533
return
514
534
}
535
+
536
+ func (c * Client ) getNewRequestBody (request ChatCompletionRequest , setter RequestBodySetter ) (io.Reader , error ) {
537
+ marshaller := openai.JSONMarshaller {}
538
+
539
+ body , err := marshaller .Marshal (request )
540
+ if err != nil {
541
+ return nil , err
542
+ }
543
+
544
+ newBody , err := setter (body )
545
+ if err != nil {
546
+ return nil , err
547
+ }
548
+
549
+ return bytes .NewBuffer (newBody ), nil
550
+ }
0 commit comments