claude.go 6.8 KB

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