model.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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: "dall-e",
  52. Object: "model",
  53. Created: 1677649963,
  54. OwnedBy: "openai",
  55. Permission: permission,
  56. Root: "dall-e",
  57. Parent: nil,
  58. },
  59. {
  60. Id: "gpt-3.5-turbo",
  61. Object: "model",
  62. Created: 1677649963,
  63. OwnedBy: "openai",
  64. Permission: permission,
  65. Root: "gpt-3.5-turbo",
  66. Parent: nil,
  67. },
  68. {
  69. Id: "gpt-3.5-turbo-0301",
  70. Object: "model",
  71. Created: 1677649963,
  72. OwnedBy: "openai",
  73. Permission: permission,
  74. Root: "gpt-3.5-turbo-0301",
  75. Parent: nil,
  76. },
  77. {
  78. Id: "gpt-3.5-turbo-0613",
  79. Object: "model",
  80. Created: 1677649963,
  81. OwnedBy: "openai",
  82. Permission: permission,
  83. Root: "gpt-3.5-turbo-0613",
  84. Parent: nil,
  85. },
  86. {
  87. Id: "gpt-3.5-turbo-16k",
  88. Object: "model",
  89. Created: 1677649963,
  90. OwnedBy: "openai",
  91. Permission: permission,
  92. Root: "gpt-3.5-turbo-16k",
  93. Parent: nil,
  94. },
  95. {
  96. Id: "gpt-3.5-turbo-16k-0613",
  97. Object: "model",
  98. Created: 1677649963,
  99. OwnedBy: "openai",
  100. Permission: permission,
  101. Root: "gpt-3.5-turbo-16k-0613",
  102. Parent: nil,
  103. },
  104. {
  105. Id: "gpt-4",
  106. Object: "model",
  107. Created: 1677649963,
  108. OwnedBy: "openai",
  109. Permission: permission,
  110. Root: "gpt-4",
  111. Parent: nil,
  112. },
  113. {
  114. Id: "gpt-4-0314",
  115. Object: "model",
  116. Created: 1677649963,
  117. OwnedBy: "openai",
  118. Permission: permission,
  119. Root: "gpt-4-0314",
  120. Parent: nil,
  121. },
  122. {
  123. Id: "gpt-4-0613",
  124. Object: "model",
  125. Created: 1677649963,
  126. OwnedBy: "openai",
  127. Permission: permission,
  128. Root: "gpt-4-0613",
  129. Parent: nil,
  130. },
  131. {
  132. Id: "gpt-4-32k",
  133. Object: "model",
  134. Created: 1677649963,
  135. OwnedBy: "openai",
  136. Permission: permission,
  137. Root: "gpt-4-32k",
  138. Parent: nil,
  139. },
  140. {
  141. Id: "gpt-4-32k-0314",
  142. Object: "model",
  143. Created: 1677649963,
  144. OwnedBy: "openai",
  145. Permission: permission,
  146. Root: "gpt-4-32k-0314",
  147. Parent: nil,
  148. },
  149. {
  150. Id: "gpt-4-32k-0613",
  151. Object: "model",
  152. Created: 1677649963,
  153. OwnedBy: "openai",
  154. Permission: permission,
  155. Root: "gpt-4-32k-0613",
  156. Parent: nil,
  157. },
  158. {
  159. Id: "text-embedding-ada-002",
  160. Object: "model",
  161. Created: 1677649963,
  162. OwnedBy: "openai",
  163. Permission: permission,
  164. Root: "text-embedding-ada-002",
  165. Parent: nil,
  166. },
  167. {
  168. Id: "text-davinci-003",
  169. Object: "model",
  170. Created: 1677649963,
  171. OwnedBy: "openai",
  172. Permission: permission,
  173. Root: "text-davinci-003",
  174. Parent: nil,
  175. },
  176. {
  177. Id: "text-davinci-002",
  178. Object: "model",
  179. Created: 1677649963,
  180. OwnedBy: "openai",
  181. Permission: permission,
  182. Root: "text-davinci-002",
  183. Parent: nil,
  184. },
  185. {
  186. Id: "text-curie-001",
  187. Object: "model",
  188. Created: 1677649963,
  189. OwnedBy: "openai",
  190. Permission: permission,
  191. Root: "text-curie-001",
  192. Parent: nil,
  193. },
  194. {
  195. Id: "text-babbage-001",
  196. Object: "model",
  197. Created: 1677649963,
  198. OwnedBy: "openai",
  199. Permission: permission,
  200. Root: "text-babbage-001",
  201. Parent: nil,
  202. },
  203. {
  204. Id: "text-ada-001",
  205. Object: "model",
  206. Created: 1677649963,
  207. OwnedBy: "openai",
  208. Permission: permission,
  209. Root: "text-ada-001",
  210. Parent: nil,
  211. },
  212. {
  213. Id: "text-moderation-latest",
  214. Object: "model",
  215. Created: 1677649963,
  216. OwnedBy: "openai",
  217. Permission: permission,
  218. Root: "text-moderation-latest",
  219. Parent: nil,
  220. },
  221. {
  222. Id: "text-moderation-stable",
  223. Object: "model",
  224. Created: 1677649963,
  225. OwnedBy: "openai",
  226. Permission: permission,
  227. Root: "text-moderation-stable",
  228. Parent: nil,
  229. },
  230. {
  231. Id: "text-davinci-edit-001",
  232. Object: "model",
  233. Created: 1677649963,
  234. OwnedBy: "openai",
  235. Permission: permission,
  236. Root: "text-davinci-edit-001",
  237. Parent: nil,
  238. },
  239. {
  240. Id: "code-davinci-edit-001",
  241. Object: "model",
  242. Created: 1677649963,
  243. OwnedBy: "openai",
  244. Permission: permission,
  245. Root: "code-davinci-edit-001",
  246. Parent: nil,
  247. },
  248. {
  249. Id: "claude-instant-1",
  250. Object: "model",
  251. Created: 1677649963,
  252. OwnedBy: "anturopic",
  253. Permission: permission,
  254. Root: "claude-instant-1",
  255. Parent: nil,
  256. },
  257. {
  258. Id: "claude-2",
  259. Object: "model",
  260. Created: 1677649963,
  261. OwnedBy: "anturopic",
  262. Permission: permission,
  263. Root: "claude-2",
  264. Parent: nil,
  265. },
  266. {
  267. Id: "ERNIE-Bot",
  268. Object: "model",
  269. Created: 1677649963,
  270. OwnedBy: "baidu",
  271. Permission: permission,
  272. Root: "ERNIE-Bot",
  273. Parent: nil,
  274. },
  275. {
  276. Id: "ERNIE-Bot-turbo",
  277. Object: "model",
  278. Created: 1677649963,
  279. OwnedBy: "baidu",
  280. Permission: permission,
  281. Root: "ERNIE-Bot-turbo",
  282. Parent: nil,
  283. },
  284. {
  285. Id: "PaLM-2",
  286. Object: "model",
  287. Created: 1677649963,
  288. OwnedBy: "google",
  289. Permission: permission,
  290. Root: "PaLM-2",
  291. Parent: nil,
  292. },
  293. {
  294. Id: "chatglm_pro",
  295. Object: "model",
  296. Created: 1677649963,
  297. OwnedBy: "zhipu",
  298. Permission: permission,
  299. Root: "chatglm_pro",
  300. Parent: nil,
  301. },
  302. {
  303. Id: "chatglm_std",
  304. Object: "model",
  305. Created: 1677649963,
  306. OwnedBy: "zhipu",
  307. Permission: permission,
  308. Root: "chatglm_std",
  309. Parent: nil,
  310. },
  311. {
  312. Id: "chatglm_lite",
  313. Object: "model",
  314. Created: 1677649963,
  315. OwnedBy: "zhipu",
  316. Permission: permission,
  317. Root: "chatglm_lite",
  318. Parent: nil,
  319. },
  320. }
  321. openAIModelsMap = make(map[string]OpenAIModels)
  322. for _, model := range openAIModels {
  323. openAIModelsMap[model.Id] = model
  324. }
  325. }
  326. func ListModels(c *gin.Context) {
  327. c.JSON(200, gin.H{
  328. "object": "list",
  329. "data": openAIModels,
  330. })
  331. }
  332. func RetrieveModel(c *gin.Context) {
  333. modelId := c.Param("model")
  334. if model, ok := openAIModelsMap[modelId]; ok {
  335. c.JSON(200, model)
  336. } else {
  337. openAIError := OpenAIError{
  338. Message: fmt.Sprintf("The model '%s' does not exist", modelId),
  339. Type: "invalid_request_error",
  340. Param: "model",
  341. Code: "model_not_found",
  342. }
  343. c.JSON(200, gin.H{
  344. "error": openAIError,
  345. })
  346. }
  347. }