anthropic[patch]: standardize init args (#20161)

Related to #20085
This commit is contained in:
Bagatur
2024-04-08 12:09:06 -05:00
committed by GitHub
parent 3490d70238
commit a27d88f12a
7 changed files with 25 additions and 15 deletions

View File

@@ -243,12 +243,13 @@ class ChatAnthropic(BaseChatModel):
top_p: Optional[float] = None
"""Total probability mass of tokens to consider at each step."""
default_request_timeout: Optional[float] = None
default_request_timeout: Optional[float] = Field(None, alias="timeout")
"""Timeout for requests to Anthropic Completion API. Default is 600 seconds."""
anthropic_api_url: str = "https://api.anthropic.com"
anthropic_api_key: Optional[SecretStr] = None
anthropic_api_key: Optional[SecretStr] = Field(None, alias="api_key")
"""Automatically read from env var `ANTHROPIC_API_KEY` if not provided."""
default_headers: Optional[Mapping[str, str]] = None
"""Headers to pass to the Anthropic clients, will be used for every API call."""

View File

@@ -1,16 +1,16 @@
"""Test chat model integration."""
import os
from typing import Any, Callable, Dict, Literal, Type
from typing import Any, Callable, Dict, Literal, Type, cast
import pytest
from anthropic.types import ContentBlock, Message, Usage
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, ToolMessage
from langchain_core.outputs import ChatGeneration, ChatResult
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr
from langchain_core.tools import BaseTool
from langchain_anthropic import ChatAnthropic, ChatAnthropicMessages
from langchain_anthropic import ChatAnthropic
from langchain_anthropic.chat_models import _merge_messages, convert_to_anthropic_tool
os.environ["ANTHROPIC_API_KEY"] = "foo"
@@ -18,8 +18,17 @@ os.environ["ANTHROPIC_API_KEY"] = "foo"
def test_initialization() -> None:
"""Test chat model initialization."""
ChatAnthropicMessages(model_name="claude-instant-1.2", anthropic_api_key="xyz")
ChatAnthropicMessages(model="claude-instant-1.2", anthropic_api_key="xyz")
for model in [
ChatAnthropic(model_name="claude-instant-1.2", api_key="xyz", timeout=2),
ChatAnthropic(
model="claude-instant-1.2",
anthropic_api_key="xyz",
default_request_timeout=2,
),
]:
assert model.model == "claude-instant-1.2"
assert cast(SecretStr, model.anthropic_api_key).get_secret_value() == "xyz"
assert model.default_request_timeout == 2.0
@pytest.mark.requires("anthropic")