Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:2aronS Duel Agents DuelLLM

From Leeroopedia
Knowledge Sources
Domains Framework_Integration, NLP
Last Updated 2026-10-27 14:00 GMT

Overview

Concrete tool for routing LlamaIndex prompts through the Duel Agents proxy via a thin subclass of OpenAILike that injects Duel defaults (model, API base, API key) in __init__, provided by the llama-index-llms-duel Python package.

Description

The DuelLLM class is a LlamaIndex LLM that routes prompts through the Duel proxy. It subclasses llama_index.llms.openai_like.OpenAILike and only changes defaults — all upstream methods (complete, chat, stream_complete, astream) are inherited unchanged.

Default injection is performed in __init__, which resolves environment variables before forwarding to the superclass:

  • api_base → overridden by os.getenv("DUEL_PROXY_URL", api_base) if the env var is set
  • api_key → falls back to os.getenv("DUEL_API_KEY") if not passed
  • model → defaults to "duel-auto"
  • is_chat_model → defaults to True (required for chat-style usage)

The class_name class method returns "DuelLLM".

Usage

Import this class when you need to:

  • Use the Duel proxy inside a LlamaIndex pipeline or query engine
  • Drop in a Duel-routed LLM with one import change from OpenAILike
  • Run LlamaIndex completion, chat, or streaming through the Duel proxy

Code Reference

Source Location

  • Repository: Duel-Agents
  • File: python/llama-index-llms-duel/llama_index/llms/duel/base.py (lines 12-56)

Signature

from llama_index.llms.openai_like import OpenAILike

DEFAULT_API_BASE = "https://duelagents.com/v1"
DEFAULT_MODEL = "duel-auto"

class DuelLLM(OpenAILike):
    def __init__(
        self,
        model: str = DEFAULT_MODEL,
        api_base: str = DEFAULT_API_BASE,
        api_key: Optional[str] = None,
        is_chat_model: bool = True,
        **kwargs: Any,
    ) -> None:
        api_base = os.getenv("DUEL_PROXY_URL", api_base)
        api_key = api_key or os.getenv("DUEL_API_KEY")
        super().__init__(
            model=model,
            api_base=api_base,
            api_key=api_key,
            is_chat_model=is_chat_model,
            **kwargs,
        )

    @classmethod
    def class_name(cls) -> str:
        return "DuelLLM"

Import

from llama_index.llms.duel import DuelLLM

I/O Contract

Inputs

Name Type Required Description
model str No Model name (default duel-auto)
api_base str No Proxy URL (default DUEL_PROXY_URL env or https://duelagents.com/v1)
api_key Optional[str] No Duel key (default DUEL_API_KEY env)
is_chat_model bool No Default True
**kwargs Any No All OpenAILike args (temperature, max_tokens, etc.)

Outputs

Name Type Description
complete returns CompletionResponse Inherited from OpenAILike
chat returns ChatResponse Inherited from OpenAILike
stream_complete yields CompletionResponse chunks Inherited from OpenAILike

Usage Examples

Basic Completion

from llama_index.llms.duel import DuelLLM

llm = DuelLLM(model="duel-auto")
response = llm.complete("Explain concurrent agents in one sentence.")
print(response.text)

With Environment Variables

import os
os.environ["DUEL_API_KEY"] = "duel_<prefix>_<secret>"
# os.environ["DUEL_PROXY_URL"] = "https://duelagents.com/v1"  # optional

from llama_index.llms.duel import DuelLLM
# All defaults filled from env — no args needed
llm = DuelLLM()
response = llm.complete("What is 2+2?")

Chat Messages

from llama_index.llms.duel import DuelLLM
from llama_index.core.llms import ChatMessage

llm = DuelLLM(model="duel-auto")
messages = [
    ChatMessage(role="user", content="Write a haiku about agents."),
]
response = llm.chat(messages)
print(response.message.content)

Streaming

from llama_index.llms.duel import DuelLLM

llm = DuelLLM(model="duel-auto")
for chunk in llm.stream_complete("Write a short poem about routing."):
    print(chunk.text, end="", flush=True)

In a LlamaIndex Query Engine

from llama_index.llms.duel import DuelLLM
from llama_index.core import Settings

Settings.llm = DuelLLM(model="duel-auto", temperature=0)
# Now all LlamaIndex operations route through the Duel proxy

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment