claude.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package dto
  2. import "encoding/json"
  3. type ClaudeMetadata struct {
  4. UserId string `json:"user_id"`
  5. }
  6. type ClaudeMediaMessage struct {
  7. Type string `json:"type,omitempty"`
  8. Text *string `json:"text,omitempty"`
  9. Model string `json:"model,omitempty"`
  10. Source *ClaudeMessageSource `json:"source,omitempty"`
  11. Usage *ClaudeUsage `json:"usage,omitempty"`
  12. StopReason *string `json:"stop_reason,omitempty"`
  13. PartialJson *string `json:"partial_json,omitempty"`
  14. Role string `json:"role,omitempty"`
  15. Thinking string `json:"thinking,omitempty"`
  16. Signature string `json:"signature,omitempty"`
  17. Delta string `json:"delta,omitempty"`
  18. // tool_calls
  19. Id string `json:"id,omitempty"`
  20. Name string `json:"name,omitempty"`
  21. Input any `json:"input,omitempty"`
  22. Content json.RawMessage `json:"content,omitempty"`
  23. ToolUseId string `json:"tool_use_id,omitempty"`
  24. }
  25. func (c *ClaudeMediaMessage) SetText(s string) {
  26. c.Text = &s
  27. }
  28. func (c *ClaudeMediaMessage) GetText() string {
  29. if c.Text == nil {
  30. return ""
  31. }
  32. return *c.Text
  33. }
  34. func (c *ClaudeMediaMessage) IsStringContent() bool {
  35. var content string
  36. return json.Unmarshal(c.Content, &content) == nil
  37. }
  38. func (c *ClaudeMediaMessage) GetStringContent() string {
  39. var content string
  40. if err := json.Unmarshal(c.Content, &content); err == nil {
  41. return content
  42. }
  43. return ""
  44. }
  45. func (c *ClaudeMediaMessage) GetJsonRowString() string {
  46. jsonContent, _ := json.Marshal(c)
  47. return string(jsonContent)
  48. }
  49. func (c *ClaudeMediaMessage) SetContent(content any) {
  50. jsonContent, _ := json.Marshal(content)
  51. c.Content = jsonContent
  52. }
  53. func (c *ClaudeMediaMessage) ParseMediaContent() []ClaudeMediaMessage {
  54. var mediaContent []ClaudeMediaMessage
  55. if err := json.Unmarshal(c.Content, &mediaContent); err == nil {
  56. return mediaContent
  57. }
  58. return make([]ClaudeMediaMessage, 0)
  59. }
  60. type ClaudeMessageSource struct {
  61. Type string `json:"type"`
  62. MediaType string `json:"media_type"`
  63. Data any `json:"data"`
  64. }
  65. type ClaudeMessage struct {
  66. Role string `json:"role"`
  67. Content any `json:"content"`
  68. }
  69. func (c *ClaudeMessage) IsStringContent() bool {
  70. _, ok := c.Content.(string)
  71. return ok
  72. }
  73. func (c *ClaudeMessage) GetStringContent() string {
  74. if c.IsStringContent() {
  75. return c.Content.(string)
  76. }
  77. return ""
  78. }
  79. func (c *ClaudeMessage) SetStringContent(content string) {
  80. c.Content = content
  81. }
  82. func (c *ClaudeMessage) ParseContent() ([]ClaudeMediaMessage, error) {
  83. // map content to []ClaudeMediaMessage
  84. // parse to json
  85. jsonContent, _ := json.Marshal(c.Content)
  86. var contentList []ClaudeMediaMessage
  87. err := json.Unmarshal(jsonContent, &contentList)
  88. if err != nil {
  89. return make([]ClaudeMediaMessage, 0), err
  90. }
  91. return contentList, nil
  92. }
  93. type Tool struct {
  94. Name string `json:"name"`
  95. Description string `json:"description,omitempty"`
  96. InputSchema map[string]interface{} `json:"input_schema"`
  97. }
  98. type InputSchema struct {
  99. Type string `json:"type"`
  100. Properties any `json:"properties,omitempty"`
  101. Required any `json:"required,omitempty"`
  102. }
  103. type ClaudeRequest struct {
  104. Model string `json:"model"`
  105. Prompt string `json:"prompt,omitempty"`
  106. System any `json:"system,omitempty"`
  107. Messages []ClaudeMessage `json:"messages,omitempty"`
  108. MaxTokens uint `json:"max_tokens,omitempty"`
  109. MaxTokensToSample uint `json:"max_tokens_to_sample,omitempty"`
  110. StopSequences []string `json:"stop_sequences,omitempty"`
  111. Temperature *float64 `json:"temperature,omitempty"`
  112. TopP float64 `json:"top_p,omitempty"`
  113. TopK int `json:"top_k,omitempty"`
  114. //ClaudeMetadata `json:"metadata,omitempty"`
  115. Stream bool `json:"stream,omitempty"`
  116. Tools any `json:"tools,omitempty"`
  117. ToolChoice any `json:"tool_choice,omitempty"`
  118. Thinking *Thinking `json:"thinking,omitempty"`
  119. }
  120. type Thinking struct {
  121. Type string `json:"type"`
  122. BudgetTokens int `json:"budget_tokens"`
  123. }
  124. func (c *ClaudeRequest) IsStringSystem() bool {
  125. _, ok := c.System.(string)
  126. return ok
  127. }
  128. func (c *ClaudeRequest) GetStringSystem() string {
  129. if c.IsStringSystem() {
  130. return c.System.(string)
  131. }
  132. return ""
  133. }
  134. func (c *ClaudeRequest) SetStringSystem(system string) {
  135. c.System = system
  136. }
  137. func (c *ClaudeRequest) ParseSystem() []ClaudeMediaMessage {
  138. // map content to []ClaudeMediaMessage
  139. // parse to json
  140. jsonContent, _ := json.Marshal(c.System)
  141. var contentList []ClaudeMediaMessage
  142. if err := json.Unmarshal(jsonContent, &contentList); err == nil {
  143. return contentList
  144. }
  145. return make([]ClaudeMediaMessage, 0)
  146. }
  147. type ClaudeError struct {
  148. Type string `json:"type,omitempty"`
  149. Message string `json:"message,omitempty"`
  150. }
  151. type ClaudeErrorWithStatusCode struct {
  152. Error ClaudeError `json:"error"`
  153. StatusCode int `json:"status_code"`
  154. LocalError bool
  155. }
  156. type ClaudeResponse struct {
  157. Id string `json:"id,omitempty"`
  158. Type string `json:"type"`
  159. Role string `json:"role,omitempty"`
  160. Content []ClaudeMediaMessage `json:"content,omitempty"`
  161. Completion string `json:"completion,omitempty"`
  162. StopReason string `json:"stop_reason,omitempty"`
  163. Model string `json:"model,omitempty"`
  164. Error *ClaudeError `json:"error,omitempty"`
  165. Usage *ClaudeUsage `json:"usage,omitempty"`
  166. Index *int `json:"index,omitempty"`
  167. ContentBlock *ClaudeMediaMessage `json:"content_block,omitempty"`
  168. Delta *ClaudeMediaMessage `json:"delta,omitempty"`
  169. Message *ClaudeMediaMessage `json:"message,omitempty"`
  170. }
  171. // set index
  172. func (c *ClaudeResponse) SetIndex(i int) {
  173. c.Index = &i
  174. }
  175. // get index
  176. func (c *ClaudeResponse) GetIndex() int {
  177. if c.Index == nil {
  178. return 0
  179. }
  180. return *c.Index
  181. }
  182. type ClaudeUsage struct {
  183. InputTokens int `json:"input_tokens"`
  184. CacheCreationInputTokens int `json:"cache_creation_input_tokens"`
  185. CacheReadInputTokens int `json:"cache_read_input_tokens"`
  186. OutputTokens int `json:"output_tokens"`
  187. }