Anthropic API

MD
S
Markdown

Anthropic API guide. Access large language model capabilities for text generation, analysis, and task completion in applications via API calls.

1import anthropic
2import base64
3import os # Import os module for environment variables (recommended)
4
5api_key = "sk--"
6if not api_key:
7    # Fallback to asking for input if env var is not set (for easier testing)
8    # In production, you might want to raise an error instead.
9    api_key = input("Please enter your Anthropic API Key: ")
10    if not api_key:
11      raise ValueError("ANTHROPIC_API_KEY environment variable not set, and no key provided.")
12
13
14# Initialize the client with the API key
15client = anthropic.Anthropic(api_key=api_key)
16
17# --- Image Processing ---
18# Convert the image to Base64
19image_path = "passport_specimen.png"
20try:
21    with open(image_path, "rb") as image_file:
22        base64_image_data = base64.b64encode(image_file.read()).decode("utf-8")
23except FileNotFoundError:
24    print(f"Error: Image file not found at {image_path}")
25    exit() # Exit if the file isn't found
26
27# --- API Request ---
28# Determine the correct media type based on the file extension
29image_extension = os.path.splitext(image_path)[1].lower()
30if image_extension == ".png":
31    media_type = "image/png"
32elif image_extension in [".jpg", ".jpeg"]:
33    media_type = "image/jpeg"
34elif image_extension == ".gif":
35    media_type = "image/gif"
36elif image_extension == ".webp":
37     media_type = "image/webp"
38else:
39    print(f"Warning: Unsupported image format '{image_extension}'. Defaulting to image/png.")
40    media_type = "image/png"
41
42# --- *** MODIFIED PROMPT *** ---
43# Provide context to guide the AI and bypass overly cautious refusals
44revised_prompt = (
45    "This image contains a SPECIMEN passport, clearly marked as such and not belonging to a real person. "
46    "It is being used solely for testing the ability to convert document image layouts into HTML structure. "
47    "Please analyze the layout, text fields, graphical elements (like the photo area), and overall structure. "
48    "Generate an HTML representation that mimics this structure and layout. Use placeholder text (like 'First Name', 'Surname', 'Passport No.', etc.) "
49    "instead of any specific names or numbers shown in the specimen. The goal is to replicate the *form* and *layout* in HTML, not the specific data."
50)
51# --- *** END MODIFIED PROMPT *** ---
52
53
54# Prepare the API request
55try:
56    response = client.messages.create(
57        model="claude-3-sonnet-20240229", # Using a standard Sonnet model name
58        max_tokens=2048, # Increased max_tokens as HTML can be verbose
59            system=revised_prompt,
60        messages=[
61            {
62                "role": "user",
63                "content": [
64                    {
65                        "type": "image",
66                        "source": {
67                            "type": "base64",
68                            "media_type": media_type, # Use the determined media type
69                            "data": base64_image_data,
70                        },
71                    }
72                ],
73            }
74        ],
75    )
76
77    # Print the content of the response
78    if response.content and isinstance(response.content, list):
79      # Assuming the HTML is in the first text block
80      html_output = response.content[0].text
81      print(html_output)
82
83      # Optional: Save the HTML to a file
84      # with open("passport_output.html", "w", encoding="utf-8") as f:
85      #    f.write(html_output)
86      # print("\nHTML output saved to passport_output.html")
87
88    else:
89      # Fallback in case the structure is different or content is empty
90      print(response)
91
92
93except anthropic.APIConnectionError as e:
94    print(f"The server could not be reached: {e.__cause__}")
95except anthropic.RateLimitError as e:
96    print(f"A 429 status code was received; we should back off a bit: {e}")
97except anthropic.APIStatusError as e:
98    print(f"Another non-200-range status code was received: {e.status_code}")
99    print(e.response)
100except Exception as e:
101     print(f"An unexpected error occurred: {e}")

Created on 4/11/2025