Created
August 9, 2025 14:40
-
-
Save maciejskorski/b7865f293842feb6dfa988a497033c15 to your computer and use it in GitHub Desktop.
async processing with Anthropic
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
import anthropic | |
from tqdm.asyncio import tqdm as tqdm_async | |
async def analyze_with_claude(text: str, temperature=0.7): | |
client=anthropic.AsyncAnthropic() | |
output = await client.messages.create( | |
model="claude-4-sonnet-20250514", | |
max_tokens=200, | |
temperature=temperature, | |
system=prompt, | |
messages=[{"role": "user", "content": f"Text: {text}"}] | |
) | |
return output.content[0].text.strip() | |
async def process(example): | |
idx = example['id'] | |
response = await analyze_with_claude(example['text']) | |
return (idx, response) | |
async def process_all(): | |
tasks = list(map(process, ds_test_sample)) | |
results = [] | |
for task in tqdm_async.as_completed(tasks): | |
result = await task | |
results.append(result) | |
return results | |
results = await process_all() | |
results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment