models.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Package gemini provides minimal fallback model metadata for Gemini native endpoints.
  2. // It is used when upstream model listing is unavailable (e.g. OAuth token missing AI Studio scopes).
  3. package gemini
  4. import "strings"
  5. type Model struct {
  6. Name string `json:"name"`
  7. DisplayName string `json:"displayName,omitempty"`
  8. Description string `json:"description,omitempty"`
  9. SupportedGenerationMethods []string `json:"supportedGenerationMethods,omitempty"`
  10. }
  11. type ModelsListResponse struct {
  12. Models []Model `json:"models"`
  13. }
  14. func DefaultModels() []Model {
  15. methods := []string{"generateContent", "streamGenerateContent"}
  16. return []Model{
  17. {Name: "models/gemini-2.0-flash", SupportedGenerationMethods: methods},
  18. {Name: "models/gemini-2.5-flash", SupportedGenerationMethods: methods},
  19. {Name: "models/gemini-2.5-flash-image", SupportedGenerationMethods: methods},
  20. {Name: "models/gemini-2.5-pro", SupportedGenerationMethods: methods},
  21. {Name: "models/gemini-3.5-flash", SupportedGenerationMethods: methods},
  22. {Name: "models/gemini-3-flash-preview", SupportedGenerationMethods: methods},
  23. {Name: "models/gemini-3-pro-preview", SupportedGenerationMethods: methods},
  24. {Name: "models/gemini-3.1-pro-preview", SupportedGenerationMethods: methods},
  25. {Name: "models/gemini-3.1-pro-preview-customtools", SupportedGenerationMethods: methods},
  26. {Name: "models/gemini-3.1-flash-image", SupportedGenerationMethods: methods},
  27. }
  28. }
  29. func HasFallbackModel(model string) bool {
  30. trimmed := strings.TrimSpace(model)
  31. if trimmed == "" {
  32. return false
  33. }
  34. if !strings.HasPrefix(trimmed, "models/") {
  35. trimmed = "models/" + trimmed
  36. }
  37. for _, model := range DefaultModels() {
  38. if model.Name == trimmed {
  39. return true
  40. }
  41. }
  42. return false
  43. }
  44. func FallbackModelsList() ModelsListResponse {
  45. return ModelsListResponse{Models: DefaultModels()}
  46. }
  47. func FallbackModel(model string) Model {
  48. methods := []string{"generateContent", "streamGenerateContent"}
  49. if model == "" {
  50. return Model{Name: "models/unknown", SupportedGenerationMethods: methods}
  51. }
  52. if len(model) >= 7 && model[:7] == "models/" {
  53. return Model{Name: model, SupportedGenerationMethods: methods}
  54. }
  55. return Model{Name: "models/" + model, SupportedGenerationMethods: methods}
  56. }