model.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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-3.5-turbo-0613",
  70. Object: "model",
  71. Created: 1677649963,
  72. OwnedBy: "openai",
  73. Permission: permission,
  74. Root: "gpt-3.5-turbo-0613",
  75. Parent: nil,
  76. },
  77. {
  78. Id: "gpt-3.5-turbo-16k",
  79. Object: "model",
  80. Created: 1677649963,
  81. OwnedBy: "openai",
  82. Permission: permission,
  83. Root: "gpt-3.5-turbo-16k",
  84. Parent: nil,
  85. },
  86. {
  87. Id: "gpt-3.5-turbo-16k-0613",
  88. Object: "model",
  89. Created: 1677649963,
  90. OwnedBy: "openai",
  91. Permission: permission,
  92. Root: "gpt-3.5-turbo-16k-0613",
  93. Parent: nil,
  94. },
  95. {
  96. Id: "gpt-4",
  97. Object: "model",
  98. Created: 1677649963,
  99. OwnedBy: "openai",
  100. Permission: permission,
  101. Root: "gpt-4",
  102. Parent: nil,
  103. },
  104. {
  105. Id: "gpt-4-0314",
  106. Object: "model",
  107. Created: 1677649963,
  108. OwnedBy: "openai",
  109. Permission: permission,
  110. Root: "gpt-4-0314",
  111. Parent: nil,
  112. },
  113. {
  114. Id: "gpt-4-0613",
  115. Object: "model",
  116. Created: 1677649963,
  117. OwnedBy: "openai",
  118. Permission: permission,
  119. Root: "gpt-4-0613",
  120. Parent: nil,
  121. },
  122. {
  123. Id: "gpt-4-32k",
  124. Object: "model",
  125. Created: 1677649963,
  126. OwnedBy: "openai",
  127. Permission: permission,
  128. Root: "gpt-4-32k",
  129. Parent: nil,
  130. },
  131. {
  132. Id: "gpt-4-32k-0314",
  133. Object: "model",
  134. Created: 1677649963,
  135. OwnedBy: "openai",
  136. Permission: permission,
  137. Root: "gpt-4-32k-0314",
  138. Parent: nil,
  139. },
  140. {
  141. Id: "gpt-4-32k-0613",
  142. Object: "model",
  143. Created: 1677649963,
  144. OwnedBy: "openai",
  145. Permission: permission,
  146. Root: "gpt-4-32k-0613",
  147. Parent: nil,
  148. },
  149. {
  150. Id: "text-embedding-ada-002",
  151. Object: "model",
  152. Created: 1677649963,
  153. OwnedBy: "openai",
  154. Permission: permission,
  155. Root: "text-embedding-ada-002",
  156. Parent: nil,
  157. },
  158. {
  159. Id: "text-davinci-003",
  160. Object: "model",
  161. Created: 1677649963,
  162. OwnedBy: "openai",
  163. Permission: permission,
  164. Root: "text-davinci-003",
  165. Parent: nil,
  166. },
  167. {
  168. Id: "text-davinci-002",
  169. Object: "model",
  170. Created: 1677649963,
  171. OwnedBy: "openai",
  172. Permission: permission,
  173. Root: "text-davinci-002",
  174. Parent: nil,
  175. },
  176. {
  177. Id: "text-curie-001",
  178. Object: "model",
  179. Created: 1677649963,
  180. OwnedBy: "openai",
  181. Permission: permission,
  182. Root: "text-curie-001",
  183. Parent: nil,
  184. },
  185. {
  186. Id: "text-babbage-001",
  187. Object: "model",
  188. Created: 1677649963,
  189. OwnedBy: "openai",
  190. Permission: permission,
  191. Root: "text-babbage-001",
  192. Parent: nil,
  193. },
  194. {
  195. Id: "text-ada-001",
  196. Object: "model",
  197. Created: 1677649963,
  198. OwnedBy: "openai",
  199. Permission: permission,
  200. Root: "text-ada-001",
  201. Parent: nil,
  202. },
  203. {
  204. Id: "text-moderation-latest",
  205. Object: "model",
  206. Created: 1677649963,
  207. OwnedBy: "openai",
  208. Permission: permission,
  209. Root: "text-moderation-latest",
  210. Parent: nil,
  211. },
  212. {
  213. Id: "text-moderation-stable",
  214. Object: "model",
  215. Created: 1677649963,
  216. OwnedBy: "openai",
  217. Permission: permission,
  218. Root: "text-moderation-stable",
  219. Parent: nil,
  220. },
  221. }
  222. openAIModelsMap = make(map[string]OpenAIModels)
  223. for _, model := range openAIModels {
  224. openAIModelsMap[model.Id] = model
  225. }
  226. }
  227. func ListModels(c *gin.Context) {
  228. c.JSON(200, gin.H{
  229. "object": "list",
  230. "data": openAIModels,
  231. })
  232. }
  233. func RetrieveModel(c *gin.Context) {
  234. modelId := c.Param("model")
  235. if model, ok := openAIModelsMap[modelId]; ok {
  236. c.JSON(200, model)
  237. } else {
  238. openAIError := OpenAIError{
  239. Message: fmt.Sprintf("The model '%s' does not exist", modelId),
  240. Type: "invalid_request_error",
  241. Param: "model",
  242. Code: "model_not_found",
  243. }
  244. c.JSON(200, gin.H{
  245. "error": openAIError,
  246. })
  247. }
  248. }