-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathcompletion.go
More file actions
194 lines (183 loc) · 7.86 KB
/
completion.go
File metadata and controls
194 lines (183 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package anthropic
import (
"context"
"fmt"
"net/http"
"slices"
"github.com/anthropics/anthropic-sdk-go/internal/apijson"
"github.com/anthropics/anthropic-sdk-go/internal/requestconfig"
"github.com/anthropics/anthropic-sdk-go/option"
"github.com/anthropics/anthropic-sdk-go/packages/param"
"github.com/anthropics/anthropic-sdk-go/packages/respjson"
"github.com/anthropics/anthropic-sdk-go/packages/ssestream"
"github.com/anthropics/anthropic-sdk-go/shared/constant"
)
// CompletionService contains methods and other services that help with interacting
// with the anthropic API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewCompletionService] method instead.
type CompletionService struct {
Options []option.RequestOption
}
// NewCompletionService generates a new service that applies the given options to
// each request. These options are applied after the parent client's options (if
// there is one), and before any request-specific options.
func NewCompletionService(opts ...option.RequestOption) (r CompletionService) {
r = CompletionService{}
r.Options = opts
return
}
// [Legacy] Create a Text Completion.
//
// The Text Completions API is a legacy API. We recommend using the
// [Messages API](https://docs.claude.com/en/api/messages) going forward.
//
// Future models and features will not be compatible with Text Completions. See our
// [migration guide](https://docs.claude.com/en/api/migrating-from-text-completions-to-messages)
// for guidance in migrating from Text Completions to Messages.
//
// Note: If you choose to set a timeout for this request, we recommend 10 minutes.
func (r *CompletionService) New(ctx context.Context, params CompletionNewParams, opts ...option.RequestOption) (res *Completion, err error) {
for _, v := range params.Betas {
opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
}
opts = slices.Concat(r.Options, opts)
path := "v1/complete"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
return
}
// [Legacy] Create a Text Completion.
//
// The Text Completions API is a legacy API. We recommend using the
// [Messages API](https://docs.claude.com/en/api/messages) going forward.
//
// Future models and features will not be compatible with Text Completions. See our
// [migration guide](https://docs.claude.com/en/api/migrating-from-text-completions-to-messages)
// for guidance in migrating from Text Completions to Messages.
//
// Note: If you choose to set a timeout for this request, we recommend 10 minutes.
func (r *CompletionService) NewStreaming(ctx context.Context, params CompletionNewParams, opts ...option.RequestOption) (stream *ssestream.Stream[Completion]) {
var (
raw *http.Response
err error
)
for _, v := range params.Betas {
opts = append(opts, option.WithHeaderAdd("anthropic-beta", fmt.Sprintf("%v", v)))
}
opts = slices.Concat(r.Options, opts)
opts = append(opts, option.WithJSONSet("stream", true))
path := "v1/complete"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &raw, opts...)
return ssestream.NewStream[Completion](ssestream.NewDecoder(raw), err)
}
type Completion struct {
// Unique object identifier.
//
// The format and length of IDs may change over time.
ID string `json:"id,required"`
// The resulting completion up to and excluding the stop sequences.
Completion string `json:"completion,required"`
// The model that will complete your prompt.\n\nSee
// [models](https://docs.anthropic.com/en/docs/models-overview) for additional
// details and options.
Model Model `json:"model,required"`
// The reason that we stopped.
//
// This may be one the following values:
//
// - `"stop_sequence"`: we reached a stop sequence — either provided by you via the
// `stop_sequences` parameter, or a stop sequence built into the model
// - `"max_tokens"`: we exceeded `max_tokens_to_sample` or the model's maximum
StopReason string `json:"stop_reason,required"`
// Object type.
//
// For Text Completions, this is always `"completion"`.
Type constant.Completion `json:"type,required"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
Completion respjson.Field
Model respjson.Field
StopReason respjson.Field
Type respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r Completion) RawJSON() string { return r.JSON.raw }
func (r *Completion) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type CompletionNewParams struct {
// The maximum number of tokens to generate before stopping.
//
// Note that our models may stop _before_ reaching this maximum. This parameter
// only specifies the absolute maximum number of tokens to generate.
MaxTokensToSample int64 `json:"max_tokens_to_sample,required"`
// The model that will complete your prompt.\n\nSee
// [models](https://docs.anthropic.com/en/docs/models-overview) for additional
// details and options.
Model Model `json:"model,omitzero,required"`
// The prompt that you want Claude to complete.
//
// For proper response generation you will need to format your prompt using
// alternating `\n\nHuman:` and `\n\nAssistant:` conversational turns. For example:
//
// ```
// "\n\nHuman: {userQuestion}\n\nAssistant:"
// ```
//
// See [prompt validation](https://docs.claude.com/en/api/prompt-validation) and
// our guide to [prompt design](https://docs.claude.com/en/docs/intro-to-prompting)
// for more details.
Prompt string `json:"prompt,required"`
// Amount of randomness injected into the response.
//
// Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0`
// for analytical / multiple choice, and closer to `1.0` for creative and
// generative tasks.
//
// Note that even with `temperature` of `0.0`, the results will not be fully
// deterministic.
Temperature param.Opt[float64] `json:"temperature,omitzero"`
// Only sample from the top K options for each subsequent token.
//
// Used to remove "long tail" low probability responses.
// [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277).
//
// Recommended for advanced use cases only. You usually only need to use
// `temperature`.
TopK param.Opt[int64] `json:"top_k,omitzero"`
// Use nucleus sampling.
//
// In nucleus sampling, we compute the cumulative distribution over all the options
// for each subsequent token in decreasing probability order and cut it off once it
// reaches a particular probability specified by `top_p`. You should either alter
// `temperature` or `top_p`, but not both.
//
// Recommended for advanced use cases only. You usually only need to use
// `temperature`.
TopP param.Opt[float64] `json:"top_p,omitzero"`
// An object describing metadata about the request.
Metadata MetadataParam `json:"metadata,omitzero"`
// Sequences that will cause the model to stop generating.
//
// Our models stop on `"\n\nHuman:"`, and may include additional built-in stop
// sequences in the future. By providing the stop_sequences parameter, you may
// include additional strings that will cause the model to stop generating.
StopSequences []string `json:"stop_sequences,omitzero"`
// Optional header to specify the beta version(s) you want to use.
Betas []AnthropicBeta `header:"anthropic-beta,omitzero" json:"-"`
paramObj
}
func (r CompletionNewParams) MarshalJSON() (data []byte, err error) {
type shadow CompletionNewParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *CompletionNewParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}