AnthropicLLM¶
Anthropic LLM 实现,运行异步 API 客户端。
属性¶
-
model: 用于 LLM 的模型名称,例如 "claude-3-opus-20240229"、"claude-3-sonnet-20240229" 等。可用模型可以在这里查看:Anthropic: 模型概览。
-
api_key: 用于验证对 Anthropic API 请求的 API 密钥。如果未提供,将从
ANTHROPIC_API_KEY
环境变量中读取。 -
base_url: 用于 Anthropic API 的基本 URL。默认为
None
,这意味着内部将使用https://api.anthropic.com
。 -
timeout: 等待响应的最长秒数。默认为
600.0
。 -
max_retries: 在失败之前重试请求的最大次数。默认为
6
。 -
http_client: 如果提供,则使用备用 HTTP 客户端来调用 Anthropic API。默认为
None
。 -
structured_output: 包含使用
instructor
的结构化输出配置的字典。您可以在distilabel.steps.tasks.structured_outputs.instructor
中的InstructorStructuredOutputType
中查看字典结构。 -
_api_key_env_var: 用于 API 密钥的环境变量名称。它旨在供内部使用。
-
_aclient: 用于 Anthropic API 的
AsyncAnthropic
客户端。它旨在供内部使用。在load
方法中设置。
运行时参数¶
-
api_key: 用于验证对 Anthropic API 请求的 API 密钥。如果未提供,将从
ANTHROPIC_API_KEY
环境变量中读取。 -
base_url: 用于 Anthropic API 的基本 URL。默认为
"https://api.anthropic.com"
。 -
timeout: 等待响应的最长秒数。默认为
600.0
。 -
max_retries: 在失败之前重试请求的最大次数。默认为
6
。
示例¶
生成文本¶
from distilabel.models.llms import AnthropicLLM
llm = AnthropicLLM(model="claude-3-opus-20240229", api_key="api.key")
llm.load()
output = llm.generate_outputs(inputs=[[{"role": "user", "content": "Hello world!"}]])
生成结构化数据¶
from pydantic import BaseModel
from distilabel.models.llms import AnthropicLLM
class User(BaseModel):
name: str
last_name: str
id: int
llm = AnthropicLLM(
model="claude-3-opus-20240229",
api_key="api.key",
structured_output={"schema": User}
)
llm.load()
output = llm.generate_outputs(inputs=[[{"role": "user", "content": "Create a user profile for the following marathon"}]])