Created
October 2, 2025 02:20
-
-
Save ochilab/da1015d15496b684ab713c0ccc7d61aa to your computer and use it in GitHub Desktop.
Bedrockをpythonで呼ぶコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| REGION = os.getenv("AWS_REGION", "us-east-1") | |
| brt = boto3.client("bedrock-runtime", region_name=REGION) | |
| def call_claude(model_id: str, user_text: str, max_tokens=600, temperature=0): | |
| body = { | |
| "anthropic_version": "bedrock-2023-05-31", | |
| "max_tokens": max_tokens, | |
| "temperature": temperature, | |
| "messages": [{"role": "user", "content": [{"type":"text","text": user_text}]}] | |
| } | |
| resp = brt.invoke_model( | |
| modelId=model_id, | |
| contentType="application/json", | |
| accept="application/json", | |
| body=json.dumps(body) | |
| ) | |
| out = json.loads(resp["body"].read()) | |
| parts = out.get("content", []) | |
| text = "\n".join([p.get("text","") for p in parts if p.get("type")=="text"]).strip() | |
| return text | |
| # 評価モデル(Claude Sonnet 4 - 推論プロファイル経由) | |
| EVAL_MODEL_ID = "us.anthropic.claude-sonnet-4-20250514-v1:0" # 推論プロファイルID | |
| raw = call_claude(EVAL_MODEL_ID, prompt, max_tokens=400, temperature=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment