Skip to content

Commit b0cb6a4

Browse files
Adds examples to chat completion inference API (#4346) (#4348)
* Adds examples to chat completion inference API. * Fixes format. (cherry picked from commit b407c71) Co-authored-by: István Zoltán Szabó <szabosteve@gmail.com>
1 parent 68c914c commit b0cb6a4

File tree

6 files changed

+117
-0
lines changed

6 files changed

+117
-0
lines changed

specification/inference/_types/CommonTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import { float, integer, long } from '@_types/Numeric'
2525
export class RequestChatCompletion {
2626
/**
2727
* A list of objects representing the conversation.
28+
* Requests should generally only add new messages from the user (role `user`).
29+
* The other message roles (`assistant`, `system`, or `tool`) should generally only be copied from the response to a previous completion request, such that the messages array is built up throughout a conversation.
2830
*/
2931
messages: Array<Message>
3032
/**

specification/inference/chat_completion_unified/UnifiedRequest.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ import { Id } from '@_types/common'
2323
import { Duration } from '@_types/Time'
2424
/**
2525
* Perform chat completion inference
26+
*
27+
* The chat completion inference API enables real-time responses for chat completion tasks by delivering answers incrementally, reducing response times during computation.
28+
* It only works with the `chat_completion` task type for `openai` and `elastic` inference services.
29+
30+
* IMPORTANT: The inference APIs enable you to use certain services, such as built-in machine learning models (ELSER, E5), models uploaded through Eland, Cohere, OpenAI, Azure, Google AI Studio, Google Vertex AI, Anthropic, Watsonx.ai, or Hugging Face.
31+
* For built-in models and models uploaded through Eland, the inference APIs offer an alternative way to use and manage trained models. However, if you do not plan to use the inference APIs to use these models or if you want to use non-NLP models, use the machine learning trained model APIs.
32+
*
33+
* NOTE: The `chat_completion` task type is only available within the _stream API and only supports streaming.
34+
* The Chat completion inference API and the Stream inference API differ in their response structure and capabilities.
35+
* The Chat completion inference API provides more comprehensive customization options through more fields and function calling support.
36+
* If you use the `openai` service or the `elastic` service, use the Chat completion inference API.
2637
* @rest_spec_name inference.chat_completion_unified
2738
* @availability stack since=8.18.0 stability=stable visibility=public
2839
* @availability serverless stability=stable visibility=public
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
summary: A chat completion task
2+
description: Run `POST _inference/chat_completion/openai-completion/_stream` to perform a chat completion on the example question with streaming.
3+
# method_request: "POST _inference/chat_completion/openai-completion/_stream"
4+
# type: "request"
5+
value: |-
6+
{
7+
"model": "gpt-4o",
8+
"messages": [
9+
{
10+
"role": "user",
11+
"content": "What is Elastic?"
12+
}
13+
]
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
summary: A chat completion task with tool_calls
2+
description: Run `POST POST _inference/chat_completion/openai-completion/_stream` to perform a chat completion using an Assistant message with `tool_calls`.
3+
# method_request: "POST _inference/chat_completion/openai-completion/_stream"
4+
# type: "request"
5+
value: |-
6+
{
7+
"messages": [
8+
{
9+
"role": "assistant",
10+
"content": "Let's find out what the weather is",
11+
"tool_calls": [
12+
{
13+
"id": "call_KcAjWtAww20AihPHphUh46Gd",
14+
"type": "function",
15+
"function": {
16+
"name": "get_current_weather",
17+
"arguments": "{\"location\":\"Boston, MA\"}"
18+
}
19+
}
20+
]
21+
},
22+
{
23+
"role": "tool",
24+
"content": "The weather is cold",
25+
"tool_call_id": "call_KcAjWtAww20AihPHphUh46Gd"
26+
}
27+
]
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
summary: A chat completion task with tools and tool_calls
2+
description: Run `POST POST _inference/chat_completion/openai-completion/_stream` to perform a chat completion using a User message with `tools` and `tool_choice`.
3+
# method_request: "POST _inference/chat_completion/openai-completion/_stream"
4+
# type: "request"
5+
value: |-
6+
{
7+
"messages": [
8+
{
9+
"role": "user",
10+
"content": [
11+
{
12+
"type": "text",
13+
"text": "What's the price of a scarf?"
14+
}
15+
]
16+
}
17+
],
18+
"tools": [
19+
{
20+
"type": "function",
21+
"function": {
22+
"name": "get_current_price",
23+
"description": "Get the current price of a item",
24+
"parameters": {
25+
"type": "object",
26+
"properties": {
27+
"item": {
28+
"id": "123"
29+
}
30+
}
31+
}
32+
}
33+
}
34+
],
35+
"tool_choice": {
36+
"type": "function",
37+
"function": {
38+
"name": "get_current_price"
39+
}
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# summary:
2+
description: A successful response when performing a chat completion task using a User message with `tools` and `tool_choice`.
3+
# type: response
4+
# response_code: 200
5+
value: |-
6+
event: message
7+
data: {"chat_completion":{"id":"chatcmpl-Ae0TWsy2VPnSfBbv5UztnSdYUMFP3","choices":[{"delta":{"content":"","role":"assistant"},"index":0}],"model":"gpt-4o-2024-08-06","object":"chat.completion.chunk"}}
8+
9+
event: message
10+
data: {"chat_completion":{"id":"chatcmpl-Ae0TWsy2VPnSfBbv5UztnSdYUMFP3","choices":[{"delta":{"content":Elastic"},"index":0}],"model":"gpt-4o-2024-08-06","object":"chat.completion.chunk"}}
11+
12+
event: message
13+
data: {"chat_completion":{"id":"chatcmpl-Ae0TWsy2VPnSfBbv5UztnSdYUMFP3","choices":[{"delta":{"content":" is"},"index":0}],"model":"gpt-4o-2024-08-06","object":"chat.completion.chunk"}}
14+
15+
(...)
16+
17+
event: message
18+
data: {"chat_completion":{"id":"chatcmpl-Ae0TWsy2VPnSfBbv5UztnSdYUMFP3","choices":[],"model":"gpt-4o-2024-08-06","object":"chat.completion.chunk","usage":{"completion_tokens":28,"prompt_tokens":16,"total_tokens":44}}}
19+
20+
event: message
21+
data: [DONE]

0 commit comments

Comments
 (0)