Skip to main content

OpenAIProvider

Struct OpenAIProvider 

Source
pub struct OpenAIProvider { /* private fields */ }
Expand description

OpenAI-compatible LLM provider

§Examples

use tiny_loop::llm::OpenAIProvider;

let provider = OpenAIProvider::new()
    .api_key("sk-...")
    .model("gpt-4o");

Implementations§

Source§

impl OpenAIProvider

Source

pub fn new() -> Self

Create a new OpenAI provider with default settings

§Examples
use tiny_loop::llm::OpenAIProvider;

let provider = OpenAIProvider::new();
Source

pub fn base_url(self, value: impl Into<String>) -> Self

Set the base URL for the API endpoint (default: https://api.openai.com/v1)

§Examples
use tiny_loop::llm::OpenAIProvider;

let provider = OpenAIProvider::new()
    .base_url("https://api.custom.com/v1");
Source

pub fn api_key(self, value: impl Into<String>) -> Self

Set the API key for authentication (default: empty string)

§Examples
use tiny_loop::llm::OpenAIProvider;

let provider = OpenAIProvider::new()
    .api_key("sk-...");
Source

pub fn model(self, value: impl Into<String>) -> Self

Set the model name to use (default: gpt-4o)

§Examples
use tiny_loop::llm::OpenAIProvider;

let provider = OpenAIProvider::new()
    .model("gpt-4o-mini");
Source

pub fn header( self, key: impl Into<String>, value: impl Into<String>, ) -> Result<Self>

Add a custom HTTP header to requests

§Examples
use tiny_loop::llm::OpenAIProvider;

let provider = OpenAIProvider::new()
    .header("x-custom-header", "value")
    .unwrap();
§Errors

Returns an error if the header name or value contains invalid characters.

Source

pub fn max_retries(self, retries: u32) -> Self

Set maximum number of retries on failure (default: 3)

§Examples
use tiny_loop::llm::OpenAIProvider;

let provider = OpenAIProvider::new()
    .max_retries(5);
Source

pub fn retry_delay(self, delay_ms: u64) -> Self

Set delay between retries in milliseconds (default: 1000)

§Examples
use tiny_loop::llm::OpenAIProvider;

let provider = OpenAIProvider::new()
    .retry_delay(2000);
Source

pub fn body(self, body: Value) -> Result<Self>

Set custom body fields to merge into the request

§Examples
use tiny_loop::llm::OpenAIProvider;
use serde_json::json;

let provider = OpenAIProvider::new()
    .body(json!({
        "top_p": 0.9,
        "frequency_penalty": 0.5
    }))
    .unwrap();
§Errors

Returns an error if the value is not a JSON object

Source

pub fn stream_callback<F>(self, callback: F) -> Self
where F: FnMut(String) + Send + Sync + 'static,

Set stream callback for LLM responses

§Examples
use tiny_loop::llm::OpenAIProvider;

let provider = OpenAIProvider::new()
    .stream_callback(|chunk| print!("{}", chunk));

Trait Implementations§

Source§

impl Default for OpenAIProvider

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl LLMProvider for OpenAIProvider

Source§

fn call<'life0, 'life1, 'life2, 'async_trait>( &'life0 mut self, messages: &'life1 [Message], tools: &'life2 [ToolDefinition], ) -> Pin<Box<dyn Future<Output = Result<LLMResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Call the LLM with messages and available tools, returning the assistant’s response

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more