zach commited on
Commit
829d0b8
·
1 Parent(s): a807c4d

Refactor AnthropicConfig to encapsulate all API details including the sdk client

Browse files
Files changed (1) hide show
  1. src/integrations/anthropic_api.py +22 -4
src/integrations/anthropic_api.py CHANGED
@@ -33,7 +33,11 @@ from src.utils import truncate_text, validate_env_var
33
 
34
  @dataclass(frozen=True)
35
  class AnthropicConfig:
36
- """Immutable configuration for interacting with the Anthropic API."""
 
 
 
 
37
  model: ModelParam = "claude-3-5-sonnet-latest" # Valid predefined model
38
  max_tokens: int = 300 # Max tokens for API response
39
  system_prompt: str = """You are a highly creative and articulate assistant specialized in generating vivid, engaging, and well-written content.
@@ -54,6 +58,22 @@ When writing, tailor your tone and style to match the user's request. For exampl
54
 
55
  Always keep your responses concise, unless explicitly instructed to elaborate."""
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  class AnthropicError(Exception):
58
  """Custom exception for errors related to the Anthropic API."""
59
  def __init__(self, message: str, original_exception: Exception = None):
@@ -62,8 +82,6 @@ class AnthropicError(Exception):
62
 
63
 
64
  # Initialize the Anthropic client
65
- api_key: str = validate_env_var("ANTHROPIC_API_KEY")
66
- client: Anthropic = Anthropic(api_key=api_key)
67
  anthropic_config = AnthropicConfig()
68
 
69
 
@@ -95,7 +113,7 @@ def generate_text_with_claude(prompt: str) -> str:
95
  logger.debug(f"Preparing API request with prompt: {prompt[:50]}{'...' if len(prompt) > 50 else ''}")
96
 
97
  try:
98
- response: Message = client.messages.create(
99
  model=anthropic_config.model,
100
  max_tokens=anthropic_config.max_tokens,
101
  system=anthropic_config.system_prompt,
 
33
 
34
  @dataclass(frozen=True)
35
  class AnthropicConfig:
36
+ """
37
+ Immutable configuration for interacting with the Anthropic API.
38
+ Includes client initialization for encapsulation.
39
+ """
40
+ api_key: str = validate_env_var("ANTHROPIC_API_KEY")
41
  model: ModelParam = "claude-3-5-sonnet-latest" # Valid predefined model
42
  max_tokens: int = 300 # Max tokens for API response
43
  system_prompt: str = """You are a highly creative and articulate assistant specialized in generating vivid, engaging, and well-written content.
 
58
 
59
  Always keep your responses concise, unless explicitly instructed to elaborate."""
60
 
61
+ def __post_init__(self):
62
+ # Ensure the API key is set
63
+ if not self.api_key:
64
+ raise ValueError("Anthropic API key is not set.")
65
+
66
+ @property
67
+ def client(self) -> Anthropic:
68
+ """
69
+ Lazy initialization of the Anthropic client.
70
+
71
+ Returns:
72
+ Anthropic: Configured client instance.
73
+ """
74
+ return Anthropic(api_key=self.api_key)
75
+
76
+
77
  class AnthropicError(Exception):
78
  """Custom exception for errors related to the Anthropic API."""
79
  def __init__(self, message: str, original_exception: Exception = None):
 
82
 
83
 
84
  # Initialize the Anthropic client
 
 
85
  anthropic_config = AnthropicConfig()
86
 
87
 
 
113
  logger.debug(f"Preparing API request with prompt: {prompt[:50]}{'...' if len(prompt) > 50 else ''}")
114
 
115
  try:
116
+ response: Message = anthropic_config.client.messages.create(
117
  model=anthropic_config.model,
118
  max_tokens=anthropic_config.max_tokens,
119
  system=anthropic_config.system_prompt,