pub struct OpenAiChatModel { /* private fields */ }tool-loop only.Expand description
A ToolModel that sends each turn as POST {base}/chat/completions.
use a2a_protocol_server::tool_loop::OpenAiChatModel;
// llama.cpp's llama-server, with Qwen3's thinking turned off.
let mut extra = serde_json::Map::new();
extra.insert("chat_template_kwargs".into(), serde_json::json!({"enable_thinking": false}));
let model = OpenAiChatModel::new("http://127.0.0.1:8080/v1", "qwen3:1.7b")?
.with_extra_body(extra);Tool-call arguments are read whether the server sends them as a JSON
string, as OpenAI does, or as an object, as some compatible servers do.
Implementations§
Source§impl OpenAiChatModel
impl OpenAiChatModel
Sourcepub fn new(
base_url: &str,
model: impl Into<String>,
) -> Result<OpenAiChatModel, ModelError>
pub fn new( base_url: &str, model: impl Into<String>, ) -> Result<OpenAiChatModel, ModelError>
A client for model at base_url, the API’s base including its
version segment (https://api.openai.com/v1,
http://127.0.0.1:8080/v1).
§Errors
ModelError::Config when base_url is not an http:// or
https:// URL, or is https:// in a build without tls-rustls —
refused here rather than at the first turn.
Sourcepub fn with_tls_config(self, tls_config: ClientConfig) -> OpenAiChatModel
Available on crate feature tls-rustls only.
pub fn with_tls_config(self, tls_config: ClientConfig) -> OpenAiChatModel
tls-rustls only.Verifies the server against tls_config instead of Mozilla’s roots:
a private CA, or a client certificate for mutual TLS.
Sourcepub fn with_api_key(self, key: impl Into<String>) -> OpenAiChatModel
pub fn with_api_key(self, key: impl Into<String>) -> OpenAiChatModel
Sends key as Authorization: Bearer {key}. Local servers need
none.
Sourcepub const fn with_timeout(self, timeout: Duration) -> OpenAiChatModel
pub const fn with_timeout(self, timeout: Duration) -> OpenAiChatModel
Bounds each turn — connecting, sending and reading the whole
response. Default DEFAULT_TIMEOUT.
Sourcepub const fn with_max_response_bytes(self, max: usize) -> OpenAiChatModel
pub const fn with_max_response_bytes(self, max: usize) -> OpenAiChatModel
Caps the response body read, in bytes. Default
DEFAULT_MAX_RESPONSE_BYTES.
Sourcepub const fn with_stream_connection_reuse(self) -> OpenAiChatModel
pub const fn with_stream_connection_reuse(self) -> OpenAiChatModel
Lets a streamed turn’s connection be reused for the next turn.
By default each streamed turn asks for its connection to be closed
after it (Connection: close): llama.cpp’s llama-server says
keep-alive and then closes the connection just after a streamed
answer (seen on llama.cpp 9daa0aa, 2026-11-13: three of three
attempts to reuse the connection found it closed), and a pooled
connection would carry the next turn into that close (“connection
closed before message completed”). An endpoint that keeps its
connections (a hosted provider, a proxy) gains a handshake per turn
from this, which over TLS and a remote network is not free: call
this for such an endpoint (adversarial review N16, 2026-11-13). The
cost was not measured here: the only endpoint at hand was a local one.
Sourcepub fn with_extra_body(self, fields: Map<String, Value>) -> OpenAiChatModel
pub fn with_extra_body(self, fields: Map<String, Value>) -> OpenAiChatModel
Adds fields to every request body: temperature,
chat_template_kwargs, tool_choice and the like. The fields this
client writes itself — model, messages, and tools when there
are tools — win over an extra of the same name.
Trait Implementations§
Source§impl Debug for OpenAiChatModel
impl Debug for OpenAiChatModel
Source§impl ToolModel for OpenAiChatModel
impl ToolModel for OpenAiChatModel
Source§fn provider_name(&self) -> &'static str
fn provider_name(&self) -> &'static str
openai: the conventions name the API a client speaks, not the
server behind it (“multiple systems … are accessible by OpenAI
client libraries. In such cases, gen_ai.provider.name is set to
openai”).
Source§fn server(&self) -> Option<(&str, u16)>
fn server(&self) -> Option<(&str, u16)>
The endpoint’s host, without an IPv6 address’s brackets, and its port, or the scheme’s when it names none.
Source§fn complete_streaming<'a>(
&'a self,
request: ChatRequest<'a>,
text: &'a TextSink,
) -> Pin<Box<dyn Future<Output = Result<ModelTurn, ModelError>> + Send + 'a>>
fn complete_streaming<'a>( &'a self, request: ChatRequest<'a>, text: &'a TextSink, ) -> Pin<Box<dyn Future<Output = Result<ModelTurn, ModelError>> + Send + 'a>>
Asks for the turn as a stream ("stream": true, with its usage), and
hands text each piece of the answer as it arrives. Each streamed
turn has a connection of its own (Connection: close) unless
with_stream_connection_reuse
was called.
Source§fn model_name(&self) -> &str
fn model_name(&self) -> &str
gen_ai.request.model and in the
name of each turn’s span (chat {model}).Auto Trait Implementations§
impl !Freeze for OpenAiChatModel
impl !RefUnwindSafe for OpenAiChatModel
impl !UnwindSafe for OpenAiChatModel
impl Forget for OpenAiChatModel
impl Move for OpenAiChatModel
impl Send for OpenAiChatModel
impl Sync for OpenAiChatModel
impl Unpin for OpenAiChatModel
impl UnsafeUnpin for OpenAiChatModel
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].