Documentation
¶
Overview ¶
Example ¶
package main import ( "context" "fmt" "os" claude "github.com/potproject/claude-sdk-go" ) func main() { apiKey := os.Getenv("API_KEY") c := claude.NewClient(apiKey) m := claude.RequestBodyMessages{ Model: "claude-3-7-sonnet-20250219", MaxTokens: 1024, Messages: []claude.RequestBodyMessagesMessages{ { Role: claude.MessagesRoleUser, Content: "Hello, world!", }, }, } ctx := context.Background() res, err := c.CreateMessages(ctx, m) if err != nil { panic(err) } fmt.Println(res.Content[0].Text) }
Output: Hello! How can I assist you today?
Example (Cache) ¶
package main import ( "context" "fmt" "os" claude "github.com/potproject/claude-sdk-go" ) func main() { apiKey := os.Getenv("API_KEY") c := claude.NewClient(apiKey) m := claude.RequestBodyMessages{ Model: "claude-3-7-sonnet-20250219", MaxTokens: 1024, SystemTypeText: []claude.RequestBodySystemTypeText{ claude.UseSystemCacheEphemeral("Please speak in Japanese."), }, Messages: []claude.RequestBodyMessagesMessages{ { Role: claude.MessagesRoleUser, ContentTypeText: []claude.RequestBodyMessagesMessagesContentTypeText{ { Text: "Hello!", CacheControl: claude.UseCacheEphemeral(), }, }, }, }, } ctx := context.Background() res, err := c.CreateMessages(ctx, m) if err != nil { panic(err) } fmt.Println(res.Content[0].Text) }
Output: こんにちは!日本語でお話しましょう。
Example (Stream) ¶
package main import ( "context" "errors" "fmt" "io" "os" claude "github.com/potproject/claude-sdk-go" ) func main() { apiKey := os.Getenv("API_KEY") c := claude.NewClient(apiKey) m := claude.RequestBodyMessages{ Model: "claude-3-7-sonnet-20250219", MaxTokens: 1024, Messages: []claude.RequestBodyMessagesMessages{ { Role: claude.MessagesRoleUser, Content: "Hello, world!", }, }, } ctx := context.Background() stream, err := c.CreateMessagesStream(ctx, m) if err != nil { panic(err) } defer stream.Close() for { res, err := stream.Recv() if errors.Is(err, io.EOF) { break } if err != nil { panic(err) } fmt.Printf("%s", res.Content[0].Text) } fmt.Println() }
Output: Hello! How can I assist you today?
Example (Thinking) ¶
package main import ( "context" "fmt" "os" claude "github.com/potproject/claude-sdk-go" ) func main() { apiKey := os.Getenv("API_KEY") c := claude.NewClient(apiKey) m := claude.RequestBodyMessages{ Model: "claude-3-7-sonnet-20250219", MaxTokens: 8192, Thinking: claude.UseThinking(4096), Messages: []claude.RequestBodyMessagesMessages{ { Role: claude.MessagesRoleUser, Content: "Hello, world!", }, }, } ctx := context.Background() res, err := c.CreateMessages(ctx, m) if err != nil { panic(err) } for _, v := range res.Content { if v.Type == claude.ResponseBodyMessagesContentTypeThinking { fmt.Println("[thinking]", v.Thinking) } if v.Type == claude.ResponseBodyMessagesContentTypeText { fmt.Println("[text]", v.Text) } } }
Output: [thinking] This is a simple "Hello, world!" greeting from the user. It's a common first phrase in programming and also a standard greeting in conversations with AI assistants. I should respond in a friendly and welcoming manner. [text] Hi there! It's nice to meet you. "Hello, world!" is such a classic greeting - it brings back memories of first programming lessons for many! How are you doing today? Is there something specific I can help you with?
Example (Thinking_stream) ¶
package main import ( "context" "errors" "fmt" "io" "os" claude "github.com/potproject/claude-sdk-go" ) func main() { apiKey := os.Getenv("API_KEY") c := claude.NewClient(apiKey) m := claude.RequestBodyMessages{ Model: "claude-3-7-sonnet-20250219", MaxTokens: 8192, Thinking: claude.UseThinking(4096), Messages: []claude.RequestBodyMessagesMessages{ { Role: claude.MessagesRoleUser, Content: "Guess the Earth's population in 2100", }, }, } ctx := context.Background() stream, err := c.CreateMessagesStream(ctx, m) if err != nil { panic(err) } defer stream.Close() streamType := "" for { res, err := stream.Recv() if errors.Is(err, io.EOF) { break } if err != nil { panic(err) } if res.Content[0].Type == claude.ResponseBodyMessagesContentTypeThinking && streamType != claude.ResponseBodyMessagesContentTypeThinking { fmt.Println("[thinking]") streamType = claude.ResponseBodyMessagesContentTypeThinking } if res.Content[0].Type == claude.ResponseBodyMessagesContentTypeText && streamType != claude.ResponseBodyMessagesContentTypeText { fmt.Println() fmt.Println("[text]") streamType = claude.ResponseBodyMessagesContentTypeText } fmt.Printf("%s", res.Content[0].Thinking) fmt.Printf("%s", res.Content[0].Text) } fmt.Println()
Example (TypeImage) ¶
package main import ( "context" "fmt" "os" claude "github.com/potproject/claude-sdk-go" ) func main() { apiKey := os.Getenv("API_KEY") c := claude.NewClient(apiKey) m := claude.RequestBodyMessages{ Model: "claude-3-7-sonnet-20250219", MaxTokens: 1024, Messages: []claude.RequestBodyMessagesMessages{ { Role: claude.MessagesRoleUser, ContentTypeImage: []claude.RequestBodyMessagesMessagesContentTypeImage{ { Source: claude.TypeImageSourceLoadBase64("image/png", "iVBORw0KGgoAAAANSUhEUgAAAQAAAAEACAIAAADTED8xAAADMElEQVR4nOzVwQnAIBQFQYXff81RUkQCOyDj1YOPnbXWPmeTRef+/3O/OyBjzh3CD95BfqICMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMK0CMO0TAAD//2Anhf4QtqobAAAAAElFTkSuQmCC"), }, }, }, }, } ctx := context.Background() res, err := c.CreateMessages(ctx, m) if err != nil { panic(err) } fmt.Println(res.Content[0].Text) }
Output: The image shows a smooth gradient transitioning from a bright, vibrant orange at the bottom to a light blue at the top. The colors blend seamlessly into each other, creating a visually striking and aesthetically pleasing effect. The simplicity of the gradient allows the colors to be the main focus, showcasing their luminosity and the way they interact with one another. This type of gradient is often used as a background or design element to add depth, warmth, and visual interest to various digital or print media projects.
Index ¶
- Constants
- type Client
- type ClientConfig
- type CreateMessagesStream
- type RequestBodyMessages
- type RequestBodyMessagesMessages
- type RequestBodyMessagesMessagesContentTypeImage
- type RequestBodyMessagesMessagesContentTypeImageSource
- func TypeImageSourceLoadBase64(mediaType string, data string) RequestBodyMessagesMessagesContentTypeImageSource
- func TypeImageSourceLoadFile(filePath string) (RequestBodyMessagesMessagesContentTypeImageSource, error)
- func TypeImageSourceLoadUrl(url string) RequestBodyMessagesMessagesContentTypeImageSource
- type RequestBodyMessagesMessagesContentTypeText
- type RequestBodyMessagesThinking
- type RequestBodySystemTypeText
- type RequestCacheControl
- type ResponseBodyMessages
- type ResponseBodyMessagesContent
- type ResponseBodyMessagesContentStream
- type ResponseBodyMessagesStream
- type ResponseContentBlockDeltaStream
- type ResponseContentMessageStartStream
- type ResponseError
- type ResponseMessageDeltaStream
Examples ¶
Constants ¶
View Source
const ( RequestBodyMessagesMessagesContentTypeTextType = "text" RequestBodyMessagesMessagesContentTypeImageType = "image" )
View Source
const ( RequestBodyMessagesMessagesContentTypeImageSourceTypeBase64 = "base64" RequestBodyMessagesMessagesContentTypeImageSourceTypeUrl = "url" )
View Source
const ( MessagesRoleUser = "user" MessagesRoleAssistant = "assistant" )
View Source
const ( ResponseBodyMessagesContentTypeMessage = "message" ResponseBodyMessagesContentTypeText = "text" ResponseBodyMessagesContentTypeThinking = "thinking" )
View Source
const ( MessagesStreamResponseTypeMessageStart = "message_start" MessagesStreamResponseTypeContentBlockStart = "content_block_start" MessagesStreamResponseTypePing = "ping" MessagesStreamResponseTypeContentBlockDelta = "content_block_delta" MessagesStreamResponseTypeContentBlockStop = "content_block_stop" MessagesStreamResponseTypeMessageDelta = "message_delta" MessagesStreamResponseTypeMessageStop = "message_stop" MessagesStreamResponseTypeError = "error" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClientWithConfig ¶
func NewClientWithConfig(config ClientConfig) *Client
func (*Client) CreateMessages ¶
func (c *Client) CreateMessages(ctx context.Context, body RequestBodyMessages) (*ResponseBodyMessages, error)
func (*Client) CreateMessagesStream ¶ added in v1.1.0
func (c *Client) CreateMessagesStream(ctx context.Context, body RequestBodyMessages) (*CreateMessagesStream, error)
func (*Client) SetVersion ¶
type ClientConfig ¶
type CreateMessagesStream ¶ added in v1.1.0
type CreateMessagesStream struct { Connection *sse.Connection Unsubscribe func() Event chan sse.Event Error chan error ResponseBodyMessagesStream ResponseBodyMessagesStream }
func (*CreateMessagesStream) Close ¶ added in v1.1.0
func (c *CreateMessagesStream) Close()
func (*CreateMessagesStream) Recv ¶ added in v1.1.0
func (c *CreateMessagesStream) Recv() (ResponseBodyMessagesStream, error)
type RequestBodyMessages ¶
type RequestBodyMessages struct { Model string `json:"model"` Messages []RequestBodyMessagesMessages `json:"messages"` System string `json:"-"` SystemTypeText []RequestBodySystemTypeText `json:"-"` SystemRaw interface{} `json:"system,omitempty"` // optional MaxTokens int `json:"max_tokens"` Thinking *RequestBodyMessagesThinking `json:"thinking,omitempty"` // optional MetaData map[string]interface{} `json:"metadata"` // optional StopSequences []string `json:"stop_sequences"` // optional Stream bool `json:"stream"` // optional Temperature float64 `json:"temperature,omitempty"` // optional TopP float64 `json:"top_p,omitempty"` // optional TopK float64 `json:"top_k,omitempty"` // optional }
type RequestBodyMessagesMessages ¶
type RequestBodyMessagesMessages struct { Role string `json:"role"` ContentRaw interface{} `json:"content"` Content string `json:"-"` ContentTypeText []RequestBodyMessagesMessagesContentTypeText `json:"-"` ContentTypeImage []RequestBodyMessagesMessagesContentTypeImage `json:"-"` }
type RequestBodyMessagesMessagesContentTypeImage ¶
type RequestBodyMessagesMessagesContentTypeImage struct { Type string `json:"type"` // always "image" Source RequestBodyMessagesMessagesContentTypeImageSource `json:"source"` CacheControl *RequestCacheControl `json:"cache_control"` // optional }
type RequestBodyMessagesMessagesContentTypeImageSource ¶
type RequestBodyMessagesMessagesContentTypeImageSource struct { Type string `json:"type"` // "base64" or "url" MediaType string `json:"media_type,omitempty"` // base64 type required Data string `json:"data,omitempty"` // base64 type required Url string `json:"url,omitempty"` // url type required }
func TypeImageSourceLoadBase64 ¶ added in v1.3.2
func TypeImageSourceLoadBase64(mediaType string, data string) RequestBodyMessagesMessagesContentTypeImageSource
func TypeImageSourceLoadFile ¶ added in v1.1.3
func TypeImageSourceLoadFile(filePath string) (RequestBodyMessagesMessagesContentTypeImageSource, error)
func TypeImageSourceLoadUrl ¶ added in v1.3.1
func TypeImageSourceLoadUrl(url string) RequestBodyMessagesMessagesContentTypeImageSource
type RequestBodyMessagesMessagesContentTypeText ¶
type RequestBodyMessagesMessagesContentTypeText struct { Type string `json:"type"` // always "text" Text string `json:"text"` CacheControl *RequestCacheControl `json:"cache_control"` // optional }
type RequestBodyMessagesThinking ¶ added in v1.3.0
type RequestBodyMessagesThinking struct { Type string `json:"type"` BudgetTokens int `json:"budget_tokens"` }
func UseThinking ¶ added in v1.3.0
func UseThinking(budgetTokens int) *RequestBodyMessagesThinking
type RequestBodySystemTypeText ¶ added in v1.3.0
type RequestBodySystemTypeText struct { Type string `json:"type"` // always "text" Text string `json:"text"` CacheControl *RequestCacheControl `json:"cache_control"` }
func UseSystemCacheEphemeral ¶ added in v1.3.0
func UseSystemCacheEphemeral(text string) RequestBodySystemTypeText
func UseSystemNoCache ¶ added in v1.3.0
func UseSystemNoCache(text string) RequestBodySystemTypeText
type RequestCacheControl ¶ added in v1.3.0
type RequestCacheControl struct {
Type string `json:"type"` // always "ephemeral"
}
func UseCacheEphemeral ¶ added in v1.3.0
func UseCacheEphemeral() *RequestCacheControl
func UseNoCache ¶ added in v1.3.0
func UseNoCache() *RequestCacheControl
type ResponseBodyMessages ¶
type ResponseBodyMessages struct { Id string `json:"id"` Type string `json:"type"` // always "message" Role string `json:"role"` // always "assistant" Content []ResponseBodyMessagesContent `json:"content"` Model string `json:"model"` StopReason string `json:"stop_reason"` // "end_turn" or "max_tokens", "stop_sequence", null StopSequence string `json:"stop_sequence"` Usage struct { InputTokens int64 `json:"input_tokens"` OutputTokens int64 `json:"output_tokens"` } `json:"usage"` }
type ResponseBodyMessagesContentStream ¶ added in v1.1.0
type ResponseBodyMessagesStream ¶ added in v1.1.0
type ResponseBodyMessagesStream struct { Id string `json:"id"` Type string `json:"type"` // always "message" Role string `json:"role"` // always "assistant" Content []ResponseBodyMessagesContentStream `json:"content"` Model string `json:"model"` StopReason string `json:"stop_reason"` // "end_turn" or "max_tokens", "stop_sequence", null StopSequence string `json:"stop_sequence"` Usage struct { InputTokens int64 `json:"input_tokens"` OutputTokens int64 `json:"output_tokens"` } `json:"usage"` }
type ResponseContentBlockDeltaStream ¶ added in v1.1.0
type ResponseContentMessageStartStream ¶ added in v1.1.0
type ResponseContentMessageStartStream struct { Type string `json:"type"` Message ResponseBodyMessagesStream `json:"message"` }
type ResponseError ¶
type ResponseError struct { Error struct { Message string `json:"message"` } `json:"error"` }
type ResponseMessageDeltaStream ¶ added in v1.1.0
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
example
|
|
messages
Module
|
|
messages_image
Module
|
|
messages_image_file
Module
|
|
messages_stream
Module
|
Click to show internal directories.
Click to hide internal directories.