options.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package gpt
  2. import (
  3. "net/http"
  4. "time"
  5. )
  6. // Option sets gpt-3 Client option values.
  7. type Option interface {
  8. apply(client) error
  9. }
  10. // ClientOption are options that can be passed when creating a new client
  11. type ClientOption func(*client) *client
  12. func (fn ClientOption) apply(cli *client) *client {
  13. return fn(cli)
  14. }
  15. // WithOrg is a client option that allows you to override the organization ID
  16. func WithOrg(id string) ClientOption {
  17. return func(cli *client) *client {
  18. cli.idOrg = id
  19. return cli
  20. }
  21. }
  22. // WithDefaultEngine is a client option that allows you to override the default engine of the client
  23. func WithDefaultEngine(engine string) ClientOption {
  24. return func(cli *client) *client {
  25. cli.defaultEngine = engine
  26. return cli
  27. }
  28. }
  29. // WithUserAgent is a client option that allows you to override the default user agent of the client
  30. func WithUserAgent(userAgent string) ClientOption {
  31. return func(cli *client) *client {
  32. cli.userAgent = userAgent
  33. return cli
  34. }
  35. }
  36. // WithBaseURL is a client option that allows you to override the default base url of the client.
  37. // The default base url is "https://api.openai.com/v1"
  38. func WithBaseURL(baseURL string) ClientOption {
  39. return func(cli *client) *client {
  40. cli.baseURL = baseURL
  41. return cli
  42. }
  43. }
  44. // WithHTTPClient allows you to override the internal http.Client used
  45. func WithHTTPClient(httpClient *http.Client) ClientOption {
  46. return func(cli *client) *client {
  47. cli.httpClient = httpClient
  48. return cli
  49. }
  50. }
  51. // WithTimeout is a client option that allows you to override the default timeout duration of requests
  52. // for the client. The default is 30 seconds. If you are overriding the http client as well, just include
  53. // the timeout there.
  54. func WithTimeout(timeout time.Duration) ClientOption {
  55. return func(cli *client) *client {
  56. cli.httpClient.Timeout = timeout
  57. return cli
  58. }
  59. }