walrus_model/remote/openai/
mod.rs1use compact_str::CompactString;
6use reqwest::{Client, header::HeaderMap};
7
8mod provider;
9mod request;
10
11pub mod endpoint {
13 pub const OPENAI: &str = "https://api.openai.com/v1/chat/completions";
15 pub const OLLAMA: &str = "http://localhost:11434/v1/chat/completions";
17}
18
19#[derive(Clone)]
21pub struct OpenAI {
22 pub client: Client,
24 headers: HeaderMap,
26 endpoint: String,
28 model: CompactString,
30}
31
32impl OpenAI {
33 pub fn custom(client: Client, key: &str, endpoint: &str, model: &str) -> anyhow::Result<Self> {
35 use reqwest::header;
36 let mut headers = HeaderMap::new();
37 headers.insert(header::CONTENT_TYPE, "application/json".parse()?);
38 headers.insert(header::ACCEPT, "application/json".parse()?);
39 headers.insert(header::AUTHORIZATION, format!("Bearer {key}").parse()?);
40 Ok(Self {
41 client,
42 headers,
43 endpoint: endpoint.to_owned(),
44 model: CompactString::from(model),
45 })
46 }
47
48 pub fn no_auth(client: Client, endpoint: &str, model: &str) -> Self {
50 use reqwest::header::{self, HeaderValue};
51 let mut headers = HeaderMap::new();
52 headers.insert(
53 header::CONTENT_TYPE,
54 HeaderValue::from_static("application/json"),
55 );
56 headers.insert(header::ACCEPT, HeaderValue::from_static("application/json"));
57 Self {
58 client,
59 headers,
60 endpoint: endpoint.to_owned(),
61 model: CompactString::from(model),
62 }
63 }
64}