claude.go 8.9 KB

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