lancor
A Rust client library for llama.cpp's OpenAI-compatible API server.
Features
- 🚀 Async/await support with Tokio
- 💬 Chat completions (streaming and non-streaming)
- 📝 Text completions
- 🔢 Embeddings generation
- 🔑 API key authentication support
- 🎯 Type-safe request/response handling
- 🛠️ Builder pattern for easy request construction
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
Quick Start
use ;
use Result;
async
Usage Examples
Chat Completion
use ;
let client = new?;
let request = new
.message
.message
.temperature
.max_tokens;
let response = client.chat_completion.await?;
println!;
Streaming Chat Completion
use ;
use StreamExt;
let client = new?;
let request = new
.message
.stream
.max_tokens;
let mut stream = client.chat_completion_stream.await?;
while let Some = stream.next.await
Text Completion
use ;
let client = new?;
let request = new
.max_tokens
.temperature;
let response = client.completion.await?;
println!;
Embeddings
use ;
let client = new?;
let request = new;
let response = client.embedding.await?;
let embedding_vector = &response.data.embedding;
println!;
Authentication
use LlamaCppClient;
// With API key
let client = with_api_key?;
API Reference
LlamaCppClient
The main client for interacting with llama.cpp server.
Methods
new(base_url)- Create a new clientwith_api_key(base_url, api_key)- Create a client with API key authenticationdefault()- Create a client connecting tohttp://localhost:8080chat_completion(request)- Send a chat completion requestchat_completion_stream(request)- Send a streaming chat completion requestcompletion(request)- Send a text completion requestembedding(request)- Send an embedding request
Request Builders
All request types support a fluent builder pattern:
new
.message
.temperature
.max_tokens
.top_p
.stream;
Requirements
- Rust 1.70 or later
- A running llama.cpp server with OpenAI-compatible API enabled
Running llama.cpp Server
To use this client, you need to run llama.cpp with the --api-key flag (optional) and ensure the OpenAI-compatible endpoints are enabled:
Examples
Check out the examples directory for more usage examples:
License
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgments
- llama.cpp - The amazing llama.cpp project
- OpenAI API - API specification reference