본문 바로가기
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

모델이 한 응답에 tool_calls 세 개를 반환했다면 서로 독립적으로 실행할 수 있다는 뜻이야. 하나씩 처리하면 대기 시간이 합쳐지므로 asyncio.gather 또는 sync 코드의 threading 으로 병렬 실행하고, 결과는 같은 다음 turn 에 함께 보내.

tool_calls 가 배열인 이유

여러 도구를 한 turn 에 호출하는 게 기본 지원되는 패턴이기 때문이야. weather, news, calendar 를 동시에 가져오면 사용자는 한 번만 기다리면 돼.

순서가 필요할 때는 병렬 호출을 꺼

parallel_tool_calls=False 는 turn 마다 tool 하나만 호출하게 해. 다음 단계가 앞선 결과에 의존하는 pipeline 에서는 느리더라도 순서를 명시하는 편이 맞아. 기본값은 병렬 호출이 가능한 True 야.

실제 시간을 비교해

도시 세 곳의 날씨를 가져오는 작업으로 병렬과 순차 실행 시간을 재봐. 병렬 실행은 대체로 가장 느린 호출 하나의 시간에 가깝고, 순차 실행은 세 호출의 시간을 더한 만큼 걸려.

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

모델에게 도시 세 곳의 날씨를 가져오게 하고 한 응답에 tool_calls 세 개가 오는지 확인해. asyncio.gather 로 실행한 시간과 순차 실행 시간을 비교해 속도 차이를 기록해.

Progress

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

댓글 0

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

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