Available in Classic and VPC
Calculate number of tokens in sentences entered in the HCX model via Chat Completions V3.
Request
This section describes the request format. The method and URI are as follows:
Method | URI |
---|---|
POST | /v3/api-tools/chat-tokenize/{modelName} |
Request headers
The following describes the request headers.
Field | Required | Description |
---|---|---|
Authorization |
Required | API key for authentication Example: Bearer nv-************ |
X-NCP-CLOVASTUDIO-REQUEST-ID |
Optional | Request ID |
Content-Type |
Required | Request data format
|
Request path parameters
You can use the following path parameters with your request:
Field | Type | Required | Description |
---|---|---|---|
modelName |
String | Required | Model name
|
Request body
You can include the following data in the body of your request:
Field | Type | Required | Description |
---|---|---|---|
messages |
Array | Required | List of conversation messages to count tokens: ChatMessage |
tools |
Array | Optional | List of tools available for Function Calling : tools |
toolChoice |
String | Object | Optional | Function Calling tool call behavior
|
toolChoice.type |
String | Optional | Tool type to be called by the Function Calling model |
toolChoice.function |
Object | Optional | Tool to be called by the Function Calling model
|
toolChoice.function.name |
String | Optional | Tool name to be called by the Function Calling model |
ChatMessage
The following describes ChatMessage
.
Field | Type | Required | Description |
---|---|---|---|
role |
Enum | Required | Role of conversation messages
|
content |
String | Array | Required | Conversation message content
|
toolCalls |
Array | Conditional | Assistant call tool information
|
toolCallId |
String | Conditional | Tool ID
|
content
The following describes content
.
Field | Type | Required | Description |
---|---|---|---|
type |
Enum | Required | Format of conversation message content
|
text |
String | Conditional | Conversation message content
|
imageUrl |
Object | Conditional | Image list
|
imageUrl.url |
String | Conditional | Public URL of a single image, including file extension
|
dataUri |
Object | Conditional | Image list
|
dataUri.data |
String | Conditional | Base64-encoded image string
|
tools
The following describes tools
.
Field | Type | Required | Description |
---|---|---|---|
type |
String | Required | Tool type
|
function |
Object | Required | Call function information |
function.name |
String | Required | function name |
function.description |
String | Required | function description |
function.parameters |
Object | Required | Parameter passed when using function
|
toolCalls
The following describes toolCalls
.
Field | Type | Required | Description |
---|---|---|---|
id |
String | - | Tool identifier |
type |
String | - | Tool type
|
function |
Object | - | Call function information |
function.name |
String | - | function name |
function.arguments |
Object | - | Parameter passed when using function |
Request example
The request example is as follows:
When querying with image
The following is a sample request when querying by image.
curl --location --request POST 'https://clovastudio.stream.ntruss.com/v3/api-tools/chat-tokenize/{modelName}' \
--header 'Authorization: Bearer {API Key}' \
--header 'X-NCP-CLOVASTUDIO-REQUEST-ID: {Request ID}' \
--header 'Content-Type: application/json' \
--data '{
"messages": [
{
"messages": [
{
"role": "system",
"content": "- This is a friendly AI assistant."
},
{
"role": "user",
"content": [
{
"type": "image_url",
"imageUrl": [
{
"url": "https://www.******.com/image_a1b1c1.png"
}
]
},
{
"type": "text",
"text": "Please describe this photo."
}
]
},
{
"role": "assistant",
"content": "The photo shows a young child feeding a sheep."
}
]
}'
When querying with text
The following is a sample request when querying by text.
curl --location --request POST 'https://clovastudio.stream.ntruss.com/v3/api-tools/chat-tokenize/{modelName}' \
--header 'Authorization: Bearer {API Key}' \
--header 'X-NCP-CLOVASTUDIO-REQUEST-ID: {Request ID}' \
--header 'Content-Type: application/json' \
--data '{
"messages":[
{
"content": "What will the weather be like in Seoul tomorrow?",
"role":"user"
}
],
"tools":[
{
"type":"function",
"function":{
"description": "Tool that can provide weather information",
"name":"weather",
"parameters":{
"properties":{
"location":{
"description": "City names such as Seoul, Daejeon, and Busan",
"type":"string"
},
"unit":{
"enum":[
"celsius",
"fahrenheit"
],
"type":"string"
},
"date":{
"description": "Date string in the format such as 2023-08-01. Date you want to know the weather for",
"type":"string"
}
},
"required":[
"location"
],
"type":"object"
}
}
},
{
"type":"function",
"function":{
"description": "Tool that recommends travel destinations",
"name":"travel",
"parameters":{
"properties":{
"location":{
"description":"The city and state, e.g. San Francisco, CA",
"type":"string"
},
"date":{
"description": "Date string in the format such as \"2023-08-01~2023-09-01\". Travel date range",
"type":"string"
}
},
"required":[
"location"
],
"type":"object"
}
}
}
],
"toolChoice":"auto"
}'
Response
This section describes the response format.
Response body
The response body includes the following data:
Field | Type | Required | Description |
---|---|---|---|
messages |
Array | - | List of request conversation messages with counted tokens: ChatMessage |
ChatMessages.role |
Enum | - | Role of conversation messages
|
ChatMessage.content |
Array | - | Message content token calculation result: ChatMessageCount |
tools |
Array | - | List of request conversation messages with counted tokens: ToolCount |
ChatMessageCount
The following describes ChatMessageCount
.
Field | Type | Required | Description |
---|---|---|---|
type |
Enum | Required | Format of conversation message content
|
text |
String | Conditional | Conversation message content
|
imageUrl |
Object | Conditional | Conversation message content
|
imageUrl.url |
String | Conditional | Public URL of a single image, including file extension
|
dataUri |
Object | Conditional | Conversation message content
|
dataUri.data |
String | Conditional | Base64-encoded image string
|
count |
Integer | Required | Token count calculation results by conversation message content |
ToolCount
The following describes ToolCount
.
Field | Type | Required | Description |
---|---|---|---|
count |
Integer | Required | Tool list token count calculation result |
Response example
The response example is as follows:
Succeeded
The following is a sample response upon a successful call.
{
"status": {
"code": "20000",
"message": "OK"
},
"result": {
"messages": [
{
"role": "system",
"content": {
"type": "text",
"text": "- This is a friendly AI assistant.",
"count": 16
}
},
{
"role": "user",
"content": [
{
"type": "image_url",
"imageUrl": {
"url": "https://www.******.com/image_a1b1c1.png"
},
"count": 1478
},
{
"type": "text",
"text": "Please describe this photo.",
"count": 12
}
]
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "The photo shows a young child feeding a sheep.",
"count": 20
}
]
}
]
}
}
{
"status": {
"code": "20000",
"message": "OK"
},
"result": {
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What will the weather be like in Seoul tomorrow?",
"count": 12
}
]
}
],
"tools": {
"count": 230
}
}
}
Failure
The following is a sample response upon a failed call.