claude.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 ClaudeRequest struct {
  141. Model string `json:"model"`
  142. Prompt string `json:"prompt,omitempty"`
  143. System any `json:"system,omitempty"`
  144. Messages []ClaudeMessage `json:"messages,omitempty"`
  145. MaxTokens uint `json:"max_tokens,omitempty"`
  146. MaxTokensToSample uint `json:"max_tokens_to_sample,omitempty"`
  147. StopSequences []string `json:"stop_sequences,omitempty"`
  148. Temperature *float64 `json:"temperature,omitempty"`
  149. TopP float64 `json:"top_p,omitempty"`
  150. TopK int `json:"top_k,omitempty"`
  151. //ClaudeMetadata `json:"metadata,omitempty"`
  152. Stream bool `json:"stream,omitempty"`
  153. Tools any `json:"tools,omitempty"`
  154. ToolChoice any `json:"tool_choice,omitempty"`
  155. Thinking *Thinking `json:"thinking,omitempty"`
  156. }
  157. type Thinking struct {
  158. Type string `json:"type"`
  159. BudgetTokens *int `json:"budget_tokens,omitempty"`
  160. }
  161. func (c *Thinking) GetBudgetTokens() int {
  162. if c.BudgetTokens == nil {
  163. return 0
  164. }
  165. return *c.BudgetTokens
  166. }
  167. func (c *ClaudeRequest) IsStringSystem() bool {
  168. _, ok := c.System.(string)
  169. return ok
  170. }
  171. func (c *ClaudeRequest) GetStringSystem() string {
  172. if c.IsStringSystem() {
  173. return c.System.(string)
  174. }
  175. return ""
  176. }
  177. func (c *ClaudeRequest) SetStringSystem(system string) {
  178. c.System = system
  179. }
  180. func (c *ClaudeRequest) ParseSystem() []ClaudeMediaMessage {
  181. mediaContent, _ := common.Any2Type[[]ClaudeMediaMessage](c.System)
  182. return mediaContent
  183. }
  184. type ClaudeError struct {
  185. Type string `json:"type,omitempty"`
  186. Message string `json:"message,omitempty"`
  187. }
  188. type ClaudeErrorWithStatusCode struct {
  189. Error ClaudeError `json:"error"`
  190. StatusCode int `json:"status_code"`
  191. LocalError bool
  192. }
  193. type ClaudeResponse struct {
  194. Id string `json:"id,omitempty"`
  195. Type string `json:"type"`
  196. Role string `json:"role,omitempty"`
  197. Content []ClaudeMediaMessage `json:"content,omitempty"`
  198. Completion string `json:"completion,omitempty"`
  199. StopReason string `json:"stop_reason,omitempty"`
  200. Model string `json:"model,omitempty"`
  201. Error *ClaudeError `json:"error,omitempty"`
  202. Usage *ClaudeUsage `json:"usage,omitempty"`
  203. Index *int `json:"index,omitempty"`
  204. ContentBlock *ClaudeMediaMessage `json:"content_block,omitempty"`
  205. Delta *ClaudeMediaMessage `json:"delta,omitempty"`
  206. Message *ClaudeMediaMessage `json:"message,omitempty"`
  207. }
  208. // set index
  209. func (c *ClaudeResponse) SetIndex(i int) {
  210. c.Index = &i
  211. }
  212. // get index
  213. func (c *ClaudeResponse) GetIndex() int {
  214. if c.Index == nil {
  215. return 0
  216. }
  217. return *c.Index
  218. }
  219. type ClaudeUsage struct {
  220. InputTokens int `json:"input_tokens"`
  221. CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
  222. CacheReadInputTokens int `json:"cache_read_input_tokens"`
  223. OutputTokens int `json:"output_tokens"`
  224. }