テキスト補完を作成
Deprecated: Use the Messages API instead.
[レガシー] テキスト補完を作成します。
Text Completions APIはレガシーAPIです。今後はMessages APIの使用をお勧めします。
将来のモデルや機能はText Completionsと互換性がありません。Text CompletionsからMessagesへの移行については、移行ガイドをご覧ください。
Parameters
params: CompletionCreateParams { max_tokens_to_sample; model; prompt; /* 8 more */ }
max_tokens_to_sample: numberThe maximum number of tokens to generate before stopping.
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.
model: ModelThe model that will complete your prompt.
The model that will complete your prompt.
See models for additional details and options.
Fastest model for high-volume, real-time tasks
Efficient model for coding and agents
Frontier intelligence for ambitious tasks across coding, scientific discovery, and enterprise workflows
Powerful intelligence for coding, knowledge work, and long-running agents
Our most capable model for cybersecurity and biology research, available through trusted access programs
Efficient model for coding and agents
Next generation of intelligence for the hardest knowledge work and coding problems
Most capable model for cybersecurity and biology research
Powerful intelligence for long-running agents and coding
Powerful intelligence for long-running agents and coding
Powerful intelligence for long-running agents and coding
Powerful intelligence for long-running agents and coding
Best combination of speed and intelligence
Powerful intelligence for long-running agents and coding
Powerful intelligence for long-running agents and coding
"claude-mythos-preview"DeprecatedNew class of intelligence, strongest in coding and cybersecurity
New class of intelligence, strongest in coding and cybersecurity
"claude-sonnet-4-5"DeprecatedHigh-performance model for agents and coding
High-performance model for agents and coding
"claude-sonnet-4-5-20250929"DeprecatedHigh-performance model for agents and coding
High-performance model for agents and coding
prompt: stringThe prompt that you want Claude to complete.
The prompt that you want Claude to complete.
For proper response generation you will need to format your prompt using alternating `
Human:and
Assistant:` conversational turns. For example:
"
Human: {userQuestion}
Assistant:"
See prompt validation and our guide to prompt design for more details.
metadata?: Metadata { user_id }An object describing metadata about the request.
An object describing metadata about the request.
user_id?: string | nullAn external identifier for the user who is associated with the request.
An external identifier for the user who is associated with the request.
This should be a uuid, hash value, or other opaque identifier. Anthropic may use this id to help detect abuse. Do not include any identifying information such as name, email address, or phone number.
stop_sequences?: Array<string>Sequences that will cause the model to stop generating.
Sequences that will cause the model to stop generating.
Our models stop on `"
Human:"`, 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.
stream?: booleanWhether to incrementally stream the response using server-sent events.
Whether to incrementally stream the response using server-sent events.
See streaming for details.
workspace_id?: stringHeader parameterOptional header to select the Workspace for this request. The value is a Workspace ID (for example, wrkspc_011CZkZaBF1tNoB5wlCeusgy).
Optional header to select the Workspace for this request. The value is a Workspace ID (for example, wrkspc_011CZkZaBF1tNoB5wlCeusgy).
Only needed for credentials that can act on more than one Workspace. A credential that belongs to a specific Workspace may omit it; if sent, it must match that Workspace.
temperature?: numberDeprecatedAmount of randomness injected into the response.
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.
top_k?: numberDeprecatedOnly sample from the top K options for each subsequent token.
Only sample from the top K options for each subsequent token.
Used to remove "long tail" low probability responses. Learn more technical details here.
Recommended for advanced use cases only.
top_p?: numberDeprecatedUse nucleus sampling.
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.
Recommended for advanced use cases only.
betas?: Array<AnthropicBeta>Header parameterDeprecatedOptional header to specify the beta version(s) you want to use.
Optional header to specify the beta version(s) you want to use.
Returns
interface Completion { type: "completion"; id; completion; /* 2 more */ }
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: process.env["ANTHROPIC_API_KEY"] // This is the default and can be omitted
});
const completion = await client.completions.create({
max_tokens_to_sample: 256,
model: "claude-2.1",
prompt: "\n\nHuman: Hello, world!\n\nAssistant:"
});
console.log(completion.id);{
"id": "compl_018CKm6gsux7P8yMcwZbeCPw",
"completion": " Hello! My name is Claude.",
"model": "claude-2.1",
"stop_reason": "stop_sequence",
"type": "completion"
}Returns Examples
{
"id": "compl_018CKm6gsux7P8yMcwZbeCPw",
"completion": " Hello! My name is Claude.",
"model": "claude-2.1",
"stop_reason": "stop_sequence",
"type": "completion"
}