claude.go 6.4 KB

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