model.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. openAIModelsMap = make(map[string]OpenAIModels)
  115. for _, model := range openAIModels {
  116. openAIModelsMap[model.Id] = model
  117. }
  118. }
  119. func ListModels(c *gin.Context) {
  120. c.JSON(200, gin.H{
  121. "object": "list",
  122. "data": openAIModels,
  123. })
  124. }
  125. func RetrieveModel(c *gin.Context) {
  126. modelId := c.Param("model")
  127. if model, ok := openAIModelsMap[modelId]; ok {
  128. c.JSON(200, model)
  129. } else {
  130. openAIError := OpenAIError{
  131. Message: fmt.Sprintf("The model '%s' does not exist", modelId),
  132. Type: "invalid_request_error",
  133. Param: "model",
  134. Code: "model_not_found",
  135. }
  136. c.JSON(200, gin.H{
  137. "error": openAIError,
  138. })
  139. }
  140. }