claude.go 6.2 KB

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