Skip to content

Commit c01b388

Browse files
committed
chore: Improve documentation in source files
1 parent b49ea86 commit c01b388

File tree

8 files changed

+110
-35
lines changed

8 files changed

+110
-35
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.editorconfig export-ignore
2+
.github/ export-ignore

.github/workflows/ci.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ jobs:
3232
release:
3333
name: Release?
3434
needs: [ verify ]
35+
outputs:
36+
version: ${{ steps.release.outputs.version }}
3537
permissions:
3638
contents: write
3739
pull-requests: write
@@ -46,3 +48,14 @@ jobs:
4648
config-file: .github/release-config.json
4749
manifest-file: .github/release-manifest.json
4850
token: ${{ secrets.GITHUB_TOKEN }}
51+
52+
configure_version:
53+
name: Configure version
54+
if: ${{ steps.release.outputs.version }}
55+
needs: [ release ]
56+
runs-on: ubuntu-latest
57+
steps:
58+
- name: Checkout code
59+
uses: actions/checkout@v4
60+
- run: |
61+
sed 's/^[^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p'

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Anthropic SDK for Go
22

3+
> [!NOTE]
4+
> This is not an official SDK and I have no affiliation with [Anthropic].
5+
36
Client library for interacting with the [Anthropic] safety-first language model
47
REST APIs.
58

anthropic.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func NewClient(client *http.Client) *Client {
3737
claude := &Client{client: client}
3838
claude.baseURL, _ = url.Parse(defaultBaseURL)
3939
claude.reusable.client = claude
40+
4041
claude.Messages = (*MessagesService)(&claude.reusable)
4142

4243
return claude

messages.go

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,13 @@ type Message struct {
1313
Role string `json:"role"`
1414
}
1515

16-
// Usage defines billing and rate-limit usage information.
17-
// Billing and rate-limiting are driven by token counts, since tokens represent
18-
// the underlying cost to Anthropic.
19-
type Usage struct {
20-
InputTokens int `json:"input_tokens"`
21-
OutputTokens int `json:"output_tokens"`
22-
}
23-
2416
type Content struct {
2517
Type string `json:"type"`
2618
Text string `json:"text"`
2719
}
2820

2921
// CreateMessageInput defines a structured list of input messages.
3022
type CreateMessageInput struct {
31-
// MaxTokens defines the maximum number of tokens to generate before
32-
// stopping. Token generation may stop before reaching this limit, this only
33-
// specifies the absolute maximum number of tokens to generate. Different
34-
// models have different maximum token limits.
35-
MaxTokens int `json:"max_tokens"`
36-
// Messages are the input messages, models are trained to operate on
37-
// alternating user and assistant conversational turns. When creating a new
38-
// message, prior conversational turns can be specified with this field.
39-
Messages []Message `json:"messages"`
40-
// Model defines the language model that will be used to complete the
41-
// prompt. See model.go for a list of available models.
42-
Model LanguageModel `json:"model"`
43-
// StopSequences defines custom text sequences that will cause the model to
44-
// stop generating.
45-
StopSequences []string `json:"stop_sequences,omitempty"`
46-
// System provides a means of specifying context and instructions to the
47-
// model, such as specifying a particular goal or role.
48-
System string `json:"system,omitempty"`
4923
// Temperature defines the amount of randomness injected into the response.
5024
// Note that even with a temperature of 0.0, results will not be fully
5125
// deterministic.
@@ -54,23 +28,45 @@ type CreateMessageInput struct {
5428
// Recommended for advanced use cases only. You usually only need to use
5529
// Temperature.
5630
TopK *int `json:"top_k,omitempty"`
57-
// TopP (nucleus-sampling) defines the cumulative probability of the highest probability.
31+
// TopP (nucleus-sampling) defines the cumulative probability of the highest
32+
// probability.
5833
// Recommended for advanced use cases only. You usually only need to use
5934
// Temperature.
6035
TopP *float64 `json:"top_p,omitempty"`
36+
// Model defines the language model that will be used to complete the
37+
// prompt. See model.go for a list of available models.
38+
Model LanguageModel `json:"model"`
39+
// System provides a means of specifying context and instructions to the
40+
// model, such as specifying a particular goal or role.
41+
System string `json:"system,omitempty"`
42+
// Messages are the input messages, models are trained to operate on
43+
// alternating user and assistant conversational turns. When creating a new
44+
// message, prior conversational turns can be specified with this field.
45+
Messages []Message `json:"messages"`
46+
// StopSequences defines custom text sequences that will cause the model to
47+
// stop generating.
48+
StopSequences []string `json:"stop_sequences,omitempty"`
49+
// MaxTokens defines the maximum number of tokens to generate before
50+
// stopping. Token generation may stop before reaching this limit, this only
51+
// specifies the absolute maximum number of tokens to generate. Different
52+
// models have different maximum token limits.
53+
MaxTokens int `json:"max_tokens"`
6154
}
6255

56+
// CreateMessageOutput defines the response from creating a new message.
6357
type CreateMessageOutput struct {
64-
Id *string `json:"id"`
65-
Type *string `json:"type"`
66-
Role *string `json:"role"`
67-
Model *string `json:"model"`
68-
StopSequence *string `json:"stop_sequence"`
69-
StopReason *string `json:"stop_reason"`
70-
Content []*Content `json:"content"`
71-
Usage *Usage `json:"usage"`
58+
ID *string `json:"id"`
59+
Type *string `json:"type"`
60+
Role *string `json:"role"`
61+
Model *string `json:"model"`
62+
StopSequence *string `json:"stop_sequence"`
63+
StopReason *string `json:"stop_reason"`
64+
Usage *Usage `json:"usage"`
65+
// Content is a list of generated messages.
66+
Content []*Content `json:"content"`
7267
}
7368

69+
// String implements the fmt.Stringer interface for CreateMessageOutput.
7470
func (c *CreateMessageOutput) String() string {
7571
return c.Content[0].Text
7672
}

transport.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ type Transport struct {
1111
APIKey string
1212
}
1313

14+
// Client returns an HTTP client that will include the API key in the request,
15+
// and is safe for concurrent use by multiple goroutines.
1416
func (t *Transport) Client() *http.Client {
1517
return &http.Client{
1618
Transport: t,
1719
}
1820
}
1921

2022
// RoundTrip implements the http.RoundTripper interface.
23+
// It makes a copy of the HTTP request so that it complies with the requirements
24+
// of the interface and adds the API key to the request before calling the
25+
// underlying default http.RoundTripper.
2126
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
2227
if t.APIKey == "" {
2328
return nil, errors.New("API key is required")

usage.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package anthropic
2+
3+
import "fmt"
4+
5+
// Usage defines billing and rate-limit usage information.
6+
// Billing and rate-limiting are driven by token counts, since tokens represent
7+
// the underlying cost to Anthropic.
8+
type Usage struct {
9+
InputTokens int `json:"input_tokens"`
10+
OutputTokens int `json:"output_tokens"`
11+
}
12+
13+
// String implements the fmt.Stringer interface for Usage.
14+
func (u *Usage) String() string {
15+
return fmt.Sprintf("Input: %d, Output: %d", u.InputTokens, u.OutputTokens)
16+
}

usage_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package anthropic
2+
3+
import "testing"
4+
5+
func TestUsage_String(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
usage *Usage
9+
want string
10+
}{
11+
{
12+
name: "empty",
13+
usage: &Usage{},
14+
want: "Input: 0, Output: 0",
15+
},
16+
{
17+
name: "input",
18+
usage: &Usage{InputTokens: 1},
19+
want: "Input: 1, Output: 0",
20+
},
21+
{
22+
name: "output",
23+
usage: &Usage{OutputTokens: 2},
24+
want: "Input: 0, Output: 2",
25+
},
26+
{
27+
name: "both",
28+
usage: &Usage{InputTokens: 3, OutputTokens: 4},
29+
want: "Input: 3, Output: 4",
30+
},
31+
}
32+
for _, tt := range tests {
33+
t.Run(tt.name, func(t *testing.T) {
34+
if got := tt.usage.String(); got != tt.want {
35+
t.Errorf("Usage.String() = %v, want %v", got, tt.want)
36+
}
37+
})
38+
}
39+
}

0 commit comments

Comments
 (0)