ai_api/services.py [chapter] deterministic
import httpx import json OLLAMA_URL = "http://localhost:11434/api/generate" def ollama_generate(prompt: str) -> str: data = { "model": "llama3", "prompt": prompt, "stream": False, } with httpx.Client(
import httpx import json OLLAMA_URL = "http://localhost:11434/api/generate" def ollama_generate(prompt: str) -> str: data = { "model": "llama3", "prompt": prompt, "stream": False, } with httpx.Client() as client: response = client.post(OLLAMA_URL, json=data) response.raise_for_status() return response.json().get("response", "")
`
This approach is simple and effective for synchronous use. For production, consider adding retries and timeouts.
Implementing Async AI Processing AI inference can be slow, so offload it to background workers. Django 3.1+ supports async views, but heavy CPU work is better handled by a task queue.
```python
Sources
Sovereign AI: Building Local-First Intelligent Systems (book) · source
Related (0)
No recorded relationships.