C.W.K.
Stream
Lesson 05 of 07 · published

Parallel Function Calling — 한 turn 에 여러 호출

~22 min · parallel-calls, concurrency

Level 0Tokenizer
0 XP0/54 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

모델이 한 응답에 3 개 tool_calls 돌려준다 = '이 셋은 독립적이라 병렬 가능' 의 신호. Sequential 실행은 wall time 의 2/3 낭비. asyncio.gather (또는 sync 면 threading) 으로 병렬 실행, 같은 followup turn 에 모든 결과 함께 반환.

왜 plural 인가

tool_calls 가 array 인 이유 — 여러 tool 동시 호출이 first-class 패턴. 한 turn 에 weather, news, calendar 동시 fetch — 사용자 한테는 한 번의 wait.

Ordering 필요할 때

parallel_tool_calls=False = turn 당 1 tool. Step N 이 step N-1 결과에 의존하는 pipeline 에 적합. 느려도 명시적. Default 는 True (parallel).

Speedup 측정

3 cities weather fetch 같은 작업으로 parallel vs sequential wall time 측정. Gather 가 거의 max(t1,t2,t3) 에 끝, sequential 은 sum. Speedup factor 가 직관에 박혀.

Code

한 turn 의 여러 tool_calls 처리·python
import asyncio

# The model may call both get_weather and search_news in one turn
# Execute them in parallel for speed:

async def execute_tools(tool_calls):
    tasks = []
    for tc in tool_calls:
        func = TOOLS_MAP[tc.name]
        args = json.loads(tc.arguments)
        if asyncio.iscoroutinefunction(func):
            tasks.append(asyncio.wait_for(func(**args), timeout=30.0))
        else:
            loop = asyncio.get_event_loop()
            tasks.append(loop.run_in_executor(None, lambda f=func, a=args: f(**a)))
    return await asyncio.gather(*tasks, return_exceptions=True)

# To disable parallel calling (Chat Completions only):
completion = client.chat.completions.create(
    model="gpt-5.4",
    tools=tools,
    messages=messages,
    parallel_tool_calls=False,  # enforce serial, one tool at a time
)

External links

Exercise

3 개 city weather fetch 모델한테 시켜. 한 응답에 3 tool_calls 받는 거 검증. asyncio.gather 로 실행. Parallel vs sequential wall time 비교 + speedup 기록.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.