walrus_model/openai/
mod.rs1use 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 DEEPSEEK: &str = "https://api.deepseek.com/chat/completions";
17 pub const GROK: &str = "https://api.x.ai/v1/chat/completions";
19 pub const QWEN: &str = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
21 pub const KIMI: &str = "https://api.moonshot.cn/v1/chat/completions";
23 pub const OLLAMA: &str = "http://localhost:11434/v1/chat/completions";
25}
26
27#[derive(Clone)]
29pub struct OpenAI {
30 pub client: Client,
32 headers: HeaderMap,
34 endpoint: String,
36}
37
38impl OpenAI {
39 pub fn api(client: Client, key: &str) -> anyhow::Result<Self> {
41 Self::custom(client, key, endpoint::OPENAI)
42 }
43
44 pub fn deepseek(client: Client, key: &str) -> anyhow::Result<Self> {
46 Self::custom(client, key, endpoint::DEEPSEEK)
47 }
48
49 pub fn grok(client: Client, key: &str) -> anyhow::Result<Self> {
51 Self::custom(client, key, endpoint::GROK)
52 }
53
54 pub fn qwen(client: Client, key: &str) -> anyhow::Result<Self> {
56 Self::custom(client, key, endpoint::QWEN)
57 }
58
59 pub fn kimi(client: Client, key: &str) -> anyhow::Result<Self> {
61 Self::custom(client, key, endpoint::KIMI)
62 }
63
64 pub fn ollama(client: Client) -> anyhow::Result<Self> {
66 use reqwest::header;
67 let mut headers = HeaderMap::new();
68 headers.insert(header::CONTENT_TYPE, "application/json".parse()?);
69 headers.insert(header::ACCEPT, "application/json".parse()?);
70 Ok(Self {
71 client,
72 headers,
73 endpoint: endpoint::OLLAMA.to_owned(),
74 })
75 }
76
77 pub fn custom(client: Client, key: &str, endpoint: &str) -> anyhow::Result<Self> {
79 use reqwest::header;
80 let mut headers = HeaderMap::new();
81 headers.insert(header::CONTENT_TYPE, "application/json".parse()?);
82 headers.insert(header::ACCEPT, "application/json".parse()?);
83 headers.insert(header::AUTHORIZATION, format!("Bearer {key}").parse()?);
84 Ok(Self {
85 client,
86 headers,
87 endpoint: endpoint.to_owned(),
88 })
89 }
90}