Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple ollama chat example #336

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

matthewhaynesonline
Copy link

Fixes #191.

I used the code examples from #173 as a starting point and tried to simplify them to match the existing chat example.

@matthewhaynesonline matthewhaynesonline force-pushed the 191-ollama-example branch 2 times, most recently from d4122cd to 2ded27b Compare February 17, 2025 18:09
@matthewhaynesonline matthewhaynesonline changed the title #191: Add simple ollama chat example Add simple ollama chat example Feb 17, 2025
@@ -59,6 +59,11 @@ $Env:OPENAI_API_KEY='sk-...'
- Visit [examples](https://github.com/64bit/async-openai/tree/main/examples) directory on how to use `async-openai`.
- Visit [docs.rs/async-openai](https://docs.rs/async-openai) for docs.

### Local LLM Usage
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not required. Ollama example has its own README

@@ -0,0 +1,18 @@
## Prerequisites
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider adding fully contained example - in this case if there's dependency on ollama - please consider creating docker-compose file to bring it up in single command.

I'd prefer to have quick way to run an example, instead having them additional steps - which a user may already do on their own

@matthewhaynesonline
Copy link
Author

matthewhaynesonline commented Feb 24, 2025

Hi @64bit, I've updated my PR with your feedback, though the Docker setup has some caveats I didn't have elegant solutions for.

  1. Build context: since Docker restricts where you can copy files from to the current directory branch (parents / siblings are not allowed), I adjusted the example to pull the async_openai crate from crates.io, not the parent dir.
    1. The only way I could think to use the crate from the parent project dir was to place the docker-compose.yml / Dockerfile in the project root, but that would no longer make the example self contained.
  2. First time Ollama model pull: Ollama requires you to pull a model before you can use it and, understandably, the Ollama docker image doesn't come preloaded with any models. As such, a custom entrypoint is use to pull the model on startup. A healthcheck is then used to wait to start the Rust container until after the Ollama container is fully ready.

What do you think?

@64bit
Copy link
Owner

64bit commented Mar 3, 2025

Thanks for update, I just tried it and looks good!

I think Dockerfile and ollam_chat containers in docker-compose are not required. My initial reason for docker-compose was to bring up Ollama and then cargo run example which would point to "localhost:ollama-port".

Your entrypoint stuff to download model looks good to me.

So user has to do two steps:

# bring ollama up with model
docker compose up -d

# once model is downloaded and ollama is up
cargo run 

@@ -0,0 +1,9 @@
FROM rust:1.85 AS builder
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not required

Comment on lines +3 to +14
container_name: ollama_chat
build: .
command: ollama-chat
restart: on-failure:3
environment:
API_BASE: "http://ollama:11434/v1"
API_KEY: "ollama"
MODEL: "llama3.2:1b"
depends_on:
ollama:
condition: service_healthy

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not required

sleep 5

echo "Retrieving model $MODEL..."
ollama pull $MODEL
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice


#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let api_base = env::var("API_BASE").expect("API_BASE is not set in the environment.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be hard coded to what docker compose will bring up

so localhost:11434

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ollama AI Chat Completion Example / OpenAI Compatibility
2 participants