Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

578 строки
15 KiB

  1. package dto
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "github.com/QuantumNous/new-api/common"
  7. "github.com/QuantumNous/new-api/types"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type ClaudeMetadata struct {
  11. UserId string `json:"user_id"`
  12. }
  13. type ClaudeMediaMessage struct {
  14. Type string `json:"type,omitempty"`
  15. Text *string `json:"text,omitempty"`
  16. Model string `json:"model,omitempty"`
  17. Source *ClaudeMessageSource `json:"source,omitempty"`
  18. Usage *ClaudeUsage `json:"usage,omitempty"`
  19. StopReason *string `json:"stop_reason,omitempty"`
  20. PartialJson *string `json:"partial_json,omitempty"`
  21. Role string `json:"role,omitempty"`
  22. Thinking *string `json:"thinking,omitempty"`
  23. Signature string `json:"signature,omitempty"`
  24. Delta string `json:"delta,omitempty"`
  25. CacheControl json.RawMessage `json:"cache_control,omitempty"`
  26. // tool_calls
  27. Id string `json:"id,omitempty"`
  28. Name string `json:"name,omitempty"`
  29. Input any `json:"input,omitempty"`
  30. Content any `json:"content,omitempty"`
  31. ToolUseId string `json:"tool_use_id,omitempty"`
  32. }
  33. func (c *ClaudeMediaMessage) SetText(s string) {
  34. c.Text = &s
  35. }
  36. func (c *ClaudeMediaMessage) GetText() string {
  37. if c.Text == nil {
  38. return ""
  39. }
  40. return *c.Text
  41. }
  42. func (c *ClaudeMediaMessage) IsStringContent() bool {
  43. if c.Content == nil {
  44. return false
  45. }
  46. _, ok := c.Content.(string)
  47. if ok {
  48. return true
  49. }
  50. return false
  51. }
  52. func (c *ClaudeMediaMessage) GetStringContent() string {
  53. if c.Content == nil {
  54. return ""
  55. }
  56. switch c.Content.(type) {
  57. case string:
  58. return c.Content.(string)
  59. case []any:
  60. var contentStr string
  61. for _, contentItem := range c.Content.([]any) {
  62. contentMap, ok := contentItem.(map[string]any)
  63. if !ok {
  64. continue
  65. }
  66. if contentMap["type"] == ContentTypeText {
  67. if subStr, ok := contentMap["text"].(string); ok {
  68. contentStr += subStr
  69. }
  70. }
  71. }
  72. return contentStr
  73. }
  74. return ""
  75. }
  76. func (c *ClaudeMediaMessage) GetJsonRowString() string {
  77. jsonContent, _ := common.Marshal(c)
  78. return string(jsonContent)
  79. }
  80. func (c *ClaudeMediaMessage) SetContent(content any) {
  81. c.Content = content
  82. }
  83. func (c *ClaudeMediaMessage) ParseMediaContent() []ClaudeMediaMessage {
  84. mediaContent, _ := common.Any2Type[[]ClaudeMediaMessage](c.Content)
  85. return mediaContent
  86. }
  87. type ClaudeMessageSource struct {
  88. Type string `json:"type"`
  89. MediaType string `json:"media_type,omitempty"`
  90. Data any `json:"data,omitempty"`
  91. Url string `json:"url,omitempty"`
  92. }
  93. type ClaudeMessage struct {
  94. Role string `json:"role"`
  95. Content any `json:"content"`
  96. }
  97. func (c *ClaudeMessage) IsStringContent() bool {
  98. if c.Content == nil {
  99. return false
  100. }
  101. _, ok := c.Content.(string)
  102. return ok
  103. }
  104. func (c *ClaudeMessage) GetStringContent() string {
  105. if c.Content == nil {
  106. return ""
  107. }
  108. switch c.Content.(type) {
  109. case string:
  110. return c.Content.(string)
  111. case []any:
  112. var contentStr string
  113. for _, contentItem := range c.Content.([]any) {
  114. contentMap, ok := contentItem.(map[string]any)
  115. if !ok {
  116. continue
  117. }
  118. if contentMap["type"] == ContentTypeText {
  119. if subStr, ok := contentMap["text"].(string); ok {
  120. contentStr += subStr
  121. }
  122. }
  123. }
  124. return contentStr
  125. }
  126. return ""
  127. }
  128. func (c *ClaudeMessage) SetStringContent(content string) {
  129. c.Content = content
  130. }
  131. func (c *ClaudeMessage) SetContent(content any) {
  132. c.Content = content
  133. }
  134. func (c *ClaudeMessage) ParseContent() ([]ClaudeMediaMessage, error) {
  135. return common.Any2Type[[]ClaudeMediaMessage](c.Content)
  136. }
  137. type Tool struct {
  138. Name string `json:"name"`
  139. Description string `json:"description,omitempty"`
  140. InputSchema map[string]interface{} `json:"input_schema"`
  141. }
  142. type InputSchema struct {
  143. Type string `json:"type"`
  144. Properties any `json:"properties,omitempty"`
  145. Required any `json:"required,omitempty"`
  146. }
  147. type ClaudeWebSearchTool struct {
  148. Type string `json:"type"`
  149. Name string `json:"name"`
  150. MaxUses int `json:"max_uses,omitempty"`
  151. UserLocation *ClaudeWebSearchUserLocation `json:"user_location,omitempty"`
  152. }
  153. type ClaudeWebSearchUserLocation struct {
  154. Type string `json:"type"`
  155. Timezone string `json:"timezone,omitempty"`
  156. Country string `json:"country,omitempty"`
  157. Region string `json:"region,omitempty"`
  158. City string `json:"city,omitempty"`
  159. }
  160. type ClaudeToolChoice struct {
  161. Type string `json:"type"`
  162. Name string `json:"name,omitempty"`
  163. DisableParallelToolUse bool `json:"disable_parallel_tool_use,omitempty"`
  164. }
  165. type ClaudeRequest struct {
  166. Model string `json:"model"`
  167. Prompt string `json:"prompt,omitempty"`
  168. System any `json:"system,omitempty"`
  169. Messages []ClaudeMessage `json:"messages,omitempty"`
  170. // InferenceGeo controls Claude data residency region.
  171. // This field is filtered by default and can be enabled via channel setting allow_inference_geo.
  172. InferenceGeo string `json:"inference_geo,omitempty"`
  173. MaxTokens uint `json:"max_tokens,omitempty"`
  174. MaxTokensToSample uint `json:"max_tokens_to_sample,omitempty"`
  175. StopSequences []string `json:"stop_sequences,omitempty"`
  176. Temperature *float64 `json:"temperature,omitempty"`
  177. TopP float64 `json:"top_p,omitempty"`
  178. TopK int `json:"top_k,omitempty"`
  179. Stream bool `json:"stream,omitempty"`
  180. Tools any `json:"tools,omitempty"`
  181. ContextManagement json.RawMessage `json:"context_management,omitempty"`
  182. OutputConfig json.RawMessage `json:"output_config,omitempty"`
  183. OutputFormat json.RawMessage `json:"output_format,omitempty"`
  184. Container json.RawMessage `json:"container,omitempty"`
  185. ToolChoice any `json:"tool_choice,omitempty"`
  186. Thinking *Thinking `json:"thinking,omitempty"`
  187. McpServers json.RawMessage `json:"mcp_servers,omitempty"`
  188. Metadata json.RawMessage `json:"metadata,omitempty"`
  189. // ServiceTier specifies upstream service level and may affect billing.
  190. // This field is filtered by default and can be enabled via channel setting allow_service_tier.
  191. ServiceTier string `json:"service_tier,omitempty"`
  192. CacheControl json.RawMessage `json:"cache_control,omitempty"`
  193. }
  194. // createClaudeFileSource 根据数据内容创建正确类型的 FileSource
  195. func createClaudeFileSource(data string) *types.FileSource {
  196. if strings.HasPrefix(data, "http://") || strings.HasPrefix(data, "https://") {
  197. return types.NewURLFileSource(data)
  198. }
  199. return types.NewBase64FileSource(data, "")
  200. }
  201. func (c *ClaudeRequest) GetTokenCountMeta() *types.TokenCountMeta {
  202. var tokenCountMeta = types.TokenCountMeta{
  203. TokenType: types.TokenTypeTokenizer,
  204. MaxTokens: int(c.MaxTokens),
  205. }
  206. var texts = make([]string, 0)
  207. var fileMeta = make([]*types.FileMeta, 0)
  208. // system
  209. if c.System != nil {
  210. if c.IsStringSystem() {
  211. sys := c.GetStringSystem()
  212. if sys != "" {
  213. texts = append(texts, sys)
  214. }
  215. } else {
  216. systemMedia := c.ParseSystem()
  217. for _, media := range systemMedia {
  218. switch media.Type {
  219. case "text":
  220. texts = append(texts, media.GetText())
  221. case "image":
  222. if media.Source != nil {
  223. data := media.Source.Url
  224. if data == "" {
  225. data = common.Interface2String(media.Source.Data)
  226. }
  227. if data != "" {
  228. fileMeta = append(fileMeta, &types.FileMeta{
  229. FileType: types.FileTypeImage,
  230. Source: createClaudeFileSource(data),
  231. })
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. // messages
  239. for _, message := range c.Messages {
  240. tokenCountMeta.MessagesCount++
  241. texts = append(texts, message.Role)
  242. if message.IsStringContent() {
  243. content := message.GetStringContent()
  244. if content != "" {
  245. texts = append(texts, content)
  246. }
  247. continue
  248. }
  249. content, _ := message.ParseContent()
  250. for _, media := range content {
  251. switch media.Type {
  252. case "text":
  253. texts = append(texts, media.GetText())
  254. case "image":
  255. if media.Source != nil {
  256. data := media.Source.Url
  257. if data == "" {
  258. data = common.Interface2String(media.Source.Data)
  259. }
  260. if data != "" {
  261. fileMeta = append(fileMeta, &types.FileMeta{
  262. FileType: types.FileTypeImage,
  263. Source: createClaudeFileSource(data),
  264. })
  265. }
  266. }
  267. case "tool_use":
  268. if media.Name != "" {
  269. texts = append(texts, media.Name)
  270. }
  271. if media.Input != nil {
  272. b, _ := common.Marshal(media.Input)
  273. texts = append(texts, string(b))
  274. }
  275. case "tool_result":
  276. if media.Content != nil {
  277. b, _ := common.Marshal(media.Content)
  278. texts = append(texts, string(b))
  279. }
  280. }
  281. }
  282. }
  283. // tools
  284. if c.Tools != nil {
  285. tools := c.GetTools()
  286. normalTools, webSearchTools := ProcessTools(tools)
  287. if normalTools != nil {
  288. for _, t := range normalTools {
  289. tokenCountMeta.ToolsCount++
  290. if t.Name != "" {
  291. texts = append(texts, t.Name)
  292. }
  293. if t.Description != "" {
  294. texts = append(texts, t.Description)
  295. }
  296. if t.InputSchema != nil {
  297. b, _ := common.Marshal(t.InputSchema)
  298. texts = append(texts, string(b))
  299. }
  300. }
  301. }
  302. if webSearchTools != nil {
  303. for _, t := range webSearchTools {
  304. tokenCountMeta.ToolsCount++
  305. if t.Name != "" {
  306. texts = append(texts, t.Name)
  307. }
  308. if t.UserLocation != nil {
  309. b, _ := common.Marshal(t.UserLocation)
  310. texts = append(texts, string(b))
  311. }
  312. }
  313. }
  314. }
  315. tokenCountMeta.CombineText = strings.Join(texts, "\n")
  316. tokenCountMeta.Files = fileMeta
  317. return &tokenCountMeta
  318. }
  319. func (c *ClaudeRequest) IsStream(ctx *gin.Context) bool {
  320. return c.Stream
  321. }
  322. func (c *ClaudeRequest) SetModelName(modelName string) {
  323. if modelName != "" {
  324. c.Model = modelName
  325. }
  326. }
  327. func (c *ClaudeRequest) SearchToolNameByToolCallId(toolCallId string) string {
  328. for _, message := range c.Messages {
  329. content, _ := message.ParseContent()
  330. for _, mediaMessage := range content {
  331. if mediaMessage.Id == toolCallId {
  332. return mediaMessage.Name
  333. }
  334. }
  335. }
  336. return ""
  337. }
  338. // AddTool 添加工具到请求中
  339. func (c *ClaudeRequest) AddTool(tool any) {
  340. if c.Tools == nil {
  341. c.Tools = make([]any, 0)
  342. }
  343. switch tools := c.Tools.(type) {
  344. case []any:
  345. c.Tools = append(tools, tool)
  346. default:
  347. // 如果Tools不是[]any类型,重新初始化为[]any
  348. c.Tools = []any{tool}
  349. }
  350. }
  351. // GetTools 获取工具列表
  352. func (c *ClaudeRequest) GetTools() []any {
  353. if c.Tools == nil {
  354. return nil
  355. }
  356. switch tools := c.Tools.(type) {
  357. case []any:
  358. return tools
  359. default:
  360. return nil
  361. }
  362. }
  363. // ProcessTools 处理工具列表,支持类型断言
  364. func ProcessTools(tools []any) ([]*Tool, []*ClaudeWebSearchTool) {
  365. var normalTools []*Tool
  366. var webSearchTools []*ClaudeWebSearchTool
  367. for _, tool := range tools {
  368. switch t := tool.(type) {
  369. case *Tool:
  370. normalTools = append(normalTools, t)
  371. case *ClaudeWebSearchTool:
  372. webSearchTools = append(webSearchTools, t)
  373. case Tool:
  374. normalTools = append(normalTools, &t)
  375. case ClaudeWebSearchTool:
  376. webSearchTools = append(webSearchTools, &t)
  377. default:
  378. // 未知类型,跳过
  379. continue
  380. }
  381. }
  382. return normalTools, webSearchTools
  383. }
  384. type Thinking struct {
  385. Type string `json:"type"`
  386. BudgetTokens *int `json:"budget_tokens,omitempty"`
  387. }
  388. func (c *Thinking) GetBudgetTokens() int {
  389. if c.BudgetTokens == nil {
  390. return 0
  391. }
  392. return *c.BudgetTokens
  393. }
  394. func (c *ClaudeRequest) IsStringSystem() bool {
  395. _, ok := c.System.(string)
  396. return ok
  397. }
  398. func (c *ClaudeRequest) GetStringSystem() string {
  399. if c.IsStringSystem() {
  400. return c.System.(string)
  401. }
  402. return ""
  403. }
  404. func (c *ClaudeRequest) SetStringSystem(system string) {
  405. c.System = system
  406. }
  407. func (c *ClaudeRequest) ParseSystem() []ClaudeMediaMessage {
  408. mediaContent, _ := common.Any2Type[[]ClaudeMediaMessage](c.System)
  409. return mediaContent
  410. }
  411. type ClaudeErrorWithStatusCode struct {
  412. Error types.ClaudeError `json:"error"`
  413. StatusCode int `json:"status_code"`
  414. LocalError bool
  415. }
  416. type ClaudeResponse struct {
  417. Id string `json:"id,omitempty"`
  418. Type string `json:"type"`
  419. Role string `json:"role,omitempty"`
  420. Content []ClaudeMediaMessage `json:"content,omitempty"`
  421. Completion string `json:"completion,omitempty"`
  422. StopReason string `json:"stop_reason,omitempty"`
  423. Model string `json:"model,omitempty"`
  424. Error any `json:"error,omitempty"`
  425. Usage *ClaudeUsage `json:"usage,omitempty"`
  426. Index *int `json:"index,omitempty"`
  427. ContentBlock *ClaudeMediaMessage `json:"content_block,omitempty"`
  428. Delta *ClaudeMediaMessage `json:"delta,omitempty"`
  429. Message *ClaudeMediaMessage `json:"message,omitempty"`
  430. }
  431. // set index
  432. func (c *ClaudeResponse) SetIndex(i int) {
  433. c.Index = &i
  434. }
  435. // get index
  436. func (c *ClaudeResponse) GetIndex() int {
  437. if c.Index == nil {
  438. return 0
  439. }
  440. return *c.Index
  441. }
  442. // GetClaudeError 从动态错误类型中提取ClaudeError结构
  443. func (c *ClaudeResponse) GetClaudeError() *types.ClaudeError {
  444. if c.Error == nil {
  445. return nil
  446. }
  447. switch err := c.Error.(type) {
  448. case types.ClaudeError:
  449. return &err
  450. case *types.ClaudeError:
  451. return err
  452. case map[string]interface{}:
  453. // 处理从JSON解析来的map结构
  454. claudeErr := &types.ClaudeError{}
  455. if errType, ok := err["type"].(string); ok {
  456. claudeErr.Type = errType
  457. }
  458. if errMsg, ok := err["message"].(string); ok {
  459. claudeErr.Message = errMsg
  460. }
  461. return claudeErr
  462. case string:
  463. // 处理简单字符串错误
  464. return &types.ClaudeError{
  465. Type: "upstream_error",
  466. Message: err,
  467. }
  468. default:
  469. // 未知类型,尝试转换为字符串
  470. return &types.ClaudeError{
  471. Type: "unknown_upstream_error",
  472. Message: fmt.Sprintf("unknown_error: %v", err),
  473. }
  474. }
  475. }
  476. type ClaudeUsage struct {
  477. InputTokens int `json:"input_tokens"`
  478. CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
  479. CacheReadInputTokens int `json:"cache_read_input_tokens"`
  480. OutputTokens int `json:"output_tokens"`
  481. CacheCreation *ClaudeCacheCreationUsage `json:"cache_creation,omitempty"`
  482. // claude cache 1h
  483. ClaudeCacheCreation5mTokens int `json:"claude_cache_creation_5_m_tokens"`
  484. ClaudeCacheCreation1hTokens int `json:"claude_cache_creation_1_h_tokens"`
  485. ServerToolUse *ClaudeServerToolUse `json:"server_tool_use,omitempty"`
  486. }
  487. type ClaudeCacheCreationUsage struct {
  488. Ephemeral5mInputTokens int `json:"ephemeral_5m_input_tokens,omitempty"`
  489. Ephemeral1hInputTokens int `json:"ephemeral_1h_input_tokens,omitempty"`
  490. }
  491. func (u *ClaudeUsage) GetCacheCreation5mTokens() int {
  492. if u == nil || u.CacheCreation == nil {
  493. return 0
  494. }
  495. return u.CacheCreation.Ephemeral5mInputTokens
  496. }
  497. func (u *ClaudeUsage) GetCacheCreation1hTokens() int {
  498. if u == nil || u.CacheCreation == nil {
  499. return 0
  500. }
  501. return u.CacheCreation.Ephemeral1hInputTokens
  502. }
  503. func (u *ClaudeUsage) GetCacheCreationTotalTokens() int {
  504. if u == nil {
  505. return 0
  506. }
  507. if u.CacheCreationInputTokens > 0 {
  508. return u.CacheCreationInputTokens
  509. }
  510. return u.GetCacheCreation5mTokens() + u.GetCacheCreation1hTokens()
  511. }
  512. type ClaudeServerToolUse struct {
  513. WebSearchRequests int `json:"web_search_requests"`
  514. }