claude.go 5.5 KB

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