#![cfg_attr(docsrs, feature(doc_cfg))]
use url::Url;
#[cfg(feature = "macros")]
pub use ollama_rs_macros::function;
#[cfg(feature = "macros")]
pub mod re_exports {
pub use schemars;
pub use serde;
}
pub mod coordinator;
pub mod error;
pub mod generation;
#[cfg_attr(docsrs, doc(cfg(feature = "headers")))]
#[cfg(feature = "headers")]
pub mod headers;
pub mod history;
pub mod models;
pub trait IntoUrl: IntoUrlSealed {}
impl IntoUrl for Url {}
impl IntoUrl for String {}
impl IntoUrl for &str {}
impl IntoUrl for &String {}
pub trait IntoUrlSealed {
fn into_url(self) -> Result<Url, url::ParseError>;
fn as_str(&self) -> &str;
}
impl IntoUrlSealed for Url {
fn into_url(self) -> Result<Url, url::ParseError> {
Ok(self)
}
fn as_str(&self) -> &str {
self.as_str()
}
}
impl IntoUrlSealed for &str {
fn into_url(self) -> Result<Url, url::ParseError> {
Url::parse(self)?.into_url()
}
fn as_str(&self) -> &str {
self
}
}
impl IntoUrlSealed for &String {
fn into_url(self) -> Result<Url, url::ParseError> {
(&**self).into_url()
}
fn as_str(&self) -> &str {
self.as_ref()
}
}
impl IntoUrlSealed for String {
fn into_url(self) -> Result<Url, url::ParseError> {
(&*self).into_url()
}
fn as_str(&self) -> &str {
self.as_ref()
}
}
#[derive(Debug, Clone)]
pub struct Ollama {
pub(crate) url: Url,
pub(crate) reqwest_client: reqwest::Client,
#[cfg(feature = "headers")]
pub(crate) request_headers: reqwest::header::HeaderMap,
}
impl Ollama {
pub fn builder() -> OllamaBuilder {
OllamaBuilder::new()
}
#[deprecated(
since = "0.3.5",
note = "use `Ollama::builder().host(host).port(port).build()` instead"
)]
pub fn new(host: impl IntoUrl, port: u16) -> Self {
let mut url: Url = host.into_url().unwrap();
url.set_port(Some(port)).unwrap();
Self::from_url(url)
}
#[deprecated(
since = "0.3.5",
note = "use `Ollama::builder().host(host).port(port).reqwest_client(client).build()` instead"
)]
pub fn new_with_client(host: impl IntoUrl, port: u16, reqwest_client: reqwest::Client) -> Self {
let mut url: Url = host.into_url().unwrap();
url.set_port(Some(port)).unwrap();
Self {
url,
reqwest_client,
#[cfg(feature = "headers")]
request_headers: reqwest::header::HeaderMap::new(),
}
}
#[inline]
pub fn try_new(url: impl IntoUrl) -> Result<Self, url::ParseError> {
Ok(Self::from_url(url.into_url()?))
}
#[inline]
pub fn from_url(url: Url) -> Self {
Self {
url,
..Default::default()
}
}
#[inline]
pub fn uri(&self) -> String {
self.url.host().unwrap().to_string()
}
pub fn url(&self) -> &Url {
&self.url
}
#[inline]
pub fn url_str(&self) -> &str {
self.url.as_str()
}
}
impl From<Url> for Ollama {
fn from(url: Url) -> Self {
Self::from_url(url)
}
}
impl Default for Ollama {
fn default() -> Self {
Self {
url: Url::parse("http://127.0.0.1:11434").unwrap(),
reqwest_client: reqwest::Client::new(),
#[cfg(feature = "headers")]
request_headers: reqwest::header::HeaderMap::new(),
}
}
}
#[derive(Debug, Clone)]
pub struct OllamaBuilder {
url: Url,
reqwest_client: Option<reqwest::Client>,
#[cfg(feature = "headers")]
request_headers: reqwest::header::HeaderMap,
}
impl OllamaBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn host(mut self, host: impl IntoUrl) -> Self {
self.url = host.into_url().unwrap();
self
}
pub fn port(mut self, port: u16) -> Self {
self.url.set_port(Some(port)).unwrap();
self
}
pub fn url(mut self, url: impl IntoUrl) -> Self {
self.url = url.into_url().unwrap();
self
}
pub fn reqwest_client(mut self, reqwest_client: reqwest::Client) -> Self {
self.reqwest_client = Some(reqwest_client);
self
}
#[cfg_attr(docsrs, doc(cfg(feature = "headers")))]
#[cfg(feature = "headers")]
pub fn request_headers(mut self, request_headers: reqwest::header::HeaderMap) -> Self {
self.request_headers = request_headers;
self
}
pub fn build(self) -> Ollama {
Ollama {
url: self.url,
reqwest_client: self.reqwest_client.unwrap_or_default(),
#[cfg(feature = "headers")]
request_headers: self.request_headers,
}
}
}
impl Default for OllamaBuilder {
fn default() -> Self {
Self {
url: Url::parse("http://127.0.0.1:11434").unwrap(),
reqwest_client: None,
#[cfg(feature = "headers")]
request_headers: reqwest::header::HeaderMap::new(),
}
}
}