-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreaming-relay.rs
56 lines (47 loc) · 1.54 KB
/
streaming-relay.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! This example shows how to build a simple REST API for relaying requests to and
//! streamed responses from Ollama.
//!
//! This example uses [Server-Sent Events (SSE)](https://en.wikipedia.org/wiki/Server-sent_events)
//! for realtime JSON streaming.
use axum::{response::{sse::Event, Sse}, routing::post, Json, Router};
use futures::{Stream, TryStreamExt};
use ollama_rest::{errors::Error, models::chat::ChatRequest, Ollama};
use once_cell::sync::Lazy;
use tokio::net::TcpListener;
const HOST_ADDR: &str = "127.0.0.1:9890";
static API: Lazy<Ollama> = Lazy::new(|| Ollama::default());
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", post(chat));
let listener = TcpListener::bind(HOST_ADDR).await.unwrap();
print_help_text();
axum::serve(listener, app).await.unwrap();
}
async fn chat(Json(payload): Json<ChatRequest>) -> Sse<impl Stream<Item = Result<Event, Error>>> {
Sse::new(
API.chat_streamed(&payload).await.unwrap()
.map_ok(|res| Event::default().json_data(res).unwrap()),
)
}
fn print_help_text() {
println!("Server listening at {HOST_ADDR}. Press Ctrl+C to exit.");
println!();
println!(r#"===
HOW TO USE THIS EXAMPLE:
===
1. Make sure the Ollama server is on!
2. Try calling this API using cURL, or Postman... (whatever you like :D)
For example:
```
curl -X POST http://127.0.0.1:9890/ -H 'Content-Type: application/json' -d '{{
"model": "llama3.2:1b",
"messages": [
{{
"role": "user",
"content": "why is the sky blue?"
}}
]
}}'
```"#);
}