Class ChatCompletionsController

java.lang.Object
io.agentscope.spring.boot.chat.web.ChatCompletionsController

@RestController @RequestMapping public class ChatCompletionsController extends Object
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:

  1. Client sends complete conversation history in messages
  2. Server creates a fresh agent instance, loads the messages
  3. Agent processes and returns a response
  4. 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:

  1. Append the assistant message (with tool_calls) to history
  2. Execute the tools and create tool result messages
  3. 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

    Constructors
    Constructor
    Description
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    createCompletion(@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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 instances
      messageConverter - Converter for HTTP DTOs to framework messages
      responseBuilder - Builder for response objects
      streamingService - Service for streaming responses
      toolConverter - 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 without Accept: text/event-stream header for better client compatibility.

      Parameters:
      request - The chat completion request containing the full message history
      Returns:
      A Mono containing the ChatCompletionsResponse with the agent's reply, or a Flux of ServerSentEvent if 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 Flux of ServerSentEvent containing text deltas and completion events