model.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. )
  6. // https://platform.openai.com/docs/api-reference/models/list
  7. type OpenAIModelPermission struct {
  8. Id string `json:"id"`
  9. Object string `json:"object"`
  10. Created int `json:"created"`
  11. AllowCreateEngine bool `json:"allow_create_engine"`
  12. AllowSampling bool `json:"allow_sampling"`
  13. AllowLogprobs bool `json:"allow_logprobs"`
  14. AllowSearchIndices bool `json:"allow_search_indices"`
  15. AllowView bool `json:"allow_view"`
  16. AllowFineTuning bool `json:"allow_fine_tuning"`
  17. Organization string `json:"organization"`
  18. Group *string `json:"group"`
  19. IsBlocking bool `json:"is_blocking"`
  20. }
  21. type OpenAIModels struct {
  22. Id string `json:"id"`
  23. Object string `json:"object"`
  24. Created int `json:"created"`
  25. OwnedBy string `json:"owned_by"`
  26. Permission []OpenAIModelPermission `json:"permission"`
  27. Root string `json:"root"`
  28. Parent *string `json:"parent"`
  29. }
  30. var openAIModels []OpenAIModels
  31. var openAIModelsMap map[string]OpenAIModels
  32. func init() {
  33. var permission []OpenAIModelPermission
  34. permission = append(permission, OpenAIModelPermission{
  35. Id: "modelperm-LwHkVFn8AcMItP432fKKDIKJ",
  36. Object: "model_permission",
  37. Created: 1626777600,
  38. AllowCreateEngine: true,
  39. AllowSampling: true,
  40. AllowLogprobs: true,
  41. AllowSearchIndices: false,
  42. AllowView: true,
  43. AllowFineTuning: false,
  44. Organization: "*",
  45. Group: nil,
  46. IsBlocking: false,
  47. })
  48. // https://platform.openai.com/docs/models/model-endpoint-compatibility
  49. openAIModels = []OpenAIModels{
  50. {
  51. Id: "gpt-3.5-turbo",
  52. Object: "model",
  53. Created: 1677649963,
  54. OwnedBy: "openai",
  55. Permission: permission,
  56. Root: "gpt-3.5-turbo",
  57. Parent: nil,
  58. },
  59. {
  60. Id: "gpt-3.5-turbo-0301",
  61. Object: "model",
  62. Created: 1677649963,
  63. OwnedBy: "openai",
  64. Permission: permission,
  65. Root: "gpt-3.5-turbo-0301",
  66. Parent: nil,
  67. },
  68. {
  69. Id: "gpt-4",
  70. Object: "model",
  71. Created: 1677649963,
  72. OwnedBy: "openai",
  73. Permission: permission,
  74. Root: "gpt-4",
  75. Parent: nil,
  76. },
  77. {
  78. Id: "gpt-4-0314",
  79. Object: "model",
  80. Created: 1677649963,
  81. OwnedBy: "openai",
  82. Permission: permission,
  83. Root: "gpt-4-0314",
  84. Parent: nil,
  85. },
  86. {
  87. Id: "gpt-4-32k",
  88. Object: "model",
  89. Created: 1677649963,
  90. OwnedBy: "openai",
  91. Permission: permission,
  92. Root: "gpt-4-32k",
  93. Parent: nil,
  94. },
  95. {
  96. Id: "gpt-4-32k-0314",
  97. Object: "model",
  98. Created: 1677649963,
  99. OwnedBy: "openai",
  100. Permission: permission,
  101. Root: "gpt-4-32k-0314",
  102. Parent: nil,
  103. },
  104. {
  105. Id: "text-embedding-ada-002",
  106. Object: "model",
  107. Created: 1677649963,
  108. OwnedBy: "openai",
  109. Permission: permission,
  110. Root: "text-embedding-ada-002",
  111. Parent: nil,
  112. },
  113. {
  114. Id: "text-davinci-003",
  115. Object: "model",
  116. Created: 1677649963,
  117. OwnedBy: "openai",
  118. Permission: permission,
  119. Root: "text-davinci-003",
  120. Parent: nil,
  121. },
  122. {
  123. Id: "text-davinci-002",
  124. Object: "model",
  125. Created: 1677649963,
  126. OwnedBy: "openai",
  127. Permission: permission,
  128. Root: "text-davinci-002",
  129. Parent: nil,
  130. },
  131. {
  132. Id: "text-curie-001",
  133. Object: "model",
  134. Created: 1677649963,
  135. OwnedBy: "openai",
  136. Permission: permission,
  137. Root: "text-curie-001",
  138. Parent: nil,
  139. },
  140. {
  141. Id: "text-babbage-001",
  142. Object: "model",
  143. Created: 1677649963,
  144. OwnedBy: "openai",
  145. Permission: permission,
  146. Root: "text-babbage-001",
  147. Parent: nil,
  148. },
  149. {
  150. Id: "text-ada-001",
  151. Object: "model",
  152. Created: 1677649963,
  153. OwnedBy: "openai",
  154. Permission: permission,
  155. Root: "text-ada-001",
  156. Parent: nil,
  157. },
  158. }
  159. openAIModelsMap = make(map[string]OpenAIModels)
  160. for _, model := range openAIModels {
  161. openAIModelsMap[model.Id] = model
  162. }
  163. }
  164. func ListModels(c *gin.Context) {
  165. c.JSON(200, gin.H{
  166. "object": "list",
  167. "data": openAIModels,
  168. })
  169. }
  170. func RetrieveModel(c *gin.Context) {
  171. modelId := c.Param("model")
  172. if model, ok := openAIModelsMap[modelId]; ok {
  173. c.JSON(200, model)
  174. } else {
  175. openAIError := OpenAIError{
  176. Message: fmt.Sprintf("The model '%s' does not exist", modelId),
  177. Type: "invalid_request_error",
  178. Param: "model",
  179. Code: "model_not_found",
  180. }
  181. c.JSON(200, gin.H{
  182. "error": openAIError,
  183. })
  184. }
  185. }