claude.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package dto
  2. import (
  3. "encoding/json"
  4. "one-api/common"
  5. )
  6. type ClaudeMetadata struct {
  7. UserId string `json:"user_id"`
  8. }
  9. type ClaudeMediaMessage struct {
  10. Type string `json:"type,omitempty"`
  11. Text *string `json:"text,omitempty"`
  12. Model string `json:"model,omitempty"`
  13. Source *ClaudeMessageSource `json:"source,omitempty"`
  14. Usage *ClaudeUsage `json:"usage,omitempty"`
  15. StopReason *string `json:"stop_reason,omitempty"`
  16. PartialJson *string `json:"partial_json,omitempty"`
  17. Role string `json:"role,omitempty"`
  18. Thinking string `json:"thinking,omitempty"`
  19. Signature string `json:"signature,omitempty"`
  20. Delta string `json:"delta,omitempty"`
  21. CacheControl json.RawMessage `json:"cache_control,omitempty"`
  22. // tool_calls
  23. Id string `json:"id,omitempty"`
  24. Name string `json:"name,omitempty"`
  25. Input any `json:"input,omitempty"`
  26. Content any `json:"content,omitempty"`
  27. ToolUseId string `json:"tool_use_id,omitempty"`
  28. }
  29. func (c *ClaudeMediaMessage) SetText(s string) {
  30. c.Text = &s
  31. }
  32. func (c *ClaudeMediaMessage) GetText() string {
  33. if c.Text == nil {
  34. return ""
  35. }
  36. return *c.Text
  37. }
  38. func (c *ClaudeMediaMessage) IsStringContent() bool {
  39. if c.Content == nil {
  40. return false
  41. }
  42. _, ok := c.Content.(string)
  43. if ok {
  44. return true
  45. }
  46. return false
  47. }
  48. func (c *ClaudeMediaMessage) GetStringContent() string {
  49. if c.Content == nil {
  50. return ""
  51. }
  52. switch c.Content.(type) {
  53. case string:
  54. return c.Content.(string)
  55. case []any:
  56. var contentStr string
  57. for _, contentItem := range c.Content.([]any) {
  58. contentMap, ok := contentItem.(map[string]any)
  59. if !ok {
  60. continue
  61. }
  62. if contentMap["type"] == ContentTypeText {
  63. if subStr, ok := contentMap["text"].(string); ok {
  64. contentStr += subStr
  65. }
  66. }
  67. }
  68. return contentStr
  69. }
  70. return ""
  71. }
  72. func (c *ClaudeMediaMessage) GetJsonRowString() string {
  73. jsonContent, _ := json.Marshal(c)
  74. return string(jsonContent)
  75. }
  76. func (c *ClaudeMediaMessage) SetContent(content any) {
  77. c.Content = content
  78. }
  79. func (c *ClaudeMediaMessage) ParseMediaContent() []ClaudeMediaMessage {
  80. mediaContent, _ := common.Any2Type[[]ClaudeMediaMessage](c.Content)
  81. return mediaContent
  82. }
  83. type ClaudeMessageSource struct {
  84. Type string `json:"type"`
  85. MediaType string `json:"media_type,omitempty"`
  86. Data any `json:"data,omitempty"`
  87. Url string `json:"url,omitempty"`
  88. }
  89. type ClaudeMessage struct {
  90. Role string `json:"role"`
  91. Content any `json:"content"`
  92. }
  93. func (c *ClaudeMessage) IsStringContent() bool {
  94. if c.Content == nil {
  95. return false
  96. }
  97. _, ok := c.Content.(string)
  98. return ok
  99. }
  100. func (c *ClaudeMessage) GetStringContent() string {
  101. if c.Content == nil {
  102. return ""
  103. }
  104. switch c.Content.(type) {
  105. case string:
  106. return c.Content.(string)
  107. case []any:
  108. var contentStr string
  109. for _, contentItem := range c.Content.([]any) {
  110. contentMap, ok := contentItem.(map[string]any)
  111. if !ok {
  112. continue
  113. }
  114. if contentMap["type"] == ContentTypeText {
  115. if subStr, ok := contentMap["text"].(string); ok {
  116. contentStr += subStr
  117. }
  118. }
  119. }
  120. return contentStr
  121. }
  122. return ""
  123. }
  124. func (c *ClaudeMessage) SetStringContent(content string) {
  125. c.Content = content
  126. }
  127. func (c *ClaudeMessage) ParseContent() ([]ClaudeMediaMessage, error) {
  128. return common.Any2Type[[]ClaudeMediaMessage](c.Content)
  129. }
  130. type Tool struct {
  131. Name string `json:"name"`
  132. Description string `json:"description,omitempty"`
  133. InputSchema map[string]interface{} `json:"input_schema"`
  134. }
  135. type InputSchema struct {
  136. Type string `json:"type"`
  137. Properties any `json:"properties,omitempty"`
  138. Required any `json:"required,omitempty"`
  139. }
  140. type ClaudeWebSearchTool struct {
  141. Type string `json:"type"`
  142. Name string `json:"name"`
  143. MaxUses int `json:"max_uses,omitempty"`
  144. UserLocation *ClaudeWebSearchUserLocation `json:"user_location,omitempty"`
  145. }
  146. type ClaudeWebSearchUserLocation struct {
  147. Type string `json:"type"`
  148. Timezone string `json:"timezone,omitempty"`
  149. Country string `json:"country,omitempty"`
  150. Region string `json:"region,omitempty"`
  151. City string `json:"city,omitempty"`
  152. }
  153. type ClaudeToolChoice struct {
  154. Type string `json:"type"`
  155. Name string `json:"name,omitempty"`
  156. DisableParallelToolUse bool `json:"disable_parallel_tool_use,omitempty"`
  157. }
  158. type ClaudeRequest struct {
  159. Model string `json:"model"`
  160. Prompt string `json:"prompt,omitempty"`
  161. System any `json:"system,omitempty"`
  162. Messages []ClaudeMessage `json:"messages,omitempty"`
  163. MaxTokens uint `json:"max_tokens,omitempty"`
  164. MaxTokensToSample uint `json:"max_tokens_to_sample,omitempty"`
  165. StopSequences []string `json:"stop_sequences,omitempty"`
  166. Temperature *float64 `json:"temperature,omitempty"`
  167. TopP float64 `json:"top_p,omitempty"`
  168. TopK int `json:"top_k,omitempty"`
  169. //ClaudeMetadata `json:"metadata,omitempty"`
  170. Stream bool `json:"stream,omitempty"`
  171. Tools any `json:"tools,omitempty"`
  172. ToolChoice any `json:"tool_choice,omitempty"`
  173. Thinking *Thinking `json:"thinking,omitempty"`
  174. }
  175. // AddTool 添加工具到请求中
  176. func (c *ClaudeRequest) AddTool(tool any) {
  177. if c.Tools == nil {
  178. c.Tools = make([]any, 0)
  179. }
  180. switch tools := c.Tools.(type) {
  181. case []any:
  182. c.Tools = append(tools, tool)
  183. default:
  184. // 如果Tools不是[]any类型,重新初始化为[]any
  185. c.Tools = []any{tool}
  186. }
  187. }
  188. // GetTools 获取工具列表
  189. func (c *ClaudeRequest) GetTools() []any {
  190. if c.Tools == nil {
  191. return nil
  192. }
  193. switch tools := c.Tools.(type) {
  194. case []any:
  195. return tools
  196. default:
  197. return nil
  198. }
  199. }
  200. // ProcessTools 处理工具列表,支持类型断言
  201. func ProcessTools(tools []any) ([]*Tool, []*ClaudeWebSearchTool) {
  202. var normalTools []*Tool
  203. var webSearchTools []*ClaudeWebSearchTool
  204. for _, tool := range tools {
  205. switch t := tool.(type) {
  206. case *Tool:
  207. normalTools = append(normalTools, t)
  208. case *ClaudeWebSearchTool:
  209. webSearchTools = append(webSearchTools, t)
  210. case Tool:
  211. normalTools = append(normalTools, &t)
  212. case ClaudeWebSearchTool:
  213. webSearchTools = append(webSearchTools, &t)
  214. default:
  215. // 未知类型,跳过
  216. continue
  217. }
  218. }
  219. return normalTools, webSearchTools
  220. }
  221. type Thinking struct {
  222. Type string `json:"type"`
  223. BudgetTokens *int `json:"budget_tokens,omitempty"`
  224. }
  225. func (c *Thinking) GetBudgetTokens() int {
  226. if c.BudgetTokens == nil {
  227. return 0
  228. }
  229. return *c.BudgetTokens
  230. }
  231. func (c *ClaudeRequest) IsStringSystem() bool {
  232. _, ok := c.System.(string)
  233. return ok
  234. }
  235. func (c *ClaudeRequest) GetStringSystem() string {
  236. if c.IsStringSystem() {
  237. return c.System.(string)
  238. }
  239. return ""
  240. }
  241. func (c *ClaudeRequest) SetStringSystem(system string) {
  242. c.System = system
  243. }
  244. func (c *ClaudeRequest) ParseSystem() []ClaudeMediaMessage {
  245. mediaContent, _ := common.Any2Type[[]ClaudeMediaMessage](c.System)
  246. return mediaContent
  247. }
  248. type ClaudeError struct {
  249. Type string `json:"type,omitempty"`
  250. Message string `json:"message,omitempty"`
  251. }
  252. type ClaudeErrorWithStatusCode struct {
  253. Error ClaudeError `json:"error"`
  254. StatusCode int `json:"status_code"`
  255. LocalError bool
  256. }
  257. type ClaudeResponse struct {
  258. Id string `json:"id,omitempty"`
  259. Type string `json:"type"`
  260. Role string `json:"role,omitempty"`
  261. Content []ClaudeMediaMessage `json:"content,omitempty"`
  262. Completion string `json:"completion,omitempty"`
  263. StopReason string `json:"stop_reason,omitempty"`
  264. Model string `json:"model,omitempty"`
  265. Error *ClaudeError `json:"error,omitempty"`
  266. Usage *ClaudeUsage `json:"usage,omitempty"`
  267. Index *int `json:"index,omitempty"`
  268. ContentBlock *ClaudeMediaMessage `json:"content_block,omitempty"`
  269. Delta *ClaudeMediaMessage `json:"delta,omitempty"`
  270. Message *ClaudeMediaMessage `json:"message,omitempty"`
  271. }
  272. // set index
  273. func (c *ClaudeResponse) SetIndex(i int) {
  274. c.Index = &i
  275. }
  276. // get index
  277. func (c *ClaudeResponse) GetIndex() int {
  278. if c.Index == nil {
  279. return 0
  280. }
  281. return *c.Index
  282. }
  283. type ClaudeUsage struct {
  284. InputTokens int `json:"input_tokens"`
  285. CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
  286. CacheReadInputTokens int `json:"cache_read_input_tokens"`
  287. OutputTokens int `json:"output_tokens"`
  288. }