go-chatgpt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

116 lines
4.3 KiB

  1. package gogpt
  2. import (
  3. "context"
  4. "errors"
  5. "net/http"
  6. )
  7. var (
  8. ErrCompletionUnsupportedModel = errors.New("this model is not supported with this method, please use CreateChatCompletion client method instead") //nolint:lll
  9. )
  10. // GPT3 Defines the models provided by OpenAI to use when generating
  11. // completions from OpenAI.
  12. // GPT3 Models are designed for text-based tasks. For code-specific
  13. // tasks, please refer to the Codex series of models.
  14. const (
  15. GPT432K0314 = "gpt-4-32k-0314"
  16. GPT432K = "gpt-4-32k"
  17. GPT40314 = "gpt-4-0314"
  18. GPT4 = "gpt-4"
  19. GPT3Dot5Turbo0301 = "gpt-3.5-turbo-0301"
  20. GPT3Dot5Turbo = "gpt-3.5-turbo"
  21. GPT3TextDavinci003 = "text-davinci-003"
  22. GPT3TextDavinci002 = "text-davinci-002"
  23. GPT3TextCurie001 = "text-curie-001"
  24. GPT3TextBabbage001 = "text-babbage-001"
  25. GPT3TextAda001 = "text-ada-001"
  26. GPT3TextDavinci001 = "text-davinci-001"
  27. GPT3DavinciInstructBeta = "davinci-instruct-beta"
  28. GPT3Davinci = "davinci"
  29. GPT3CurieInstructBeta = "curie-instruct-beta"
  30. GPT3Curie = "curie"
  31. GPT3Ada = "ada"
  32. GPT3Babbage = "babbage"
  33. )
  34. // Codex Defines the models provided by OpenAI.
  35. // These models are designed for code-specific tasks, and use
  36. // a different tokenizer which optimizes for whitespace.
  37. const (
  38. CodexCodeDavinci002 = "code-davinci-002"
  39. CodexCodeCushman001 = "code-cushman-001"
  40. CodexCodeDavinci001 = "code-davinci-001"
  41. )
  42. // CompletionRequest represents a request structure for completion API.
  43. type CompletionRequest struct {
  44. Model string `json:"model"`
  45. Prompt string `json:"prompt,omitempty"`
  46. Suffix string `json:"suffix,omitempty"`
  47. MaxTokens int `json:"max_tokens,omitempty"`
  48. Temperature float32 `json:"temperature,omitempty"`
  49. TopP float32 `json:"top_p,omitempty"`
  50. N int `json:"n,omitempty"`
  51. Stream bool `json:"stream,omitempty"`
  52. LogProbs int `json:"logprobs,omitempty"`
  53. Echo bool `json:"echo,omitempty"`
  54. Stop []string `json:"stop,omitempty"`
  55. PresencePenalty float32 `json:"presence_penalty,omitempty"`
  56. FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
  57. BestOf int `json:"best_of,omitempty"`
  58. LogitBias map[string]int `json:"logit_bias,omitempty"`
  59. User string `json:"user,omitempty"`
  60. }
  61. // CompletionChoice represents one of possible completions.
  62. type CompletionChoice struct {
  63. Text string `json:"text"`
  64. Index int `json:"index"`
  65. FinishReason string `json:"finish_reason"`
  66. LogProbs LogprobResult `json:"logprobs"`
  67. }
  68. // LogprobResult represents logprob result of Choice.
  69. type LogprobResult struct {
  70. Tokens []string `json:"tokens"`
  71. TokenLogprobs []float32 `json:"token_logprobs"`
  72. TopLogprobs []map[string]float32 `json:"top_logprobs"`
  73. TextOffset []int `json:"text_offset"`
  74. }
  75. // CompletionResponse represents a response structure for completion API.
  76. type CompletionResponse struct {
  77. ID string `json:"id"`
  78. Object string `json:"object"`
  79. Created int64 `json:"created"`
  80. Model string `json:"model"`
  81. Choices []CompletionChoice `json:"choices"`
  82. Usage Usage `json:"usage"`
  83. }
  84. // CreateCompletion — API call to create a completion. This is the main endpoint of the API. Returns new text as well
  85. // as, if requested, the probabilities over each alternative token at each position.
  86. //
  87. // If using a fine-tuned model, simply provide the model's ID in the CompletionRequest object,
  88. // and the server will use the model's parameters to generate the completion.
  89. func (c *Client) CreateCompletion(
  90. ctx context.Context,
  91. request CompletionRequest,
  92. ) (response CompletionResponse, err error) {
  93. if request.Model == GPT3Dot5Turbo0301 || request.Model == GPT3Dot5Turbo {
  94. err = ErrCompletionUnsupportedModel
  95. return
  96. }
  97. urlSuffix := "/completions"
  98. req, err := c.requestBuilder.build(ctx, http.MethodPost, c.fullURL(urlSuffix), request)
  99. if err != nil {
  100. return
  101. }
  102. err = c.sendRequest(req, &response)
  103. return
  104. }