Expand description
Sakura-AI-rs
§さくらのAI Engine を簡単に使うための非公式Rustクライアント
ollama-rsと互換性を持つことで既存のコードを最小限の変更で再利用できるようにしています。
※現在はChat Completionのみをサポート
§インストール
ollama-rsとsakura-ai-rsをCargo.tomlのdependenciesに追加
cargo add sakura-ai-rs
cargo add ollama-rs§使い方
アカウントトークンを作成し、環境変数SAKURA_AI_ENGINE_API_KEYに設定します。
# macOS / Linux
export SAKURA_AI_ENGINE_API_KEY='...'# Windows PowerShell
$env:SAKURA_AI_ENGINE_API_KEY = '...'§SakuraAIの初期化
use sakura_ai_rs::SakuraAI;
let sakura = SakuraAI::default();§Chat Completion API
send_chat_messagesを用いてSakura AI EngineのChat Completionを呼び出す。
use ollama_rs::generation::chat::{ChatMessage, request::ChatMessageRequest};
use ollama_rs::history::ChatHistory;
use sakura_ai_rs::SakuraAI;
let sakura = SakuraAI::default();
let model = "gpt-oss-120b".to_string();
let prompt = "Why is the sky blue?".to_string();
let res = sakura
.send_chat_messages(
ChatMessageRequest::new(
model,
vec![ChatMessage::user(prompt)],
),
)
.await;
if let Ok(res) = res {
println!("{}", res.message.content);
}send_chat_messages_streamの使用例はexamples/chat_api_chatbot.rsを参考に。
> cargo run --example chat_api_chatbot
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.19s
Running `target\debug\examples\chat_api_chatbot.exe`
> こんにちは
こんにちは!今日はどのようなお手伝いが必要ですか?
>§Chat Completion API with History
send_chat_messages_with_historyを用いると会話履歴も管理される。
use ollama_rs::generation::chat::{ChatMessage, request::ChatMessageRequest};
use ollama_rs::history::ChatHistory;
use sakura_ai_rs::SakuraAI;
let sakura = SakuraAI::default();
let model = "llama2:latest".to_string();
let prompt = "Why is the sky blue?".to_string();
// `Vec<ChatMessage>` implements `ChatHistory`,
// but you could also implement it yourself on a custom type
let mut history = vec![];
let res = sakura
.send_chat_messages_with_history(
&mut history, // <- messages will be saved here
ChatMessageRequest::new(
model,
vec![ChatMessage::user(prompt)], // <- You should provide only one message
),
)
.await;
if let Ok(res) = res {
println!("{}", res.message.content);
}examples/chat_with_history.rsとexamples/chat_with_history_stream.rsを参考に。
§Ollama-rsからの移行
上の例をollama-rsと比較すると分かる通り、ollama_rs::Ollamaの代わりにsakura_ai_rs::SakuraAIを使用すればよい。
入出力のオブジェクトには同じものを使用しているため、それ以外のコードは同じ。
現在、send_chat_messages, send_chat_messages_stream, send_chat_messages_with_history, send_chat_messages_with_history_stream がサポートされている。