Overview
Concrete tool for LLM generation via the OpenAI Chat Completions API, with cost tracking, streaming, and native tool-calling support.
Description
The OpenAILLM class wraps OpenAI's synchronous and asynchronous chat completion clients. It inherits from BaseLLM and is registered via the `@register_model` decorator with OpenAILLMConfig. It handles message formulation, completion parameter management, streaming responses, tool-call accumulation, and token-level cost tracking through LiteLLM's `cost_per_token` utility.
Usage
- Agent creation: Pass an `OpenAILLMConfig` to `CustomizeAgent(llm_config=...)` to power agent generation.
- Benchmark evaluation: Use as the LLM for `Evaluator(llm=...)` when evaluating workflows against benchmarks.
- Multi-agent debate: Serve as the default LLM config for `MultiAgentDebateActionGraph`.
- Direct generation: Call `generate()` / `async_generate()` for standalone LLM completions with structured output parsing.
Code Reference
Source Location
- Repository: EvoAgentX
- Config File: evoagentx/models/model_configs.py (L18-50)
- Model File: evoagentx/models/openai_model.py (L24-289)
Signature
class OpenAILLMConfig(LLMConfig):
llm_type: str = "OpenAILLM"
model: str
openai_key: Optional[str] = None
temperature: Optional[float] = None
max_tokens: Optional[int] = None
max_completion_tokens: Optional[int] = None
top_p: Optional[float] = None
n: Optional[int] = None
stream: Optional[bool] = None
stream_options: Optional[dict] = None
timeout: Optional[Union[float, int]] = None
tools: Optional[List] = None
tool_choice: Optional[Union[str, dict]] = None
parallel_tool_calls: Optional[bool] = None
reasoning_effort: Optional[str] = None
logprobs: Optional[bool] = None
top_logprobs: Optional[int] = None
response_format: Optional[Union[BaseModel, dict]] = None
output_response: bool = False
class OpenAILLM(BaseLLM):
def init_model(self) -> None
def supports_native_tool_calling(self) -> bool
def single_generate(self, messages: List[dict], **kwargs) -> str
def batch_generate(self, batch_messages: List[List[dict]], **kwargs) -> List[str]
async def single_generate_async(self, messages: List[dict], **kwargs) -> str
Import
from evoagentx.models.openai_model import OpenAILLM
from evoagentx.models.model_configs import OpenAILLMConfig
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| config |
OpenAILLMConfig |
Yes |
Config with model name, API key, and generation params
|
| messages |
List[dict] |
Yes (for generate) |
Chat messages in OpenAI format
|
| **kwargs |
dict |
No |
Per-call overrides for stream, output_response, etc.
|
Outputs
| Name |
Type |
Description
|
| single_generate returns |
str |
Raw or formatted LLM completion text
|
| batch_generate returns |
List[str] |
One completion per message batch
|
| cost |
Cost |
Tracked via cost_manager with input/output token counts and costs
|
Usage Examples
Basic Agent with OpenAI LLM
from evoagentx.models.openai_model import OpenAILLM
from evoagentx.models.model_configs import OpenAILLMConfig
from evoagentx.agents.customize_agent import CustomizeAgent
# 1. Configure the LLM
llm_config = OpenAILLMConfig(
model="gpt-4o-mini",
openai_key="sk-...",
temperature=0.7,
max_completion_tokens=1024
)
llm = OpenAILLM(config=llm_config)
# 2. Create an agent powered by this LLM
agent = CustomizeAgent(
name="Summarizer",
description="Summarizes text input",
prompt="Summarize the following text:\n{text}",
llm_config=llm_config,
inputs=[{"name": "text", "type": "str", "description": "Text to summarize"}],
outputs=[{"name": "summary", "type": "str", "description": "Summary"}],
parse_mode="title",
)
# 3. Execute the agent
result = agent(inputs={"text": "Long article content here..."})
print(result.content.get_structured_data())
Direct LLM Generation
from evoagentx.models.openai_model import OpenAILLM
from evoagentx.models.model_configs import OpenAILLMConfig
llm = OpenAILLM(config=OpenAILLMConfig(
model="gpt-4o-mini",
openai_key="sk-...",
))
# Generate with structured output parsing
response = llm.generate(
prompt="What is the capital of France?",
parse_mode="str",
)
print(response.content)
Related Pages
Implements Principle
Requires Environment