参考数据:Claude API 参考 — Ruby
Data: Claude API reference — Ruby
v2.1.63Ruby SDK reference including installation, client initialization, basic requests, streaming, and beta tool runner
Claude API — Ruby
注意: Ruby SDK 支持 Claude API。工具运行器目前处于 beta 测试阶段,可通过
client.beta.messages.tool_runner()使用。Agent SDK 尚未支持 Ruby。
安装
gem install anthropic客户端初始化
require "anthropic"
# 默认方式(使用 ANTHROPIC_API_KEY 环境变量)
client = Anthropic::Client.new
# 显式指定 API 密钥
client = Anthropic::Client.new(api_key: "your-api-key")基础消息请求
message = client.messages.create(
model: :"{\{OPUS_ID}\}",
max_tokens: 1024,
messages: [
{ role: "user", content: "What is the capital of France?" }
]
)
puts message.content.first.text流式传输
stream = client.messages.stream(
model: :"{\{OPUS_ID}\}",
max_tokens: 1024,
messages: [{ role: "user", content: "Write a haiku" }]
)
stream.text.each { |text| print(text) }工具使用
Ruby SDK 支持通过原始 JSON 模式定义来使用工具,并提供了一个 beta 版本的工具运行器来自动执行工具。
工具运行器(Beta)
class GetWeatherInput < Anthropic::BaseModel
required :location, String, doc: "City and state, e.g. San Francisco, CA"
end
class GetWeather < Anthropic::BaseTool
doc "Get the current weather for a location"
input_schema GetWeatherInput
def call(input)
"The weather in #{input.location} is sunny and 72°F."
end
end
client.beta.messages.tool_runner(
model: :"{\{OPUS_ID}\}",
max_tokens: 1024,
tools: [GetWeather.new],
messages: [{ role: "user", content: "What's the weather in San Francisco?" }]
).each_message do |message|
puts message.content
end手动循环
有关工具定义格式和 agentic 循环模式,请参阅共享的工具使用概念。
英文原文 / English Original
Claude API — Ruby
Note: The Ruby SDK supports the Claude API. A tool runner is available in beta via
client.beta.messages.tool_runner(). Agent SDK is not yet available for Ruby.
Installation
gem install anthropicClient Initialization
require "anthropic"
# Default (uses ANTHROPIC_API_KEY env var)
client = Anthropic::Client.new
# Explicit API key
client = Anthropic::Client.new(api_key: "your-api-key")Basic Message Request
message = client.messages.create(
model: :"{\{OPUS_ID}\}",
max_tokens: 1024,
messages: [
{ role: "user", content: "What is the capital of France?" }
]
)
puts message.content.first.textStreaming
stream = client.messages.stream(
model: :"{\{OPUS_ID}\}",
max_tokens: 1024,
messages: [{ role: "user", content: "Write a haiku" }]
)
stream.text.each { |text| print(text) }Tool Use
The Ruby SDK supports tool use via raw JSON schema definitions and also provides a beta tool runner for automatic tool execution.
Tool Runner (Beta)
class GetWeatherInput < Anthropic::BaseModel
required :location, String, doc: "City and state, e.g. San Francisco, CA"
end
class GetWeather < Anthropic::BaseTool
doc "Get the current weather for a location"
input_schema GetWeatherInput
def call(input)
"The weather in #{input.location} is sunny and 72°F."
end
end
client.beta.messages.tool_runner(
model: :"{\{OPUS_ID}\}",
max_tokens: 1024,
tools: [GetWeather.new],
messages: [{ role: "user", content: "What's the weather in San Francisco?" }]
).each_message do |message|
puts message.content
endManual Loop
See the shared tool use concepts for the tool definition format and agentic loop pattern.