Files
chat_backend/llm_be/chat_backend/client.py
T
westfarn d1660792ad
Unit Tests / test (push) Successful in 13s
Dockerize chat_backend + Ollama LAN + DB file storage (#6) (#7)
## Summary

Implements [chat_backend#6](#6) Part A:

- **uv** packaging (`pyproject.toml` + `uv.lock`), Docker/compose (dev + prod), entrypoint/validate-env, Gitea unit-test + auto-deploy workflows (mirror `scha`)
- Env-driven Django settings (`DJANGO_*`, `DATABASE_URL`, CSRF/CORS)
- **`OLLAMA_BASE_URL`** wired through all Ollama/LangChain clients (prod → `http://10.0.0.128:11434`)
- **DatabaseStorage** — prompt/document file blobs in Postgres (`StoredFile`), not container FS; RAG materializes temp paths for loaders
- ASGI via `gunicorn` + `UvicornWorker` (HTTP + WebSockets)

Companion server-infra PR registers `app_catalog` / `host_apps` (port **8003**).

## Test plan

- [ ] `uv sync && cd llm_be && SKIP_RAG_INIT=1 uv run python manage.py test`
- [ ] `docker compose build && docker compose up` against bundled Postgres
- [ ] Confirm Ollama calls use `OLLAMA_BASE_URL` (not hardcoded localhost)
- [ ] Upload a document / prompt file → row in `chat_backend_storedfile`, no disk under `media/`
- [ ] After server-infra merge + secret/Postgres/NPM: deploy via `deploy.sh --app chat_backend --env prod`Reviewed-on: #7
2026-07-25 05:23:33 -07:00

34 lines
1.1 KiB
Python

"""
llama client - Abstract this in the future
"""
import ollama
from typing import List, Dict
from chat_backend.ollama_config import ollama_base_url
class LlamaClient(object):
def __init__(self, model: str = "llama3"):
self.client = ollama.Client(host=ollama_base_url())
self.model = model
def check_if_model_exists(self) -> bool:
raise NotImplementedError
def generate_conversation_title(self, message: str):
response = self.generate_single_message(
'Summarise the phrase in one to for words"%s"' % message
)
raw_response = response["response"].replace('"', "")
return " ".join(raw_response.split()[:4])
def generate_single_message(self, message: str):
return self.client.generate(model=self.model, prompt=message)
def get_chat_response(self, messages: List[str]):
return self.client.chat(model=self.model, messages=messages, stream=False)
def get_streamed_chat_response(self, messages: List[str]):
return self.client.chat(model=self.model, messages=messages, stream=True)