-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54cabbe
commit 5936f34
Showing
11 changed files
with
243 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from datetime import timedelta, datetime | ||
import os | ||
from pydantic import BaseModel | ||
from jose import jwt | ||
|
||
|
||
from database.database import DatabaseConnection | ||
|
||
SECRET_KEY = os.environ.get("SECRET_KEY", "default_unsecure_key") | ||
ALGORITHM = "HS256" | ||
|
||
class User(BaseModel): | ||
email: str = None | ||
password: str = None | ||
|
||
def create_user(user: User): | ||
with DatabaseConnection() as connection: | ||
connection.query("INSERT INTO user (email, password) VALUES (?, ?)", (user.email, user.password)) | ||
|
||
def get_user(email: str): | ||
with DatabaseConnection() as connection: | ||
user_row = connection.query("SELECT * FROM user WHERE email = ?", (email,))[0] | ||
for row in user_row: | ||
return User(**row) | ||
raise Exception("User not found") | ||
|
||
def authenticate_user(username: str, password: str): | ||
user = get_user(username) | ||
if not user or not password == user.password: | ||
return False | ||
return user | ||
|
||
def create_access_token(*, data: dict, expires_delta: timedelta = None): | ||
to_encode = data.copy() | ||
if expires_delta: | ||
expire = datetime.utcnow() + expires_delta | ||
else: | ||
expire = datetime.utcnow() + timedelta(minutes=15) | ||
to_encode.update({"exp": expire}) | ||
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) | ||
return encoded_jwt |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from pathlib import Path | ||
import sqlite3 | ||
from typing import List | ||
|
||
class DatabaseConnection: | ||
def __enter__(self): | ||
self.conn = sqlite3.connect(Path(__file__).parent / "database.sqlite") | ||
self.conn.row_factory = sqlite3.Row | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
self.conn.commit() | ||
self.conn.close() | ||
|
||
def query(self, query, params=None) -> List[List[sqlite3.Row]]: | ||
cursor = self.conn.cursor() | ||
results = [] | ||
commands = filter(None, query.split(";")) | ||
for command in commands: | ||
cursor.execute(command, params or ()) | ||
results.append(cursor.fetchall()) | ||
return results | ||
|
||
def query_from_file(self, file_path): | ||
with open(file_path, 'r') as file: | ||
query = file.read() | ||
self.query(query) | ||
|
||
with DatabaseConnection() as connection: | ||
connection.query_from_file(Path(__file__).parent / "database_init.sql") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
-- Go to https://dbdiagram.io/d/RAGAAS-63dbdcc6296d97641d7e07c8 | ||
-- Make your changes | ||
-- Export > Export to PostgresSQL (or other) | ||
-- Translate to SQLite (works with a cmd+k in Cursor, or https://www.rebasedata.com/convert-postgresql-to-sqlite-online) | ||
-- Paste here | ||
-- Replace "CREATE TABLE" with "CREATE TABLE IF NOT EXISTS" | ||
|
||
CREATE TABLE IF NOT EXISTS "user" ( | ||
"email" TEXT PRIMARY KEY, | ||
"password" TEXT | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS "chat" ( | ||
"id" TEXT PRIMARY KEY, | ||
"user_id" TEXT, | ||
FOREIGN KEY ("user_id") REFERENCES "user" ("email") | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS "message" ( | ||
"id" TEXT PRIMARY KEY, | ||
"timestamp" TEXT, | ||
"chat_id" TEXT, | ||
"sender" TEXT, | ||
"content" TEXT, | ||
FOREIGN KEY ("chat_id") REFERENCES "chat" ("id") | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS "feedback" ( | ||
"id" TEXT PRIMARY KEY, | ||
"message_id" TEXT, | ||
"feedback" TEXT, | ||
FOREIGN KEY ("message_id") REFERENCES "message" ("id") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
class ChatMessage(BaseModel): | ||
message: str | ||
message_id: str | ||
session_id: str | ||
|
||
class Doc(BaseModel): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,6 @@ universal_pathlib | |
chromadb | ||
langchain | ||
langchainhub | ||
gpt4all | ||
gpt4all | ||
python-multipart | ||
httpx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from fastapi.testclient import TestClient | ||
from main import app | ||
|
||
client = TestClient(app) | ||
|
||
def test_signup(): | ||
response = client.post("/user/signup", json={"email": "[email protected]", "password": "testpassword"}) | ||
assert response.status_code == 200 | ||
assert response.json()["email"] == "[email protected]" | ||
|
||
def test_login(): | ||
response = client.post("/user/login", data={"username": "[email protected]", "password": "testpassword"}) | ||
assert response.status_code == 200 | ||
assert "access_token" in response.json() | ||
|
||
def test_user_me(): | ||
login_response = client.post("/user/login", data={"username": "[email protected]", "password": "testpassword"}) | ||
token = login_response.json()["access_token"] | ||
response = client.get("/user/me", headers={"Authorization": f"Bearer {token}"}) | ||
assert response.status_code == 200 | ||
assert response.json()["email"] == "[email protected]" |