- A single-request text generation (one-shot prompt)
- A context-aware chat loop that preserves conversation history
1) Create and activate a virtual environment
From your project directory create a virtual environment with Python 3:- WSL / macOS / Linux:
- Windows (PowerShell):
| Purpose | Command |
|---|---|
| Create venv | python3 -m venv venv |
| Activate (WSL/macOS/Linux) | source venv/bin/activate |
| Activate (Windows PowerShell) | .\venv\Scripts\Activate.ps1 |
| Deactivate | deactivate |
2) Install the Ollama Python client
With the venv active, install the client:The Ollama Python package is a lightweight HTTP client that sends requests to your locally running Ollama background process. The package does not run models locally; it forwards requests to the Ollama server.
3) Simple generation example (single prompt)
Create a file namedmain.py and add:
generate call is ideal for one-shot prompts — you send a single prompt and receive a single response, useful for tasks like summarization, code generation, and short Q&A.
4) Context-aware chat loop
For multi-turn conversations you should accumulate the message history and use the chat API. Save this aschat_main.py (or merge into main.py).
- Type a prompt when asked (for example: “Tell me a funny joke about Python”).
- Continue the conversation; the
messageslist preserves the full exchange so the model can reference earlier turns. - Enter
/exitor press Enter on an empty line to quit.
- The
generateexample demonstrates single-shot usage. - The chat loop shows how to preserve conversational context by appending
{'role': 'user'|'assistant', 'content': ...}entries to amessageslist and sending that full history each time.
File summary
| Filename | Purpose |
|---|---|
main.py | Single-shot generation example using ollama.generate() |
chat_main.py | Multi-turn chat example using ollama.chat() and a messages list |
Make sure your Ollama server is running locally before executing these scripts. The Python client communicates with the background Ollama process via HTTP and will fail if the server is not available. Also replace
gemma3:latest with the model name you have installed.- Keep your virtual environment activated while installing and running the scripts.
- Monitor available models with
ollama listand update themodel=parameter accordingly. - Use the chat pattern to maintain conversational state when building bots, assistants, or multi-turn tools.