도구#
LLM을 외부 도구와 연결하는 방법을 배웁니다.
소개#
tools 기능을 통해 모델이 외부 도구를 사용할 수 있도록 설정할 수 있습니다.
OpenAI의 Function calling API 와 유사하게, 매개변수가 있는 함수를 정의하고 모델이 동적으로 호출할 함수와 전달할 매개변수를 선택하도록 할 수 있습니다.
다음은 함수 호출의 일반적인 과정입니다:
당신이 함수, 해당 매개변수 및 설명을 상세히 명시한 쿼리를 제출합니다.
LLM은 기능을 실행할지 여부를 결정합니다. 실행하지 않기로 선택하면 일상적인 언어로 응답하며, 내부 이해에 기반한 해결책을 제시하거나 쿼리 및 도구 사용에 대한 추가 세부 사항을 질문합니다. 도구 사용을 결정할 때는 적합한 API와 JSON 형식의 사용 지침을 추천합니다.
다음으로, 애플리케이션에서 API 호출을 구현하고, 반환된 응답을 LLM으로 보내 결과를 분석한 후 다음 단계를 계속 실행합니다.
현재 tools 기능을 위한 전용 API 엔드포인트는 구현되어 있지 않습니다. 반드시 Chat API와 함께 사용해야 합니다.
지원되는 모델 목록#
Xinference는 다음 모델이 tools 기능을 사용하도록 지원합니다:
빠른 시작#
Chat API의 선택적 매개변수 ``tools``는 함수 사양을 제공하는 데 사용할 수 있습니다. 그 목적은 모델이 제공된 사양에 맞는 함수 매개변수를 생성할 수 있도록 하는 것입니다.
OpenAI 클라이언트 사용 예제#
import openai
client = openai.Client(
api_key="cannot be empty",
base_url="http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
response = client.chat.completions.create(
model="<MODEL_UID>",
messages=[{
"role": "user",
"content": "Call me an Uber ride type 'Plus' in Berkeley at zipcode 94704 in 10 minutes"
}],
tools=[
{
"type": "function",
"function": {
"name": "uber_ride",
"description": "Find suitable ride for customers given the location, "
"type of ride, and the amount of time the customer is "
"willing to wait as parameters",
"parameters": {
"type": "object",
"properties": {
"loc": {
"type": "int",
"description": "Location of the starting place of the Uber ride",
},
"type": {
"type": "string",
"enum": ["plus", "comfort", "black"],
"description": "Types of Uber ride user is ordering",
},
"time": {
"type": "int",
"description": "The amount of time in minutes the customer is willing to wait",
},
},
},
},
}
],
)
print(response.choices[0].message)
출력 결과:
{
"role": "assistant",
"content": null,
"tool_calls": [
"id": "call_ad2f383f-31c7-47d9-87b7-3abe928e629c",
"type": "function",
"function": {
"name": "uber_ride",
"arguments": "{\"loc\": 94704, \"type\": \"plus\", \"time\": 10}"
}
],
}
Anthropic 클라이언트 사용 예제#
import anthropic
import json
import uuid
client = anthropic.Anthropic(
api_key="cannot be empty",
base_url="http://localhost:9997"
)
response = client.messages.create(
model="qwen3",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "What's the weather like in Beijing?"
}
],
tools=[
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get weather information for a city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city name",
},
},
"required": ["city"]
},
},
}
],
tool_choice={"type": "auto"}
)
출력 결과:
{
"role": "assistant",
"content": null,
"tool_calls": [
"id": "call_26884d11-ff6b-48fb-ada7-734f3fd0dfcc",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"Beijing\"}"
}
],
}
참고
LLM이 도구 호출을 사용한 경우, 완료 이유는 ``tool_calls``입니다. 그렇지 않으면 기본 완료 이유가 됩니다.
참고
API 자체는 함수 호출을 수행하지 않습니다. 개발자는 모델 출력을 사용하여 함수 호출을 실행해야 합니다.
tools 기능에 대한 더 많은 예제는 자습서 노트북에서 확인할 수 있습니다.
함수 호출 과정을 보여주는 완전한 예제를 학습해 보세요.