Skip to content

OpenAI Adapter

OpenAI Adapter is implemented by OpenAIGenerator in the root package and adapts the OpenAI-compatible Chat Completions API to genx.Generator.

Convert boundaries

  • Convert prompts, messages, tools and model parameters of ModelContext to OpenAI request.
  • Convert streaming text, binary content, tool call and finish reason to MessageChunk and State.
  • Invoke It is preferred to use JSON Schema structured output, and function tool call can also be used.
  • Convert token usage to unified genx.Usage.

Core structure and main function

SymbolFunction
OpenAIGeneratorStores OpenAI client, model, generation parameters and capability flags, and implement Generator.
OpenAIGenerator.GenerateStreamInitiate streaming chat completion and continue writing to GenX Stream.
OpenAIGenerator.InvokeGenerate typed FuncCall arguments through structured output or tool call.
FormatOpenAISchemaNormalize generic JSON Schema to OpenAI structured-output schema.

OpenAI-compatible only means provider protocol compatibility; credentials, endpoint and product model selection are provided by the caller.