model.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. permission := OpenAIModelPermission{
  34. Id: "modelperm-LwHkVFn8AcMItP432fKKDIKJ",
  35. Object: "model_permission",
  36. Created: 1626777600,
  37. AllowCreateEngine: true,
  38. AllowSampling: true,
  39. AllowLogprobs: true,
  40. AllowSearchIndices: false,
  41. AllowView: true,
  42. AllowFineTuning: false,
  43. Organization: "*",
  44. Group: nil,
  45. IsBlocking: false,
  46. }
  47. // https://platform.openai.com/docs/models/model-endpoint-compatibility
  48. openAIModels = []OpenAIModels{
  49. {
  50. Id: "gpt-3.5-turbo",
  51. Object: "model",
  52. Created: 1677649963,
  53. OwnedBy: "openai",
  54. Permission: permission,
  55. Root: "gpt-3.5-turbo",
  56. Parent: nil,
  57. },
  58. {
  59. Id: "gpt-3.5-turbo-0301",
  60. Object: "model",
  61. Created: 1677649963,
  62. OwnedBy: "openai",
  63. Permission: permission,
  64. Root: "gpt-3.5-turbo-0301",
  65. Parent: nil,
  66. },
  67. {
  68. Id: "gpt-4",
  69. Object: "model",
  70. Created: 1677649963,
  71. OwnedBy: "openai",
  72. Permission: permission,
  73. Root: "gpt-4",
  74. Parent: nil,
  75. },
  76. {
  77. Id: "gpt-4-0314",
  78. Object: "model",
  79. Created: 1677649963,
  80. OwnedBy: "openai",
  81. Permission: permission,
  82. Root: "gpt-4-0314",
  83. Parent: nil,
  84. },
  85. {
  86. Id: "gpt-4-32k",
  87. Object: "model",
  88. Created: 1677649963,
  89. OwnedBy: "openai",
  90. Permission: permission,
  91. Root: "gpt-4-32k",
  92. Parent: nil,
  93. },
  94. {
  95. Id: "gpt-4-32k-0314",
  96. Object: "model",
  97. Created: 1677649963,
  98. OwnedBy: "openai",
  99. Permission: permission,
  100. Root: "gpt-4-32k-0314",
  101. Parent: nil,
  102. },
  103. {
  104. Id: "gpt-3.5-turbo",
  105. Object: "model",
  106. Created: 1677649963,
  107. OwnedBy: "openai",
  108. Permission: permission,
  109. Root: "gpt-3.5-turbo",
  110. Parent: nil,
  111. },
  112. {
  113. Id: "text-embedding-ada-002",
  114. Object: "model",
  115. Created: 1677649963,
  116. OwnedBy: "openai",
  117. Permission: permission,
  118. Root: "text-embedding-ada-002",
  119. Parent: nil,
  120. },
  121. }
  122. openAIModelsMap = make(map[string]OpenAIModels)
  123. for _, model := range openAIModels {
  124. openAIModelsMap[model.Id] = model
  125. }
  126. }
  127. func ListModels(c *gin.Context) {
  128. c.JSON(200, openAIModels)
  129. }
  130. func RetrieveModel(c *gin.Context) {
  131. modelId := c.Param("model")
  132. if model, ok := openAIModelsMap[modelId]; ok {
  133. c.JSON(200, model)
  134. } else {
  135. openAIError := OpenAIError{
  136. Message: fmt.Sprintf("The model '%s' does not exist", modelId),
  137. Type: "invalid_request_error",
  138. Param: "model",
  139. Code: "model_not_found",
  140. }
  141. c.JSON(200, gin.H{
  142. "error": openAIError,
  143. })
  144. }
  145. }