Class ChatCompletionsController
java.lang.Object
io.agentscope.spring.boot.chat.web.ChatCompletionsController
HTTP controller exposing a Chat Completions API compatible with OpenAI's standard.
This controller implements a 100% stateless API, fully compatible with OpenAI's Chat Completions API. Each request is independent, and the client is responsible for managing conversation history.
How It Works:
- Client sends complete conversation history in
messages - Server creates a fresh agent instance, loads the messages
- Agent processes and returns a response
- Client appends the assistant message to their history for next request
Tool Calls:
When the assistant uses tools, the response includes tool_calls in the assistant
message. The client should:
- Append the assistant message (with tool_calls) to history
- Execute the tools and create tool result messages
- Send the updated history in the next request
Features:
- Non-streaming JSON response (default)
- SSE streaming when Accept: text/event-stream
- Full OpenAI compatibility
-
Constructor Summary
ConstructorsConstructorDescriptionChatCompletionsController(org.springframework.beans.factory.ObjectProvider<io.agentscope.core.ReActAgent> agentProvider, io.agentscope.core.chat.completions.converter.ChatMessageConverter messageConverter, io.agentscope.core.chat.completions.builder.ChatCompletionsResponseBuilder responseBuilder, ChatCompletionsStreamingService streamingService, io.agentscope.core.chat.completions.converter.OpenAIToolConverter toolConverter) Constructs a new ChatCompletionsController. -
Method Summary
Modifier and TypeMethodDescriptioncreateCompletion(@Valid io.agentscope.core.chat.completions.model.ChatCompletionsRequest request) Non-streaming chat completion endpoint.reactor.core.publisher.Flux<org.springframework.http.codec.ServerSentEvent<String>>createCompletionStream(@Valid io.agentscope.core.chat.completions.model.ChatCompletionsRequest request) Streaming chat completion endpoint.
-
Constructor Details
-
ChatCompletionsController
public ChatCompletionsController(org.springframework.beans.factory.ObjectProvider<io.agentscope.core.ReActAgent> agentProvider, io.agentscope.core.chat.completions.converter.ChatMessageConverter messageConverter, io.agentscope.core.chat.completions.builder.ChatCompletionsResponseBuilder responseBuilder, ChatCompletionsStreamingService streamingService, io.agentscope.core.chat.completions.converter.OpenAIToolConverter toolConverter) Constructs a new ChatCompletionsController.- Parameters:
agentProvider- Provider for creating prototype-scoped agent instancesmessageConverter- Converter for HTTP DTOs to framework messagesresponseBuilder- Builder for response objectsstreamingService- Service for streaming responsestoolConverter- Converter for OpenAI tools to ToolSchema
-
-
Method Details
-
createCompletion
@PostMapping(value="${agentscope.chat-completions.base-path:/v1/chat/completions}", consumes="application/json") public Object createCompletion(@Valid @RequestBody @Valid io.agentscope.core.chat.completions.model.ChatCompletionsRequest request) Non-streaming chat completion endpoint.Processes the complete message history and returns an assistant response.
If the request has
stream=true, automatically switches to streaming mode even withoutAccept: text/event-streamheader for better client compatibility.- Parameters:
request- The chat completion request containing the full message history- Returns:
- A
Monocontaining theChatCompletionsResponsewith the agent's reply, or aFluxofServerSentEventif streaming is requested
-
createCompletionStream
@PostMapping(value="${agentscope.chat-completions.base-path:/v1/chat/completions}", consumes="application/json", produces="text/event-stream") public reactor.core.publisher.Flux<org.springframework.http.codec.ServerSentEvent<String>> createCompletionStream(@Valid @RequestBody @Valid io.agentscope.core.chat.completions.model.ChatCompletionsRequest request) Streaming chat completion endpoint.Processes the complete message history and streams the response as Server-Sent Events.
- Parameters:
request- The chat completion request containing the full message history- Returns:
- A
FluxofServerSentEventcontaining text deltas and completion events
-