-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathanthropic_api.py
More file actions
61 lines (49 loc) · 2.01 KB
/
anthropic_api.py
File metadata and controls
61 lines (49 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import json
from anthropic import Anthropic
from dotenv import load_dotenv
load_dotenv()
api_key = os.getenv("ANTHROPIC_API_KEY")
if not api_key:
raise ValueError("ANTHROPIC_API_KEY not found in environment variables. Please set it in your .env file.")
try:
anthropic = Anthropic(api_key=api_key)
except Exception as e:
raise ValueError(f"Failed to initialize Anthropic client: {e}")
def analyze_with_anthropic(page_data):
system_prompt = """You are an expert analyst. Your task is to review structured JSON data from a webpage.
Summarize the strengths and weaknesses of this page in terms of SEO, accessibility, and semantic HTML structure.
Provide specific, actionable suggestions for improvements.
Structure your response clearly, using Markdown for headings (e.g., ## Strengths, ## Weaknesses, ## Suggestions)."""
user_message_content = f"""
Here is a structured JSON of a webpage:
{json.dumps(page_data, indent=2)}
Please analyze it based on the instructions provided.
"""
try:
response = anthropic.messages.create(
model="claude-3-7-sonnet-20250219",
max_tokens=1500,
temperature=0.5,
system=system_prompt,
messages=[
{
"role": "user",
"content": user_message_content
}
]
)
if response.content and len(response.content) > 0:
return response.content[0].text.strip()
else:
return "No content returned from API."
except Exception as e:
error_message = f"Anthropic API error: {e}"
print(error_message)
if hasattr(e, 'response') and hasattr(e.response, 'json'):
try:
error_details = e.response.json()
error_message += f" | Details: {json.dumps(error_details)}"
except json.JSONDecodeError:
error_message += f" | Details: (Could not decode JSON error response from API)"
raise Exception(error_message)