claude.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. package dto
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "one-api/common"
  6. "one-api/types"
  7. "strings"
  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) ParseContent() ([]ClaudeMediaMessage, error) {
  132. return common.Any2Type[[]ClaudeMediaMessage](c.Content)
  133. }
  134. type Tool struct {
  135. Name string `json:"name"`
  136. Description string `json:"description,omitempty"`
  137. InputSchema map[string]interface{} `json:"input_schema"`
  138. }
  139. type InputSchema struct {
  140. Type string `json:"type"`
  141. Properties any `json:"properties,omitempty"`
  142. Required any `json:"required,omitempty"`
  143. }
  144. type ClaudeWebSearchTool struct {
  145. Type string `json:"type"`
  146. Name string `json:"name"`
  147. MaxUses int `json:"max_uses,omitempty"`
  148. UserLocation *ClaudeWebSearchUserLocation `json:"user_location,omitempty"`
  149. }
  150. type ClaudeWebSearchUserLocation struct {
  151. Type string `json:"type"`
  152. Timezone string `json:"timezone,omitempty"`
  153. Country string `json:"country,omitempty"`
  154. Region string `json:"region,omitempty"`
  155. City string `json:"city,omitempty"`
  156. }
  157. type ClaudeToolChoice struct {
  158. Type string `json:"type"`
  159. Name string `json:"name,omitempty"`
  160. DisableParallelToolUse bool `json:"disable_parallel_tool_use,omitempty"`
  161. }
  162. type ClaudeRequest struct {
  163. Model string `json:"model"`
  164. Prompt string `json:"prompt,omitempty"`
  165. System any `json:"system,omitempty"`
  166. Messages []ClaudeMessage `json:"messages,omitempty"`
  167. MaxTokens uint `json:"max_tokens,omitempty"`
  168. MaxTokensToSample uint `json:"max_tokens_to_sample,omitempty"`
  169. StopSequences []string `json:"stop_sequences,omitempty"`
  170. Temperature *float64 `json:"temperature,omitempty"`
  171. TopP float64 `json:"top_p,omitempty"`
  172. TopK int `json:"top_k,omitempty"`
  173. Stream bool `json:"stream,omitempty"`
  174. Tools any `json:"tools,omitempty"`
  175. ContextManagement json.RawMessage `json:"context_management,omitempty"`
  176. ToolChoice any `json:"tool_choice,omitempty"`
  177. Thinking *Thinking `json:"thinking,omitempty"`
  178. McpServers json.RawMessage `json:"mcp_servers,omitempty"`
  179. Metadata json.RawMessage `json:"metadata,omitempty"`
  180. // 服务层级字段,用于指定 API 服务等级。允许透传可能导致实际计费高于预期,默认应过滤
  181. ServiceTier string `json:"service_tier,omitempty"`
  182. }
  183. func (c *ClaudeRequest) GetTokenCountMeta() *types.TokenCountMeta {
  184. var tokenCountMeta = types.TokenCountMeta{
  185. TokenType: types.TokenTypeTokenizer,
  186. MaxTokens: int(c.MaxTokens),
  187. }
  188. var texts = make([]string, 0)
  189. var fileMeta = make([]*types.FileMeta, 0)
  190. // system
  191. if c.System != nil {
  192. if c.IsStringSystem() {
  193. sys := c.GetStringSystem()
  194. if sys != "" {
  195. texts = append(texts, sys)
  196. }
  197. } else {
  198. systemMedia := c.ParseSystem()
  199. for _, media := range systemMedia {
  200. switch media.Type {
  201. case "text":
  202. texts = append(texts, media.GetText())
  203. case "image":
  204. if media.Source != nil {
  205. data := media.Source.Url
  206. if data == "" {
  207. data = common.Interface2String(media.Source.Data)
  208. }
  209. if data != "" {
  210. fileMeta = append(fileMeta, &types.FileMeta{FileType: types.FileTypeImage, OriginData: data})
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }
  217. // messages
  218. for _, message := range c.Messages {
  219. tokenCountMeta.MessagesCount++
  220. texts = append(texts, message.Role)
  221. if message.IsStringContent() {
  222. content := message.GetStringContent()
  223. if content != "" {
  224. texts = append(texts, content)
  225. }
  226. continue
  227. }
  228. content, _ := message.ParseContent()
  229. for _, media := range content {
  230. switch media.Type {
  231. case "text":
  232. texts = append(texts, media.GetText())
  233. case "image":
  234. if media.Source != nil {
  235. data := media.Source.Url
  236. if data == "" {
  237. data = common.Interface2String(media.Source.Data)
  238. }
  239. if data != "" {
  240. fileMeta = append(fileMeta, &types.FileMeta{FileType: types.FileTypeImage, OriginData: data})
  241. }
  242. }
  243. case "tool_use":
  244. if media.Name != "" {
  245. texts = append(texts, media.Name)
  246. }
  247. if media.Input != nil {
  248. b, _ := common.Marshal(media.Input)
  249. texts = append(texts, string(b))
  250. }
  251. case "tool_result":
  252. if media.Content != nil {
  253. b, _ := common.Marshal(media.Content)
  254. texts = append(texts, string(b))
  255. }
  256. }
  257. }
  258. }
  259. // tools
  260. if c.Tools != nil {
  261. tools := c.GetTools()
  262. normalTools, webSearchTools := ProcessTools(tools)
  263. if normalTools != nil {
  264. for _, t := range normalTools {
  265. tokenCountMeta.ToolsCount++
  266. if t.Name != "" {
  267. texts = append(texts, t.Name)
  268. }
  269. if t.Description != "" {
  270. texts = append(texts, t.Description)
  271. }
  272. if t.InputSchema != nil {
  273. b, _ := common.Marshal(t.InputSchema)
  274. texts = append(texts, string(b))
  275. }
  276. }
  277. }
  278. if webSearchTools != nil {
  279. for _, t := range webSearchTools {
  280. tokenCountMeta.ToolsCount++
  281. if t.Name != "" {
  282. texts = append(texts, t.Name)
  283. }
  284. if t.UserLocation != nil {
  285. b, _ := common.Marshal(t.UserLocation)
  286. texts = append(texts, string(b))
  287. }
  288. }
  289. }
  290. }
  291. tokenCountMeta.CombineText = strings.Join(texts, "\n")
  292. tokenCountMeta.Files = fileMeta
  293. return &tokenCountMeta
  294. }
  295. func (c *ClaudeRequest) IsStream(ctx *gin.Context) bool {
  296. return c.Stream
  297. }
  298. func (c *ClaudeRequest) SetModelName(modelName string) {
  299. if modelName != "" {
  300. c.Model = modelName
  301. }
  302. }
  303. func (c *ClaudeRequest) SearchToolNameByToolCallId(toolCallId string) string {
  304. for _, message := range c.Messages {
  305. content, _ := message.ParseContent()
  306. for _, mediaMessage := range content {
  307. if mediaMessage.Id == toolCallId {
  308. return mediaMessage.Name
  309. }
  310. }
  311. }
  312. return ""
  313. }
  314. // AddTool 添加工具到请求中
  315. func (c *ClaudeRequest) AddTool(tool any) {
  316. if c.Tools == nil {
  317. c.Tools = make([]any, 0)
  318. }
  319. switch tools := c.Tools.(type) {
  320. case []any:
  321. c.Tools = append(tools, tool)
  322. default:
  323. // 如果Tools不是[]any类型,重新初始化为[]any
  324. c.Tools = []any{tool}
  325. }
  326. }
  327. // GetTools 获取工具列表
  328. func (c *ClaudeRequest) GetTools() []any {
  329. if c.Tools == nil {
  330. return nil
  331. }
  332. switch tools := c.Tools.(type) {
  333. case []any:
  334. return tools
  335. default:
  336. return nil
  337. }
  338. }
  339. // ProcessTools 处理工具列表,支持类型断言
  340. func ProcessTools(tools []any) ([]*Tool, []*ClaudeWebSearchTool) {
  341. var normalTools []*Tool
  342. var webSearchTools []*ClaudeWebSearchTool
  343. for _, tool := range tools {
  344. switch t := tool.(type) {
  345. case *Tool:
  346. normalTools = append(normalTools, t)
  347. case *ClaudeWebSearchTool:
  348. webSearchTools = append(webSearchTools, t)
  349. case Tool:
  350. normalTools = append(normalTools, &t)
  351. case ClaudeWebSearchTool:
  352. webSearchTools = append(webSearchTools, &t)
  353. default:
  354. // 未知类型,跳过
  355. continue
  356. }
  357. }
  358. return normalTools, webSearchTools
  359. }
  360. type Thinking struct {
  361. Type string `json:"type"`
  362. BudgetTokens *int `json:"budget_tokens,omitempty"`
  363. }
  364. func (c *Thinking) GetBudgetTokens() int {
  365. if c.BudgetTokens == nil {
  366. return 0
  367. }
  368. return *c.BudgetTokens
  369. }
  370. func (c *ClaudeRequest) IsStringSystem() bool {
  371. _, ok := c.System.(string)
  372. return ok
  373. }
  374. func (c *ClaudeRequest) GetStringSystem() string {
  375. if c.IsStringSystem() {
  376. return c.System.(string)
  377. }
  378. return ""
  379. }
  380. func (c *ClaudeRequest) SetStringSystem(system string) {
  381. c.System = system
  382. }
  383. func (c *ClaudeRequest) ParseSystem() []ClaudeMediaMessage {
  384. mediaContent, _ := common.Any2Type[[]ClaudeMediaMessage](c.System)
  385. return mediaContent
  386. }
  387. type ClaudeErrorWithStatusCode struct {
  388. Error types.ClaudeError `json:"error"`
  389. StatusCode int `json:"status_code"`
  390. LocalError bool
  391. }
  392. type ClaudeResponse struct {
  393. Id string `json:"id,omitempty"`
  394. Type string `json:"type"`
  395. Role string `json:"role,omitempty"`
  396. Content []ClaudeMediaMessage `json:"content,omitempty"`
  397. Completion string `json:"completion,omitempty"`
  398. StopReason string `json:"stop_reason,omitempty"`
  399. Model string `json:"model,omitempty"`
  400. Error any `json:"error,omitempty"`
  401. Usage *ClaudeUsage `json:"usage,omitempty"`
  402. Index *int `json:"index,omitempty"`
  403. ContentBlock *ClaudeMediaMessage `json:"content_block,omitempty"`
  404. Delta *ClaudeMediaMessage `json:"delta,omitempty"`
  405. Message *ClaudeMediaMessage `json:"message,omitempty"`
  406. }
  407. // set index
  408. func (c *ClaudeResponse) SetIndex(i int) {
  409. c.Index = &i
  410. }
  411. // get index
  412. func (c *ClaudeResponse) GetIndex() int {
  413. if c.Index == nil {
  414. return 0
  415. }
  416. return *c.Index
  417. }
  418. // GetClaudeError 从动态错误类型中提取ClaudeError结构
  419. func (c *ClaudeResponse) GetClaudeError() *types.ClaudeError {
  420. if c.Error == nil {
  421. return nil
  422. }
  423. switch err := c.Error.(type) {
  424. case types.ClaudeError:
  425. return &err
  426. case *types.ClaudeError:
  427. return err
  428. case map[string]interface{}:
  429. // 处理从JSON解析来的map结构
  430. claudeErr := &types.ClaudeError{}
  431. if errType, ok := err["type"].(string); ok {
  432. claudeErr.Type = errType
  433. }
  434. if errMsg, ok := err["message"].(string); ok {
  435. claudeErr.Message = errMsg
  436. }
  437. return claudeErr
  438. case string:
  439. // 处理简单字符串错误
  440. return &types.ClaudeError{
  441. Type: "upstream_error",
  442. Message: err,
  443. }
  444. default:
  445. // 未知类型,尝试转换为字符串
  446. return &types.ClaudeError{
  447. Type: "unknown_upstream_error",
  448. Message: fmt.Sprintf("unknown_error: %v", err),
  449. }
  450. }
  451. }
  452. type ClaudeUsage struct {
  453. InputTokens int `json:"input_tokens"`
  454. CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
  455. CacheReadInputTokens int `json:"cache_read_input_tokens"`
  456. OutputTokens int `json:"output_tokens"`
  457. ServerToolUse *ClaudeServerToolUse `json:"server_tool_use,omitempty"`
  458. }
  459. type ClaudeServerToolUse struct {
  460. WebSearchRequests int `json:"web_search_requests"`
  461. }