11package openai
22
33import (
4+ "bytes"
45 "context"
56 "encoding/json"
67 "errors"
8+ "io"
79 "net/http"
810
11+ openai "github.com/meguminnnnnnnnn/go-openai/internal"
12+
913 "github.com/meguminnnnnnnnn/go-openai/jsonschema"
1014)
1115
@@ -482,6 +486,7 @@ type ChatCompletionResponse struct {
482486func (c * Client ) CreateChatCompletion (
483487 ctx context.Context ,
484488 request ChatCompletionRequest ,
489+ opts ... ChatCompletionRequestOption ,
485490) (response ChatCompletionResponse , err error ) {
486491 if request .Stream {
487492 err = ErrChatCompletionStreamNotSupported
@@ -499,11 +504,26 @@ func (c *Client) CreateChatCompletion(
499504 return
500505 }
501506
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+
502522 req , err := c .newRequest (
503523 ctx ,
504524 http .MethodPost ,
505525 c .fullURL (urlSuffix , withModel (request .Model )),
506- withBody (request ),
526+ withBody (body ),
507527 )
508528 if err != nil {
509529 return
@@ -512,3 +532,19 @@ func (c *Client) CreateChatCompletion(
512532 err = c .sendRequest (req , & response )
513533 return
514534}
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