Practical Tips for ChatGPT API Integration
Recently, I helped several clients integrate ChatGPT and summarized some practical experiences to share with you.
1. Control Token Usage
API calls are billed by tokens, so controlling usage is important:
# Limit response length
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=500 # Limit max tokens
)
2. Set Reasonable Timeout
API sometimes responds slowly, it's recommended to set a timeout:
import openai
openai.api_request_timeout = 30 # 30 seconds timeout
3. Handle Errors Properly
Network issues, rate limiting, etc. may cause call failures:
try:
response = openai.ChatCompletion.create(...)
except openai.error.RateLimitError:
# Rate limit triggered, retry later
time.sleep(60)
except openai.error.APIError:
# API error, log it
logging.error("API call failed")
4. Use Streaming for Better UX
Long waiting times lead to poor experience. Use streaming:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
stream=True # Enable streaming
)
for chunk in response:
print(chunk.choices[0].delta.content, end="")
5. Content Filtering
Make sure to do content moderation before going live to avoid inappropriate content.
These are some basic experiences. For more details, feel free to contact us.