pub struct OpenAiWorker { /* private fields */ }Expand description
OpenAI API worker (GPT-4, GPT-3.5-turbo-instruct, etc.)
Requires OPENAI_API_KEY environment variable.
§Example
let worker = Arc::new(
OpenAiWorker::new("gpt-3.5-turbo-instruct")?
.with_max_tokens(512)
.with_temperature(0.7)
);§Resilience
OpenAiWorker does not retry internally. Retry logic is handled by the
pipeline’s inference stage. See ModelWorker for details.
Implementations§
Source§impl OpenAiWorker
impl OpenAiWorker
Sourcepub fn new(model: impl Into<String>) -> Result<Self, OrchestratorError>
pub fn new(model: impl Into<String>) -> Result<Self, OrchestratorError>
Create a new OpenAiWorker for the given model.
Reads the API key from the OPENAI_API_KEY environment variable and
constructs an HTTP client configured for the OpenAI chat/completions
endpoint. Default settings: 256 max tokens, temperature 0.7, 30 s
timeout. All defaults are overridable via the builder methods.
§Environment Variables
| Variable | Required | Description |
|---|---|---|
OPENAI_API_KEY | Yes | Bearer token sent with every request |
§Errors
Returns OrchestratorError::ConfigError if OPENAI_API_KEY is not
set in the environment.
§Examples
use tokio_prompt_orchestrator::OpenAiWorker;
use std::sync::Arc;
// OPENAI_API_KEY must be set in the environment.
let worker = Arc::new(
OpenAiWorker::new("gpt-4o")
.expect("OPENAI_API_KEY must be set")
.with_max_tokens(512)
.with_temperature(0.7),
);Sourcepub fn with_max_tokens(self, max_tokens: u32) -> Self
pub fn with_max_tokens(self, max_tokens: u32) -> Self
Set maximum tokens to generate
Sourcepub fn with_temperature(self, temperature: f32) -> Self
pub fn with_temperature(self, temperature: f32) -> Self
Set temperature (0.0 – 2.0 for OpenAI).
Values outside [0.0, 2.0] are clamped and a WARN-level log line is
emitted. The OpenAI API rejects values outside this range with HTTP 400.
Sourcepub fn with_timeout(self, timeout: Duration) -> Self
pub fn with_timeout(self, timeout: Duration) -> Self
Set request timeout
Sourcepub fn with_base_url(self, url: impl Into<String>) -> Self
pub fn with_base_url(self, url: impl Into<String>) -> Self
Override the API base URL.
Useful for OpenAI-compatible endpoints (Azure OpenAI, Groq, local proxies)
and for pointing at a mock server in tests.
Default: "https://api.openai.com/v1".
Trait Implementations§
Source§impl Debug for OpenAiWorker
impl Debug for OpenAiWorker
Source§impl ModelWorker for OpenAiWorker
impl ModelWorker for OpenAiWorker
Source§fn infer<'life0, 'life1, 'async_trait>(
&'life0 self,
prompt: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Vec<String>, OrchestratorError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn infer<'life0, 'life1, 'async_trait>(
&'life0 self,
prompt: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Vec<String>, OrchestratorError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Source§fn infer_stream<'life0, 'life1, 'async_trait>(
&'life0 self,
prompt: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<TokenStream, OrchestratorError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn infer_stream<'life0, 'life1, 'async_trait>(
&'life0 self,
prompt: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<TokenStream, OrchestratorError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Auto Trait Implementations§
impl !RefUnwindSafe for OpenAiWorker
impl !UnwindSafe for OpenAiWorker
impl Freeze for OpenAiWorker
impl Send for OpenAiWorker
impl Sync for OpenAiWorker
impl Unpin for OpenAiWorker
impl UnsafeUnpin for OpenAiWorker
Blanket Implementations§
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request