Class: Mana::Backends::OpenAI

Inherits:
Base
  • Object
show all
Defined in:
lib/mana/backends/openai.rb

Overview

OpenAI-compatible backend (GPT, Groq, DeepSeek, Ollama, etc.)

Translates between Mana's internal format (Anthropic-style) and OpenAI's:

- system prompt: top-level `system` → system message
- tool calls: `tool_use`/`tool_result` blocks → `tool_calls` + `role: "tool"`
- response: `choices` → content blocks

Constant Summary

Constants inherited from Base

Base::ANTHROPIC_PATTERN

Instance Method Summary collapse

Methods inherited from Base

for, #initialize

Constructor Details

This class inherits a constructor from Mana::Backends::Base

Instance Method Details

#chat(system:, messages:, tools:, model:, max_tokens: 4096) ⇒ Object

Translates to OpenAI format, posts, then normalizes back to Anthropic format. Uses max_completion_tokens (not max_tokens) per OpenAI's newer API convention.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mana/backends/openai.rb', line 14

def chat(system:, messages:, tools:, model:, max_tokens: 4096)
  uri = URI("#{@config.effective_base_url}/v1/chat/completions")
  parsed = http_post(uri, {
    model: model,
    max_completion_tokens: max_tokens,
    messages: convert_messages(system, messages),
    tools: convert_tools(tools)
  }, {
    "Authorization" => "Bearer #{@config.api_key}"
  })
  {
    content: normalize_response(parsed),
    usage: normalize_usage(parsed[:usage])
  }
end