Implementation:24mlight A Share investment Agent get chat completion
| Knowledge Sources | |
|---|---|
| Domains | LLM_Infrastructure, API_Design |
| Last Updated | 2026-10-10 14:00 GMT |
Overview
Concrete tool for unified LLM chat completion with multi-provider support, retry logic, and OpenAI-format message compatibility provided by the A_Share_investment_Agent repository.
Description
The get_chat_completion function is the single gateway for all LLM calls in the system. It delegates to LLMClientFactory.create_client() which auto-detects the appropriate LLM provider (Gemini or OpenAI-compatible) based on environment variables and parameters. The function accepts messages in OpenAI format ([{"role": "system", "content": "..."}, {"role": "user", "content": "..."}]) and returns a string response or None on failure.
This function is used by multiple agents across all workflows: debate room, sentiment, macro analyst, macro news, portfolio manager, and news query builder.
Usage
Import this function when you need to:
- Make LLM completion calls from any agent or tool
- Support multiple LLM backends without changing call sites
- Enable retry logic for flaky LLM API endpoints
This is a Wrapper Doc — it wraps external LLM APIs (Google Gemini, OpenAI-compatible) with repo-specific auto-detection and retry logic.
Code Reference
Source Location
- Repository: A_Share_investment_Agent
- File: src/tools/openrouter_config.py
- Lines: L94-128
Signature
def get_chat_completion(
messages: list,
model: str | None = None,
max_retries: int = 3,
initial_retry_delay: int = 1,
client_type: str = "auto",
api_key: str | None = None,
base_url: str | None = None,
) -> str | None:
"""
Args:
messages: Message list in OpenAI format
model: Model name (optional, uses env default)
max_retries: Maximum retry attempts
initial_retry_delay: Initial retry delay in seconds
client_type: Client type ("auto", "gemini", "openai_compatible")
api_key: API key (for OpenAI-compatible only)
base_url: API base URL (for OpenAI-compatible only)
Returns:
str: Model response content or None on error
"""
Import
from src.tools.openrouter_config import get_chat_completion
External Reference
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| messages | list[dict] | Yes | OpenAI-format messages [{"role": str, "content": str}] |
| model | str | No | Model name (defaults to GEMINI_MODEL env var) |
| max_retries | int | No | Max retries (default: 3) |
| initial_retry_delay | int | No | Initial retry delay seconds (default: 1) |
| client_type | str | No | "auto", "gemini", or "openai_compatible" (default: "auto") |
| api_key | str | No | API key for OpenAI-compatible providers |
| base_url | str | No | Base URL for OpenAI-compatible providers |
Outputs
| Name | Type | Description |
|---|---|---|
| return | None | LLM response text, or None on error |
Usage Examples
Basic LLM Call
from src.tools.openrouter_config import get_chat_completion
messages = [
{"role": "system", "content": "You are a financial analyst."},
{"role": "user", "content": "Analyze stock 002848."},
]
result = get_chat_completion(messages)
if result:
print(result)
With LLM Interaction Logging (used by agents)
from src.tools.openrouter_config import get_chat_completion
from src.utils.api_utils import log_llm_interaction
# Wrap with logging decorator for traceability
logged_call = log_llm_interaction(state)(get_chat_completion)
result = logged_call(messages)
With Custom Model and Client Type
result = get_chat_completion(
messages,
model="gpt-4",
client_type="openai_compatible",
api_key="sk-...",
base_url="https://api.openai.com/v1"
)