pepperoni21 / ollama-rs

A Rust library allowing to interact with the Ollama API.
MIT License
533 stars 79 forks source link

Make async optional #81

Open jshook opened 1 month ago

jshook commented 1 month ago

It's easy for users to make an call async if needed. It's not easy for them to (simply) call an async fn in a non-async context. Can you expose sync methods by default? (Add one if needed, to avoid impacting current users?) I have async code in the stack, but the context doesn't always (easily) reach down to where I've attached ollama-rs. I'd rather be able to pick where async calls happen, than be forced to make all calls async all the time everywhere.

InAnYan commented 2 weeks ago

As a dumb workaround, you can try to manually call API endpoints with a sync request library, and serialize arguments and deserialize responses with types from ollama-rs

pepperoni21 commented 2 weeks ago

We would need to rewrite all the functions synchronously and expose them under a "sync" feature or add an "async" feature for all the others. If someone's down to do it why not but currently I don't have the time

thewh1teagle commented 2 weeks ago

At least can someone tell how can I call it from non async program? do I have to initiate tokio runtime? I tried to use executor::executer::block_on but it failed with

there is no reactor running, must be called from the context of a Tokio 1.x runtime
pepperoni21 commented 2 weeks ago

At least can someone tell how can I call it from non async program? do I have to initiate tokio runtime?

I tried to use executor::executer::block_on but it failed with


there is no reactor running, must be called from the context of a Tokio 1.x runtime

ChatGPT gives me that example, hope this helps

use tokio::runtime::Runtime;

fn main() {
    // Create a new Tokio runtime
    let rt = Runtime::new().unwrap();

    // Use `block_on` to run the async function in the sync context
    rt.block_on(async {
        async_function().await;
    });
}

async fn async_function() {
    println!("Hello from async!");
}