model.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package gpt
  2. import "fmt"
  3. // APIError represents an error that occurred on an API
  4. type APIError struct {
  5. StatusCode int `json:"status_code"`
  6. Message string `json:"message"`
  7. Type string `json:"type"`
  8. }
  9. // Error returns a string representation of the error
  10. func (e APIError) Error() string {
  11. return fmt.Sprintf("[%d:%s] %s", e.StatusCode, e.Type, e.Message)
  12. }
  13. // APIErrorResponse is the full error response that has been returned by an API.
  14. type APIErrorResponse struct {
  15. Error APIError `json:"error"`
  16. }
  17. // EngineObject contained in an engine repose
  18. type EngineObject struct {
  19. ID string `json:"id"`
  20. Object string `json:"object"`
  21. Owner string `json:"owner"`
  22. Ready bool `json:"ready"`
  23. }
  24. // EnginesResponse is returned from the Engines API
  25. type EnginesResponse struct {
  26. Data []EngineObject `json:"data"`
  27. Object string `json:"object"`
  28. }
  29. // ChatCompletionRequestMessage is a message to use as the context for the chat completion API
  30. type ChatCompletionRequestMessage struct {
  31. // Role is the role is the role of the message. Can be "system", "user", or "assistant"
  32. Role string `json:"role"`
  33. // Content is the content of the message
  34. Content string `json:"content"`
  35. }
  36. // ChatCompletionRequest is a request for the chat completion API
  37. type ChatCompletionRequest struct {
  38. // Model is the name of the model to use. If not specified, will default to gpt-3.5-turbo.
  39. Model string `json:"model"`
  40. // Messages is a list of messages to use as the context for the chat completion.
  41. Messages []ChatCompletionRequestMessage `json:"messages"`
  42. // Temperature is sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random,
  43. // while lower values like 0.2 will make it more focused and deterministic
  44. Temperature float32 `json:"temperature,omitempty"`
  45. // TopP is an alternative to sampling with temperature, called nucleus sampling, where the model considers the results of
  46. // the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
  47. TopP float32 `json:"top_p,omitempty"`
  48. // N is number of responses to generate
  49. N int `json:"n,omitempty"`
  50. // Stream is whether to stream responses back as they are generated
  51. Stream bool `json:"stream,omitempty"`
  52. // Stop is up to 4 sequences where the API will stop generating further tokens.
  53. Stop []string `json:"stop,omitempty"`
  54. // MaxTokens is the maximum number of tokens to r eturn.
  55. MaxTokens int `json:"max_tokens,omitempty"`
  56. // PresencePenalty (-2, 2) penalize tokens that haven't appeared yet in the history.
  57. PresencePenalty float32 `json:"presence_penalty,omitempty"`
  58. // FrequencyPenalty (-2, 2) penalize tokens that appear too frequently in the history.
  59. FrequencyPenalty float32 `json:"frequency_penalty,omitempty"`
  60. // LogitBias modify the probability of specific tokens appearing in the completion.
  61. LogitBias map[string]float32 `json:"logit_bias,omitempty"`
  62. // User can be used to identify an end-user
  63. User string `json:"user,omitempty"`
  64. }
  65. // CompletionRequest is a request for the completions API
  66. type CompletionRequest struct {
  67. Model string `json:"model"`
  68. // Prompt sets a list of string prompts to use.
  69. Prompt []string `json:"prompt,omitempty"`
  70. // Suffix comes after a completion of inserted text.
  71. Suffix string `json:"suffix,omitempty"`
  72. // MaxTokens sets how many tokens to complete up to. Max of 512
  73. MaxTokens int `json:"max_tokens,omitempty"`
  74. // Temperature sets sampling temperature to use
  75. Temperature float32 `json:"temperature,omitempty"`
  76. // TopP sets alternative to temperature for nucleus sampling
  77. TopP *float32 `json:"top_p,omitempty"`
  78. // N sets how many choice to create for each prompt
  79. N *int `json:"n"`
  80. // Stream sets whether to stream back results or not. Don't set this value in the request yourself
  81. // as it will be overridden depending on if you use CompletionStream or Completion methods.
  82. Stream bool `json:"stream,omitempty"`
  83. // LogProbs sets include the probabilities of most likely tokens
  84. LogProbs *int `json:"logprobs"`
  85. // Echo sets back the prompt in addition to the completion
  86. Echo bool `json:"echo"`
  87. // Stop sets up to 4 sequences where the API will stop generating tokens. Response will not contain the stop sequence.
  88. Stop []string `json:"stop,omitempty"`
  89. // PresencePenalty sets number between 0 and 1 that penalizes tokens that have already appeared in the text so far.
  90. PresencePenalty float32 `json:"presence_penalty"`
  91. // FrequencyPenalty number between 0 and 1 that penalizes tokens on existing frequency in the text so far.
  92. FrequencyPenalty float32 `json:"frequency_penalty"`
  93. // BestOf sets how many of the n best completions to return. Defaults to 1.
  94. BestOf int `json:"best_of,omitempty"`
  95. // LogitBias sets modify the probability of specific tokens appearing in the completion.
  96. LogitBias map[string]float32 `json:"logit_bias,omitempty"`
  97. // User sets an end-user identifier. Can be used to associate completions generated by a specific user.
  98. User string `json:"user,omitempty"`
  99. }
  100. // EditsRequest is a request for the edits API
  101. type EditsRequest struct {
  102. // Model is ID of the model to use. You can use the List models API to see all of your available models,
  103. // or see our Model overview for descriptions of them.
  104. Model string `json:"model"`
  105. // Input is the input text to use as a starting point for the edit.
  106. Input string `json:"input,omitempty"`
  107. // Instruction is the instruction that tells the model how to edit the prompt.
  108. Instruction string `json:"instruction"`
  109. // N is how many edits to generate for the input and instruction. Defaults to 1
  110. N *int `json:"n,omitempty"`
  111. // Temperature is sampling temperature to use
  112. Temperature *float32 `json:"temperature,omitempty"`
  113. // TopP is alternative to temperature for nucleus sampling
  114. TopP *float32 `json:"top_p,omitempty"`
  115. }
  116. // EmbeddingsRequest is a request for the Embeddings API
  117. type EmbeddingsRequest struct {
  118. // Input text to get embeddings for, encoded as a string or array of tokens. To get embeddings
  119. // for multiple inputs in a single request, pass an array of strings or array of token arrays.
  120. // Each input must not exceed 2048 tokens in length.
  121. Input []string `json:"input"`
  122. // Model is ID of the model to use
  123. Model string `json:"model"`
  124. // User is the request user is an optional parameter meant to be used to trace abusive requests
  125. // back to the originating user. OpenAI states:
  126. // "The [user] IDs should be a string that uniquely identifies each user. We recommend hashing
  127. // their username or email address, in order to avoid sending us any identifying information.
  128. // If you offer a preview of your product to non-logged in users, you can send a session ID
  129. // instead."
  130. User string `json:"user,omitempty"`
  131. }
  132. // LogProbResult represents logprob result of Choice
  133. type LogProbResult struct {
  134. Tokens []string `json:"tokens"`
  135. TokenLogProbs []float32 `json:"token_logprobs"`
  136. TopLogProbs []map[string]float32 `json:"top_logprobs"`
  137. TextOffset []int `json:"text_offset"`
  138. }
  139. // ChatCompletionResponseMessage is a message returned in the response to the Chat Completions API
  140. type ChatCompletionResponseMessage struct {
  141. Role string `json:"role"`
  142. Content string `json:"content"`
  143. }
  144. // ChatCompletionResponseChoice is one of the choices returned in the response to the Chat Completions API
  145. type ChatCompletionResponseChoice struct {
  146. Index int `json:"index"`
  147. FinishReason string `json:"finish_reason"`
  148. Message ChatCompletionResponseMessage `json:"message"`
  149. }
  150. // ChatCompletionStreamResponseChoice is one of the choices returned in the response to the Chat Completions API
  151. type ChatCompletionStreamResponseChoice struct {
  152. Index int `json:"index"`
  153. FinishReason string `json:"finish_reason"`
  154. Delta ChatCompletionResponseMessage `json:"delta"`
  155. }
  156. // ChatCompletionsResponseUsage is the object that returns how many tokens the completion's request used
  157. type ChatCompletionsResponseUsage struct {
  158. PromptTokens int `json:"prompt_tokens"`
  159. CompletionTokens int `json:"completion_tokens"`
  160. TotalTokens int `json:"total_tokens"`
  161. }
  162. // ChatCompletionResponse is the full response from a request to the Chat Completions API
  163. type ChatCompletionResponse struct {
  164. ID string `json:"id"`
  165. Object string `json:"object"`
  166. Created int `json:"created"`
  167. Model string `json:"model"`
  168. Choices []ChatCompletionResponseChoice `json:"choices"`
  169. Usage ChatCompletionsResponseUsage `json:"usage"`
  170. }
  171. type ChatCompletionStreamResponse struct {
  172. ID string `json:"id"`
  173. Object string `json:"object"`
  174. Created int `json:"created"`
  175. Model string `json:"model"`
  176. Choices []ChatCompletionStreamResponseChoice `json:"choices"`
  177. Usage ChatCompletionsResponseUsage `json:"usage"`
  178. }
  179. // CompletionResponseChoice is one of the choices returned in the response to the Completions API
  180. type CompletionResponseChoice struct {
  181. Text string `json:"text"`
  182. Index int `json:"index"`
  183. LogProbs LogProbResult `json:"logprobs"`
  184. FinishReason string `json:"finish_reason"`
  185. }
  186. // CompletionResponse is the full response from a request to the completions API
  187. type CompletionResponse struct {
  188. ID string `json:"id"`
  189. Object string `json:"object"`
  190. Created int `json:"created"`
  191. Model string `json:"model"`
  192. Choices []CompletionResponseChoice `json:"choices"`
  193. Usage CompletionResponseUsage `json:"usage"`
  194. }
  195. // CompletionResponseUsage is the object that returns how many tokens the completion's request used
  196. type CompletionResponseUsage struct {
  197. PromptTokens int `json:"prompt_tokens"`
  198. CompletionTokens int `json:"completion_tokens"`
  199. TotalTokens int `json:"total_tokens"`
  200. }
  201. // EditsResponse is the full response from a request to the edits API
  202. type EditsResponse struct {
  203. Object string `json:"object"`
  204. Created int `json:"created"`
  205. Choices []EditsResponseChoice `json:"choices"`
  206. Usage EditsResponseUsage `json:"usage"`
  207. }
  208. // EmbeddingsResult The inner result of a create embeddings request, containing the embeddings for a single input.
  209. type EmbeddingsResult struct {
  210. // The type of object returned (e.g., "list", "object")
  211. Object string `json:"object"`
  212. // The embedding data for the input
  213. Embedding []float64 `json:"embedding"`
  214. Index int `json:"index"`
  215. }
  216. // EmbeddingsUsage The usage stats for an embeddings response
  217. type EmbeddingsUsage struct {
  218. // The number of tokens used by the prompt
  219. PromptTokens int `json:"prompt_tokens"`
  220. // The total tokens used
  221. TotalTokens int `json:"total_tokens"`
  222. }
  223. // EmbeddingsResponse is the response from a create embeddings request.
  224. // See: https://beta.openai.com/docs/api-reference/embeddings/create
  225. type EmbeddingsResponse struct {
  226. Object string `json:"object"`
  227. Data []EmbeddingsResult `json:"data"`
  228. Usage EmbeddingsUsage `json:"usage"`
  229. }
  230. // EditsResponseChoice is one of the choices returned in the response to the Edits API
  231. type EditsResponseChoice struct {
  232. Text string `json:"text"`
  233. Index int `json:"index"`
  234. }
  235. // EditsResponseUsage is a structure used in the response from a request to the edits API
  236. type EditsResponseUsage struct {
  237. PromptTokens int `json:"prompt_tokens"`
  238. CompletionTokens int `json:"completion_tokens"`
  239. TotalTokens int `json:"total_tokens"`
  240. }
  241. // SearchRequest is a request for the document search API
  242. type SearchRequest struct {
  243. Documents []string `json:"documents"`
  244. Query string `json:"query"`
  245. }
  246. // SearchData is a single search result from the document search API
  247. type SearchData struct {
  248. Document int `json:"document"`
  249. Object string `json:"object"`
  250. Score float64 `json:"score"`
  251. }
  252. // SearchResponse is the full response from a request to the document search API
  253. type SearchResponse struct {
  254. Data []SearchData `json:"data"`
  255. Object string `json:"object"`
  256. }
  257. // ImageRequest represents the request structure for the image API.
  258. type ImageRequest struct {
  259. Prompt string `json:"prompt,omitempty"`
  260. N int `json:"n,omitempty"`
  261. Size string `json:"size,omitempty"`
  262. ResponseFormat string `json:"response_format,omitempty"`
  263. User string `json:"user,omitempty"`
  264. }
  265. // ImageResponse represents a response structure for image API.
  266. type ImageResponse struct {
  267. Created int64 `json:"created,omitempty"`
  268. Data []ImageResponseDataInner `json:"data,omitempty"`
  269. }
  270. // ImageResponseDataInner represents a response data structure for image API.
  271. type ImageResponseDataInner struct {
  272. URL string `json:"url,omitempty"`
  273. B64JSON string `json:"b64_json,omitempty"`
  274. }