meta.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package meta
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/labring/aiproxy/core/model"
  6. "github.com/labring/aiproxy/core/relay/mode"
  7. )
  8. type ChannelMeta struct {
  9. Name string
  10. BaseURL string
  11. Key string
  12. ID int
  13. Type model.ChannelType
  14. ModelMapping map[string]string
  15. }
  16. type Meta struct {
  17. values map[string]any
  18. Channel ChannelMeta
  19. ChannelConfigs model.ChannelConfigs
  20. Group model.GroupCache
  21. Token model.TokenCache
  22. ModelConfig model.ModelConfig
  23. Endpoint string
  24. RequestAt time.Time
  25. RetryAt time.Time
  26. RequestID string
  27. OriginModel string
  28. ActualModel string
  29. Mode mode.Mode
  30. RequestTimeout time.Duration
  31. RequestUsage model.Usage
  32. JobID string
  33. GenerationID string
  34. ResponseID string
  35. }
  36. type Option func(meta *Meta)
  37. func WithEndpoint(endpoint string) Option {
  38. return func(meta *Meta) {
  39. meta.Endpoint = endpoint
  40. }
  41. }
  42. func WithRequestID(requestID string) Option {
  43. return func(meta *Meta) {
  44. meta.RequestID = requestID
  45. }
  46. }
  47. func WithRequestAt(requestAt time.Time) Option {
  48. return func(meta *Meta) {
  49. meta.RequestAt = requestAt
  50. }
  51. }
  52. func WithRetryAt(retryAt time.Time) Option {
  53. return func(meta *Meta) {
  54. meta.RetryAt = retryAt
  55. }
  56. }
  57. func WithGroup(group model.GroupCache) Option {
  58. return func(meta *Meta) {
  59. meta.Group = group
  60. }
  61. }
  62. func WithToken(token model.TokenCache) Option {
  63. return func(meta *Meta) {
  64. meta.Token = token
  65. }
  66. }
  67. func WithRequestUsage(requestUsage model.Usage) Option {
  68. return func(meta *Meta) {
  69. meta.RequestUsage = requestUsage
  70. }
  71. }
  72. func WithJobID(jobID string) Option {
  73. return func(meta *Meta) {
  74. meta.JobID = jobID
  75. }
  76. }
  77. func WithGenerationID(generationID string) Option {
  78. return func(meta *Meta) {
  79. meta.GenerationID = generationID
  80. }
  81. }
  82. func WithResponseID(responseID string) Option {
  83. return func(meta *Meta) {
  84. meta.ResponseID = responseID
  85. }
  86. }
  87. func NewMeta(
  88. channel *model.Channel,
  89. mode mode.Mode,
  90. modelName string,
  91. modelConfig model.ModelConfig,
  92. opts ...Option,
  93. ) *Meta {
  94. meta := Meta{
  95. values: make(map[string]any),
  96. Mode: mode,
  97. OriginModel: modelName,
  98. ActualModel: modelName,
  99. ModelConfig: modelConfig,
  100. }
  101. for _, opt := range opts {
  102. opt(&meta)
  103. }
  104. if meta.RequestAt.IsZero() {
  105. meta.RequestAt = time.Now()
  106. }
  107. if channel != nil {
  108. meta.SetChannel(channel)
  109. }
  110. return &meta
  111. }
  112. func (m *Meta) SetChannel(channel *model.Channel) {
  113. m.Channel.Name = channel.Name
  114. m.Channel.BaseURL = channel.BaseURL
  115. m.Channel.Key = channel.Key
  116. m.Channel.ID = channel.ID
  117. m.Channel.Type = channel.Type
  118. m.Channel.ModelMapping = channel.ModelMapping
  119. m.ChannelConfigs = channel.Configs
  120. m.ActualModel, _ = GetMappedModelName(m.OriginModel, channel.ModelMapping)
  121. }
  122. func (m *Meta) CopyChannelFromMeta(meta *Meta) {
  123. m.Channel = meta.Channel
  124. m.ChannelConfigs = meta.ChannelConfigs
  125. m.ActualModel, _ = GetMappedModelName(meta.OriginModel, meta.Channel.ModelMapping)
  126. }
  127. func (m *Meta) ClearValues() {
  128. clear(m.values)
  129. }
  130. func (m *Meta) Set(key string, value any) {
  131. m.values[key] = value
  132. }
  133. func (m *Meta) Get(key string) (any, bool) {
  134. v, ok := m.values[key]
  135. return v, ok
  136. }
  137. func (m *Meta) Delete(key string) {
  138. delete(m.values, key)
  139. }
  140. func (m *Meta) MustGet(key string) any {
  141. v, ok := m.Get(key)
  142. if !ok {
  143. panic(fmt.Sprintf("meta key %s not found", key))
  144. }
  145. return v
  146. }
  147. func (m *Meta) GetString(key string) string {
  148. v, ok := m.Get(key)
  149. if !ok {
  150. return ""
  151. }
  152. s, _ := v.(string)
  153. return s
  154. }
  155. func (m *Meta) GetBool(key string) bool {
  156. v, ok := m.Get(key)
  157. if !ok {
  158. return false
  159. }
  160. b, _ := v.(bool)
  161. return b
  162. }
  163. func (m *Meta) GetInt64(key string) int64 {
  164. v, ok := m.Get(key)
  165. if !ok {
  166. return 0
  167. }
  168. i, _ := v.(int64)
  169. return i
  170. }
  171. func (m *Meta) GetInt(key string) int {
  172. v, ok := m.Get(key)
  173. if !ok {
  174. return 0
  175. }
  176. i, _ := v.(int)
  177. return i
  178. }
  179. // PushToSlice appends an item to a slice stored under the given key
  180. func (m *Meta) PushToSlice(key string, item any) {
  181. var slice []any
  182. if existing, ok := m.Get(key); ok {
  183. if existingSlice, ok := existing.([]any); ok {
  184. slice = existingSlice
  185. }
  186. }
  187. slice = append(slice, item)
  188. m.Set(key, slice)
  189. }
  190. // GetSlice retrieves a slice stored under the given key
  191. func (m *Meta) GetSlice(key string) []any {
  192. if slice, ok := m.Get(key); ok {
  193. if typedSlice, ok := slice.([]any); ok {
  194. return typedSlice
  195. }
  196. }
  197. return nil
  198. }
  199. // ClearSlice removes the slice stored under the given key
  200. func (m *Meta) ClearSlice(key string) {
  201. m.Delete(key)
  202. }
  203. func GetMappedModelName(modelName string, mapping map[string]string) (string, bool) {
  204. if len(modelName) == 0 {
  205. return modelName, false
  206. }
  207. mappedModelName := mapping[modelName]
  208. if mappedModelName != "" {
  209. return mappedModelName, true
  210. }
  211. return modelName, false
  212. }