walrus_model/openai/
mod.rs1use reqwest::{Client, header::HeaderMap};
7
8mod provider;
9
10pub mod endpoint {
12 pub const OPENAI: &str = "https://api.openai.com/v1/chat/completions";
14 pub const GROK: &str = "https://api.x.ai/v1/chat/completions";
16 pub const QWEN: &str = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
18 pub const KIMI: &str = "https://api.moonshot.cn/v1/chat/completions";
20 pub const OLLAMA: &str = "http://localhost:11434/v1/chat/completions";
22}
23
24#[derive(Clone)]
26pub struct OpenAI {
27 pub client: Client,
29 headers: HeaderMap,
31 endpoint: String,
33}
34
35impl OpenAI {
36 pub fn api(client: Client, key: &str) -> anyhow::Result<Self> {
38 Self::custom(client, key, endpoint::OPENAI)
39 }
40
41 pub fn grok(client: Client, key: &str) -> anyhow::Result<Self> {
43 Self::custom(client, key, endpoint::GROK)
44 }
45
46 pub fn qwen(client: Client, key: &str) -> anyhow::Result<Self> {
48 Self::custom(client, key, endpoint::QWEN)
49 }
50
51 pub fn kimi(client: Client, key: &str) -> anyhow::Result<Self> {
53 Self::custom(client, key, endpoint::KIMI)
54 }
55
56 pub fn ollama(client: Client) -> anyhow::Result<Self> {
58 use reqwest::header;
59 let mut headers = HeaderMap::new();
60 headers.insert(header::CONTENT_TYPE, "application/json".parse()?);
61 headers.insert(header::ACCEPT, "application/json".parse()?);
62 Ok(Self {
63 client,
64 headers,
65 endpoint: endpoint::OLLAMA.to_owned(),
66 })
67 }
68
69 pub fn custom(client: Client, key: &str, endpoint: &str) -> anyhow::Result<Self> {
71 use reqwest::header;
72 let mut headers = HeaderMap::new();
73 headers.insert(header::CONTENT_TYPE, "application/json".parse()?);
74 headers.insert(header::ACCEPT, "application/json".parse()?);
75 headers.insert(header::AUTHORIZATION, format!("Bearer {key}").parse()?);
76 Ok(Self {
77 client,
78 headers,
79 endpoint: endpoint.to_owned(),
80 })
81 }
82}