Skip to content

feat: remove cache #182

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ Live at https://tenantfirstaid.com/

### Prerequisites
- [uv](https://docs.astral.sh/uv/getting-started/installation/)
- [docker](https://www.docker.com/)

1. copy `backend/.env.example` to a new file named `.env` in the same directory and populate it with your `OPENAI_API_KEY`. You can set an invalid key, in which case the bot will return error messages. This may still be useful for developing other features.
1. `cd backend`
1. `docker-compose up` (use `-d` if you want to run this in the background, otherwise open a new terminal)
1. `uv sync`
1. If you have not uploaded the Oregon Housing Law documents to a vector store in OpenAI, run `uv run scripts/create_vector_store.py` and follow the instructions to add the vector store ID to your `.env`.
1. `uv run python -m tenantfirstaid.app`
Expand Down
16 changes: 5 additions & 11 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
# Specify a different model
MODEL_NAME=gpt-2.5-flash
MODEL_NAME=gpt-2.5-pro

# Vector store ID for OpenAI (use the create_vector_store script to create one)
VECTOR_STORE_ID=my_vector_store_id

# DB Info
DB_HOST=127.0.0.1
DB_PORT=6379
DB_USE_SSL=false
# Specify model for eval user
USER_MODEL_NAME=gemini-2.0-flash-lite

# Not used for local dev
#DB_USERNAME=default
#DB_PASSWORD=password
# Vector store ID for OpenAI (use the create_vector_store script to create one)
GEMINI_RAG_CORPUS=my_vector_store_id
1 change: 0 additions & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "0.2.0"
requires-python = ">=3.12"
dependencies = [
"flask>=3.1.1",
"valkey>=6.1.0",
"gunicorn>=23.0.0",
"google-auth>=2.40.3",
"google-genai>=1.28.0",
Expand Down
43 changes: 2 additions & 41 deletions backend/tenantfirstaid/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from pathlib import Path
from flask import Flask, jsonify, session
import os
import secrets

from flask import Flask

if Path(".env").exists():
from dotenv import load_dotenv
Expand All @@ -11,46 +8,10 @@

from .chat import ChatView

from .session import InitSessionView, TenantSession
from .citations import get_citation

app = Flask(__name__)

# Configure Flask sessions
app.secret_key = os.getenv("FLASK_SECRET_KEY", secrets.token_hex(32))
app.config["SESSION_COOKIE_HTTPONLY"] = True
app.config["SESSION_COOKIE_SECURE"] = os.getenv("ENV", "dev") == "prod"
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"


tenant_session = TenantSession()


@app.get("/api/history")
def history():
saved_session = tenant_session.get()
return jsonify(saved_session["messages"])


@app.post("/api/clear-session")
def clear_session():
session.clear()
return jsonify({"success": True})


app.add_url_rule(
"/api/init",
view_func=InitSessionView.as_view("init", tenant_session),
methods=["POST"],
)

app.add_url_rule(
"/api/query", view_func=ChatView.as_view("chat", tenant_session), methods=["POST"]
)

app.add_url_rule(
"/api/citation", endpoint="citation", view_func=get_citation, methods=["GET"]
)
app.add_url_rule("/api/query", view_func=ChatView.as_view("chat"), methods=["POST"])

if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=5001)
26 changes: 8 additions & 18 deletions backend/tenantfirstaid/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def generate_gemini_chat_response(
instructions=None,
model_name=MODEL,
):
print(f"Generating response for messages: {messages}")
instructions = (
instructions
if instructions
Expand Down Expand Up @@ -112,28 +113,26 @@ def generate_gemini_chat_response(
generation_config=GenerationConfig(temperature=0.2),
tools=[rag_retrieval_tool] if use_tools else None,
)
print(f"Response: {response}")

return response


class ChatView(View):
def __init__(self, tenant_session) -> None:
self.tenant_session = tenant_session
def __init__(self) -> None:
self.chat_manager = ChatManager()

def dispatch_request(self, *args, **kwargs) -> Response:
data = request.json
user_msg = data["message"]

current_session = self.tenant_session.get()
current_session["messages"].append(dict(role="user", content=user_msg))
messages = data["messages"]
print(f"Received messages: {messages}")

def generate():
# Use the new Responses API with streaming
response_stream = self.chat_manager.generate_gemini_chat_response(
current_session["messages"],
current_session["city"],
current_session["state"],
messages,
data["city"],
data["state"],
stream=True,
)

Expand All @@ -142,15 +141,6 @@ def generate():
assistant_chunks.append(event.candidates[0].content.parts[0].text)
yield event.candidates[0].content.parts[0].text

# Join the complete response
assistant_msg = "".join(assistant_chunks)

current_session["messages"].append(
{"role": "model", "content": assistant_msg}
)

self.tenant_session.set(current_session)

return Response(
stream_with_context(generate()),
mimetype="text/plain",
Expand Down
24 changes: 0 additions & 24 deletions backend/tenantfirstaid/citations.py

This file was deleted.

97 changes: 0 additions & 97 deletions backend/tenantfirstaid/session.py

This file was deleted.

Loading