Implementation:AI Shell Team Aish stream simple
| Knowledge Sources | |
|---|---|
| Domains | LLM, API_Integration |
| Last Updated | 2026-10-29 00:00 GMT |
Overview
Concrete tool for dispatching LLM chat completion requests to the correct provider-specific HTTP adapter provided by the aish-llm crate.
Description
The stream_simple function is the single entry point for sending chat completion requests to LLM providers. It takes a resolved ApiDialect, a StreamContext (API base, key, model), the conversation messages, optional tool specifications, and streaming parameters. It dispatches to the appropriate adapter:
- OpenAiCompletions — standard OpenAI-compatible `/v1/chat/completions`
- OpenAiResponses — OpenAI `/v1/responses` (used by Codex with API key)
- AnthropicMessages — Anthropic-native messages API
- OpenAiChatgptResponses — ChatGPT backend API (Codex with OAuth)
The resolve_api_dialect function determines the dialect by inspecting the model name and API base URL. Each adapter handles its provider's specific JSON format, SSE streaming, and authentication.
Usage
Call resolve_api_dialect once when creating a session to determine the dialect, then call stream_simple for every LLM request. The dialect is stable for a given model+endpoint pair.
Code Reference
Source Location
- Repository: aish
- File: crates/aish-llm/src/api/mod.rs
Signature
pub async fn stream_simple(
dialect: ApiDialect,
ctx: &StreamContext,
messages: &[ChatMessage],
tools: Option<&[ToolSpec]>,
stream: bool,
temperature: Option<f64>,
max_tokens: Option<u32>,
) -> Result<LlmResponse, AishError>
pub fn resolve_api_dialect(model: &str, api_base: &str, api_key: &str) -> ApiDialect
pub enum ApiDialect {
OpenAiCompletions,
OpenAiResponses,
AnthropicMessages,
OpenAiChatgptResponses,
}
pub struct StreamContext {
pub api_base: String,
pub api_key: String,
pub config_model: String,
pub model: String,
pub codex_auth_path: Option<PathBuf>,
pub close_connection: bool,
}
Import
use crate::api::{stream_simple, resolve_api_dialect, ApiDialect, StreamContext};
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| dialect | ApiDialect | Yes | Resolved provider dialect |
| ctx | &StreamContext | Yes | API base, key, model credentials |
| messages | &[ChatMessage] | Yes | Conversation history |
| tools | Option<&[ToolSpec]> | No | Available tools for function calling |
| stream | bool | Yes | SSE streaming flag |
| temperature | Option<f64> | No | Sampling temperature |
| max_tokens | Option<u32> | No | Max response tokens |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Result<LlmResponse, AishError> | Response with assistant text, tool calls, and token usage |
Usage Examples
Basic LLM Call
use crate::api::{stream_simple, resolve_api_dialect, StreamContext};
let ctx = StreamContext::new("https://api.openai.com/v1", "sk-...", "gpt-4", None);
let dialect = resolve_api_dialect("gpt-4", &ctx.api_base, &ctx.api_key);
let response = stream_simple(
dialect,
&ctx,
&messages,
Some(&tool_specs),
true, // SSE streaming
Some(0.7),
Some(4096),
).await?;