model.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. // ConversationID Independent dialogue 进行独立对话
  100. ConversationID string `json:"conversation_id,omitempty"`
  101. }
  102. // EditsRequest is a request for the edits API
  103. type EditsRequest struct {
  104. // Model is ID of the model to use. You can use the List models API to see all of your available models,
  105. // or see our Model overview for descriptions of them.
  106. Model string `json:"model"`
  107. // Input is the input text to use as a starting point for the edit.
  108. Input string `json:"input,omitempty"`
  109. // Instruction is the instruction that tells the model how to edit the prompt.
  110. Instruction string `json:"instruction"`
  111. // N is how many edits to generate for the input and instruction. Defaults to 1
  112. N *int `json:"n,omitempty"`
  113. // Temperature is sampling temperature to use
  114. Temperature *float32 `json:"temperature,omitempty"`
  115. // TopP is alternative to temperature for nucleus sampling
  116. TopP *float32 `json:"top_p,omitempty"`
  117. }
  118. // EmbeddingsRequest is a request for the Embeddings API
  119. type EmbeddingsRequest struct {
  120. // Input text to get embeddings for, encoded as a string or array of tokens. To get embeddings
  121. // for multiple inputs in a single request, pass an array of strings or array of token arrays.
  122. // Each input must not exceed 2048 tokens in length.
  123. Input []string `json:"input"`
  124. // Model is ID of the model to use
  125. Model string `json:"model"`
  126. // User is the request user is an optional parameter meant to be used to trace abusive requests
  127. // back to the originating user. OpenAI states:
  128. // "The [user] IDs should be a string that uniquely identifies each user. We recommend hashing
  129. // their username or email address, in order to avoid sending us any identifying information.
  130. // If you offer a preview of your product to non-logged in users, you can send a session ID
  131. // instead."
  132. User string `json:"user,omitempty"`
  133. }
  134. // LogProbResult represents logprob result of Choice
  135. type LogProbResult struct {
  136. Tokens []string `json:"tokens"`
  137. TokenLogProbs []float32 `json:"token_logprobs"`
  138. TopLogProbs []map[string]float32 `json:"top_logprobs"`
  139. TextOffset []int `json:"text_offset"`
  140. }
  141. // ChatCompletionResponseMessage is a message returned in the response to the Chat Completions API
  142. type ChatCompletionResponseMessage struct {
  143. Role string `json:"role"`
  144. Content string `json:"content"`
  145. }
  146. // ChatCompletionResponseChoice is one of the choices returned in the response to the Chat Completions API
  147. type ChatCompletionResponseChoice struct {
  148. Index int `json:"index"`
  149. FinishReason string `json:"finish_reason"`
  150. Message ChatCompletionResponseMessage `json:"message"`
  151. }
  152. // ChatCompletionStreamResponseChoice is one of the choices returned in the response to the Chat Completions API
  153. type ChatCompletionStreamResponseChoice struct {
  154. Index int `json:"index"`
  155. FinishReason string `json:"finish_reason"`
  156. Delta ChatCompletionResponseMessage `json:"delta"`
  157. }
  158. // ChatCompletionsResponseUsage is the object that returns how many tokens the completion's request used
  159. type ChatCompletionsResponseUsage struct {
  160. PromptTokens int `json:"prompt_tokens"`
  161. CompletionTokens int `json:"completion_tokens"`
  162. TotalTokens int `json:"total_tokens"`
  163. }
  164. // ChatCompletionResponse is the full response from a request to the Chat Completions API
  165. type ChatCompletionResponse struct {
  166. ID string `json:"id"`
  167. Object string `json:"object"`
  168. Created int `json:"created"`
  169. Model string `json:"model"`
  170. Choices []ChatCompletionResponseChoice `json:"choices"`
  171. Usage ChatCompletionsResponseUsage `json:"usage"`
  172. }
  173. type ChatCompletionStreamResponse struct {
  174. ID string `json:"id"`
  175. Object string `json:"object"`
  176. Created int `json:"created"`
  177. Model string `json:"model"`
  178. Choices []ChatCompletionStreamResponseChoice `json:"choices"`
  179. Usage ChatCompletionsResponseUsage `json:"usage"`
  180. }
  181. // CompletionResponseChoice is one of the choices returned in the response to the Completions API
  182. type CompletionResponseChoice struct {
  183. Text string `json:"text"`
  184. Index int `json:"index"`
  185. LogProbs LogProbResult `json:"logprobs"`
  186. FinishReason string `json:"finish_reason"`
  187. }
  188. // CompletionResponse is the full response from a request to the completions API
  189. type CompletionResponse struct {
  190. ID string `json:"id"`
  191. Object string `json:"object"`
  192. Created int `json:"created"`
  193. Model string `json:"model"`
  194. Choices []CompletionResponseChoice `json:"choices"`
  195. Usage CompletionResponseUsage `json:"usage"`
  196. }
  197. // CompletionResponseUsage is the object that returns how many tokens the completion's request used
  198. type CompletionResponseUsage struct {
  199. PromptTokens int `json:"prompt_tokens"`
  200. CompletionTokens int `json:"completion_tokens"`
  201. TotalTokens int `json:"total_tokens"`
  202. }
  203. // EditsResponse is the full response from a request to the edits API
  204. type EditsResponse struct {
  205. Object string `json:"object"`
  206. Created int `json:"created"`
  207. Choices []EditsResponseChoice `json:"choices"`
  208. Usage EditsResponseUsage `json:"usage"`
  209. }
  210. // EmbeddingsResult The inner result of a create embeddings request, containing the embeddings for a single input.
  211. type EmbeddingsResult struct {
  212. // The type of object returned (e.g., "list", "object")
  213. Object string `json:"object"`
  214. // The embedding data for the input
  215. Embedding []float64 `json:"embedding"`
  216. Index int `json:"index"`
  217. }
  218. // EmbeddingsUsage The usage stats for an embeddings response
  219. type EmbeddingsUsage struct {
  220. // The number of tokens used by the prompt
  221. PromptTokens int `json:"prompt_tokens"`
  222. // The total tokens used
  223. TotalTokens int `json:"total_tokens"`
  224. }
  225. // EmbeddingsResponse is the response from a create embeddings request.
  226. // See: https://beta.openai.com/docs/api-reference/embeddings/create
  227. type EmbeddingsResponse struct {
  228. Object string `json:"object"`
  229. Data []EmbeddingsResult `json:"data"`
  230. Usage EmbeddingsUsage `json:"usage"`
  231. }
  232. // EditsResponseChoice is one of the choices returned in the response to the Edits API
  233. type EditsResponseChoice struct {
  234. Text string `json:"text"`
  235. Index int `json:"index"`
  236. }
  237. // EditsResponseUsage is a structure used in the response from a request to the edits API
  238. type EditsResponseUsage struct {
  239. PromptTokens int `json:"prompt_tokens"`
  240. CompletionTokens int `json:"completion_tokens"`
  241. TotalTokens int `json:"total_tokens"`
  242. }
  243. // SearchRequest is a request for the document search API
  244. type SearchRequest struct {
  245. Documents []string `json:"documents"`
  246. Query string `json:"query"`
  247. }
  248. // SearchData is a single search result from the document search API
  249. type SearchData struct {
  250. Document int `json:"document"`
  251. Object string `json:"object"`
  252. Score float64 `json:"score"`
  253. }
  254. // SearchResponse is the full response from a request to the document search API
  255. type SearchResponse struct {
  256. Data []SearchData `json:"data"`
  257. Object string `json:"object"`
  258. }
  259. // ImageRequest represents the request structure for the image API.
  260. type ImageRequest struct {
  261. Prompt string `json:"prompt,omitempty"`
  262. N int `json:"n,omitempty"`
  263. Size string `json:"size,omitempty"`
  264. ResponseFormat string `json:"response_format,omitempty"`
  265. User string `json:"user,omitempty"`
  266. }
  267. // ImageResponse represents a response structure for image API.
  268. type ImageResponse struct {
  269. Created int64 `json:"created,omitempty"`
  270. Data []ImageResponseDataInner `json:"data,omitempty"`
  271. }
  272. // ImageResponseDataInner represents a response data structure for image API.
  273. type ImageResponseDataInner struct {
  274. URL string `json:"url,omitempty"`
  275. B64JSON string `json:"b64_json,omitempty"`
  276. }