claude.go 6.3 KB

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