Web playgrounds הן נהדרות להדגמות. מדביקים בלוק של טקסט, צופים במודל מייצר סיכום מסודר, וסוגרים את הלשונית. אבל זו לא הנדסה. עבודה בסביבת ייצור (Production) משמעותה APIs, טיפול בשגיאות וקוד שרץ בזמן שאתם ישנים. אם אתם צריכים לעבד תמלולי פגישות, כרטיסי תמיכה או מאמרים מחקריים לפי לוח זמנים קבוע, אתם זקוקים ל-pipeline.
מדריך זה ילווה אתכם בבניית בדיוק זה: סקריפט קל משקל ואוטומטי לסיכום מסמכים באמצעות Python, ה-AWS SDK עבור Python (boto3), ו-Amazon Bedrock. נשתמש ב-Claude 3 Haiku של Anthropic, מודל שמגיע לנקודת האיזון המושלמת בין מהירות לעלות עבור משימות סיכום טקסט.
Why Bedrock and Claude 3 Haiku?
Amazon Bedrock הוא שירות מנוהל החושף מודלי יסוד (foundation models) באמצעות סט אחד של AWS APIs. במקום לחבר בין נקודות קצה (endpoints) חיצוניות ולהתמודד עם מודלים נפרדים של חיוב ואבטחה, אתם קוראים ל-AWS endpoint עם בקרות IAM סטנדרטיות. הנתונים שלכם נשארים בתוך סביבת ה-AWS שלכם.
Claude 3 Haiku הוא המודל הרזה ביותר במשפחת Claude 3 של Anthropic. הוא נבנה עבור תגובתיות ועלות נמוכה, מה שהופך אותו לאידיאלי לסיכום בנפחים גבוהים שבהם אתם רוצים פלט צפוי מבלי לשלם על כוח העיבוד של מודלים גדולים יותר במשימות קריאה פשוטות.
What You Need
לפני כתיבת קוד כלשהו, ודאו שהדברים הבאים מוכנים:
- חשבון AWS פעיל.
- Python 3.9 או גרסה גבוהה יותר מותקנת מקומית.
- AWS CLI מוגדר עם הרשאות שיש להן אישור להפעיל (invoke) את מודלי Bedrock. אם עדיין לא הרצתם
aws configure, עשו זאת כעת. אם תיתקלו בשגיאות הרשאה מאוחר יותר, סביר להניח שתצטרכו להצמיד את הרשאות ה-Bedrock invocation המתאימות למשתמש או לתפקיד ה-IAM שלכם. - גישה למודל (Model access) מופעלת ספציפית עבור Anthropic Claude 3 Haiku בתוך קונסולת AWS Bedrock. AWS דורשת מכם לבחור במפורש (opt in) עבור כל ספק מודל לפני שתוכלו לקרוא לו.
Step 1: Enable Model Access
Bedrock לא מאפשר לכם לקרוא למודלים באופן אוטומטי (out of the box). עליכם להפעיל את המתג בקונסולה תחילה.
- התחברו ל-AWS Management Console.
- השתמשו בשורת החיפוש כדי למצוא את Amazon Bedrock.
- בפאנל הניווט השמאלי, בחרו ב-Model access.
- לחצו על Modify model access.
- סמנו את התיבה עבור Anthropic (Claude 3 Haiku) ושלחו את הבקשה שלכם.
ברגע שהסטטוס משתנה ל-"Access granted", אתם יכולים לקרוא למודל מהקוד.
Step 2: Set Up Your Environment
סביבת Python נקייה שומרת על התלויות (dependencies) מבודדות וניתנות לשחזור. פתחו את הטרמינל והריצו את הפקודות הבאות:
mkdir bedrock-summarizer && cd bedrock-summarizer
python3 -m venv venv
source venv/bin/activate
pip install boto3
משתמשי Windows צריכים להחליף את פקודת ההפעלה (activation command) ב-venv\Scripts\activate. לאחר ש-pip install boto3 מסתיים, יש לכם את כל מה שצריך כדי לתקשר עם AWS APIs.
Step 3: Write the Script
צרו קובץ בשם summarize.py. המטרה היא לקרוא מסמך מהדיסק, להעביר אותו ל-Bedrock Converse API ולהדפיס סיכום תמציתי.
להלן מימוש מלא ועובד. אנחנו משתמשים ב-Converse API מכיוון שהוא מנטרל (abstracts away) את פורמט ה-JSON הגולמי שספקי מודלים שונים מצפים לו. אתם פשוט מעבירים רשימה של הודעות והגדרות הסקה (inference settings).
import boto3
def summarize_document(text: str) -> str:
client = boto3.client("bedrock-runtime")
model_id = "anthropic.claude-3-haiku-20240307-v1:0"
messages = [
{
"role": "user",
"content": [
{
"text": (
"Provide a concise summary of the following document. "
"Focus on the main points and avoid unnecessary detail:\n\n"
f"{text}"
)
}
]
}
]
response = client.converse(
modelId=model_id,
messages=messages,
inferenceConfig={
"temperature": 0.3,
"maxTokens": 512
}
)
summary = response["output"]["message"]["content"][0]["text"]
return summary.strip()
if __name__ == "__main__":
with open("document.txt", "r", encoding="utf-8") as f:
document_text = f.read()
result = summarize_document(document_text)
print("\n--- Summary ---\n")
print(result)
כמה פרטים מעשיים שכדאי לציין כאן:
- boto3.client("bedrock-runtime") מכוון ל-runtime endpoint שמטפל בהסקה (inference). ודאו שה-AWS region שלכם ב-
~/.aws/configתומך ב-Bedrock ושהפעלתם את Haiku באותו region. - Model ID
anthropic.claude-3-haiku-20240307-v1:0הוא המזהה המדויק ש-Bedrock מצפה לו. העתיקו אותו בדיוק. - Temperature מוגדרת ל-0.3 שומרת על הפלט מבוסס עובדות (grounded). עבור סיכום, אתם רוצים עקביות ונאמנות לטקסט המקור, לא קישוטים יצירתיים. אם תעלו את ה-temperature לכיוון 1.0, המודל יתחיל לקחת חופש פעולה בניסוחים ולפעמים להמציא פרטים.
- ה-prompt עצמו ספציפי. במקום לזרוק טקסט גולמי למודל עם "סכם את זה" מעורפל, אנחנו מבקשים במפורש נקודות עיקריות ומנחים אותו לדלג על טקסט מיותר. סוג כזה של בהירות מפריד בין פלט לא שמיש לבין משהו שבאמת אפשר להפיץ (ship).
הניחו כל קובץ טקסט שברצונכם לסכם באותה תיקייה וקראו לו document.txt.
Step 4: Run It
כאשר הסביבה הווירטואלית שלכם פעילה, הריצו:
python summarize.py
אם ההרשאות (credentials) והגישה למודל שלכם נכונים, אתם אמורים לראות סיכום מסודר מודפס בטרמינל תוך שניות בודדות. אם אתם מקבלים שגיאת גישה, בדקו שוב את הרשאות ה-IAM שלכם ואשרו שהפעלתם את Claude 3 Haiku בקונסולה.
Pushing Beyond the Script
This pipeline is intentionally simple, but it is the foundation for real automation. Here is how you can extend it without adding bloat.
Batch processing. Swap the single file read for a loop over a directory. Drop fifty PDFs or text files into an input folder, iterate through them, and write the summaries to an output folder. If you want to ingest PDFs directly, you will need a preprocessing step with a library like PyPDF2 or pdfplumber to extract raw text before it hits Bedrock.
Chunking strategy. Very long documents may exceed the model’s context limit. When that happens, split the text into logical chunks by paragraph or section, summarize each chunk individually, and then pass the intermediate summaries back through the model for a final synthesis. This two-stage approach keeps you under token limits while preserving coverage of the full document.
Error handling. Production code should catch boto3.exceptions.ClientError specifically. AWS may throttle your requests if you call the API too aggressively. Wrap your converse call in a retry loop with exponential backoff, or use a library like tenacity to handle rate limits gracefully.
Prompt engineering. The difference between a mediocre summary and a useful one often comes down to the prompt. Ask for bullet points if you need scanability. Ask for a one-paragraph executive summary if the audience is senior leadership. You can even pass formatting constraints, such as "Limit the summary to three sentences" or "Return the output as JSON with keys for topic, key_points, and action_items."
The Real Takeaway
Moving from a chat playground to a working script is the inflection point where AI becomes infrastructure. Once this pipeline runs locally, you can lift it into an AWS Lambda function triggered by S3 uploads, schedule it on ECS Fargate, or hook it into an existing data workflow. The API call is the easy part. The engineering value comes from wrapping that call in logic that handles files, errors, and formatting so you never have to copy and paste text into a browser again.
For additional context and variations on this setup, see the original walkthrough on Dev.to. If you want to discuss AWS architectures, LLM pipelines, or prompt engineering with a community of builders, join the conversation over at [GyaanSetu AI on Telegram](https://t.me/GyaanSet
