도구를 강제로 쓰는 OpenAI 채팅 완료 클라이언트
도구를 강제로 쓰는 OpenAI 채팅 완료 클라이언트 (OpenAI Chat Completion Client With Tools Required)
tool_choice="required"로 모델이 항상 도구를 호출하도록 강제하는 채팅 완료 예제입니다. 여러 도구(현재 날씨, 예보)를 정의하고 스트리밍과 비스트리밍 방식으로 도구 호출 결과를 받아보는 방법을 보여줍니다.
출처: 문서
본문
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""To run this example, you can start the vLLM server
without any specific flags:
```bash
vllm serve unsloth/Llama-3.2-1B-Instruct \
--structured-outputs-config.backend outlines
This example demonstrates how to generate chat completions using the OpenAI Python client library. """
from openai import OpenAI
Modify OpenAI's API key and API base to use vLLM's API server.
openai_api_key = "EMPTY" openai_api_base = "http://localhost:8000/v1"
tools = [ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "city": { "type": "string", "description": "The city to find the weather for" ", e.g. 'San Francisco'", }, "state": { "type": "string", "description": ( "the two-letter abbreviation for the state that the " "city is in, e.g. 'CA' which would mean 'California'" ), }, "unit": { "type": "string", "description": "The unit to fetch the temperature in", "enum": ["celsius", "fahrenheit"], }, }, "required": ["city", "state", "unit"], }, }, }, { "type": "function", "function": { "name": "get_forecast", "description": "Get the weather forecast for a given location", "parameters": { "type": "object", "properties": { "city": { "type": "string", "description": ( "The city to get the forecast for, e.g. 'New York'" ), }, "state": { "type": "string", "description": ( "The two-letter abbreviation for the state, e.g. 'NY'" ), }, "days": { "type": "integer", "description": "Number of days to get the forecast for (1-7)", }, "unit": { "type": "string", "description": "The unit to fetch the temperature in", "enum": ["celsius", "fahrenheit"], }, }, "required": ["city", "state", "days", "unit"], }, }, }, ]
messages = [
{"role": "user", "content": "Hi! How are you doing today?"},
{"role": "assistant", "content": "I'm doing well! How can I help you?"},
{
"role": "user",
"content": "Can you tell me what the current weather is in Dallas
and the forecast for the next 5 days, in fahrenheit?",
},
]
def main(): client = OpenAI( # defaults to os.environ.get("OPENAI_API_KEY") api_key=openai_api_key, base_url=openai_api_base, )
models = client.models.list()
model = models.data[0].id
chat_completion = client.chat.completions.create(
messages=messages,
model=model,
tools=tools,
tool_choice="required",
stream=True, # Enable streaming response
)
for chunk in chat_completion:
if chunk.choices and chunk.choices[0].delta.tool_calls:
print(chunk.choices[0].delta.tool_calls)
chat_completion = client.chat.completions.create(
messages=messages, model=model, tools=tools, tool_choice="required"
)
print(chat_completion.choices[0].message.tool_calls)
if name == "main": main()
이 예제는 별도 플래그 없이 vLLM 서버를 시작한 뒤 실행할 수 있습니다. `tool_choice="required"` 때문에 모델은 어떤 도구든 반드시 호출합니다. 스트리밍과 비스트리밍 두 방식 모두에서 도구 호출을 확인합니다.
## 더 알아보기 (Learn more)
- [OpenAI Chat Completion Client With Tools](../tool_calling/openai_chat_completion_client_with_tools/) — 일반(비강제) 도구 호출 예제
- [원본 예제 파일](https://github.com/vllm-project/vllm/blob/main/examples/tool_calling/openai_chat_completion_client_with_tools_required.py)