How do I process a CSV file with the Model API in Python?
import csv, os, time
from openai import OpenAI
client = OpenAI(base_url="https://api.gateway.clatterlabs.xyz/v1", api_key=os.environ["CLATTER_API_KEY"],
default_headers={"X-Clatter-Account": os.environ["CLATTER_ACCOUNT_ID"]}, max_retries=5)
with open("reviews.csv") as f, open("labelled.csv", "w", newline="") as out:
w = csv.writer(out)
w.writerow(["review", "label", "tokens"])
for row in csv.DictReader(f):
r = client.chat.completions.create(
model="supercosmos-flash",
messages=[{"role": "system", "content": "Label the review as positive, negative or mixed. One word."},
{"role": "user", "content": row["review"]}],
max_tokens=5, temperature=0,
)
w.writerow([row["review"], r.choices[0].message.content.strip(), r.usage.total_tokens])
time.sleep(0.5) # ~120 requests per minute
Size the delay to your key's limit, and estimate the total cost before you start so the run doesn't stop at 402 out_of_credits.
Good to know#
- Retry only
429and 5xx errors, with backoff; honourRetry-Afterwhen it's sent. Which errors to retry - Every response has an
X-Request-Id; log it so a request can be found later. Why log it - Every call shows in Console › Logs and Usage as Model API, with its price. See what you spent
Still need help?#
- General support: support@clatterlabs.com or team@clatterlabs.xyz
- Infrastructure questions, or permanent deletion of your data (your whole account or one service): infrastructure@clatterlabs.xyz
We usually reply within the first hour, and always within 24 hours. A full support team is with you.
Related articles
Still need help?
Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.
Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyzFor deleting your whole account or the data of one service.