뒤로
임세진
임세진 ·

OpenAI API 적절한 timeout, max_retries 수치 찾기

우선 테스트 프롬프트는 "간단하게 과일들을 주인공으로 해서 작문해볼래?"로 작성했어요.

총 100번 응답 시간을 측정해서 분포를 살펴봤구요(측정 횟수는 다다익선이겠죠), timeout을 변경해가면서 테스트해보면 각자 프롬프트에 맞는 timeout 수치를 찾아낼 수 있겠죠?

max_retries는 충분한 정도라고 생각되는 5로 고정했어요.

from openai import OpenAI
import time
from tqdm import tqdm
import matplotlib.pyplot as plt
import numpy as np

client = OpenAI(api_key="API-KEY")

tries = 100
req_times = []
for _ in tqdm(range(tries)):
    start = time.time()
    client.with_options(max_retries=5, timeout=40).chat.completions.create(
        messages=[
            {
                "role": "assistant",
                "content": "간단하게 과일들을 주인공으로 해서 작문해볼래?",
            }
        ],
        model="gpt-3.5-turbo-1106",
    )
    end = time.time()
    req_times.append(end-start)

hist, bins = np.histogram(req_times)
bins = (bins[1:] + bins[:-1]) / 2.0
hist = hist / np.sum(hist)

plt.bar(bins, hist, color="green", alpha=0.4, width=1.25)
plt.xlabel(r"$t\ [sec]$")
plt.ylabel(r"$P(t)$")
plt.savefig("histogram.png")
plt.show()
histogram (1).png

분포를 살펴보시면 요청 시간이 생각보다 짧다는 것을 알 수 있어요. timeout을 더 줄여서 측정한 뒤에 적절한 timeout을 찾는 것이 중요하다고 생각되네요.

10X AI Club 그룹의 글
휴튼

나도 몰랐던 나를 알아가는 질문들

4

댓글

로그인 후 댓글을 남길 수 있습니다.

아직 댓글이 없습니다.