Skip to content

Commit 8baf93a

Browse files
authored
Merge pull request #77 from ansari-project/code-reorg
Moving files around to adhere to Python standards
2 parents cf47a5e + fc02ec1 commit 8baf93a

40 files changed

+131
-64
lines changed

.github/workflows/python-app.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ jobs:
2828
WHATSAPP_BUSINESS_PHONE_NUMBER_ID: ${{ secrets.WHATSAPP_BUSINESS_PHONE_NUMBER_ID }}
2929
WHATSAPP_ACCESS_TOKEN_FROM_SYS_USER: ${{ secrets.WHATSAPP_ACCESS_TOKEN_FROM_SYS_USER }}
3030
WHATSAPP_VERIFY_TOKEN_FOR_WEBHOOK: ${{ secrets.WHATSAPP_VERIFY_TOKEN_FOR_WEBHOOK }}
31-
31+
PYTHONPATH: src
32+
3233
container: python:3.10
3334
services:
3435
postgres:

main_file.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

pyproject.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[build-system]
2+
requires = ["setuptools", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "ansari-backend"
7+
version = "0.1.0"
8+
description = "Ansari is an AI assistant to enhance understanding and practice of Islam."
9+
authors = [
10+
{ name = "Ansari Project", email = "[email protected]" }
11+
]
12+
requires-python = ">=3.8"
13+
14+
[project.urls]
15+
Homepage = "https://github.com/ansari-project/ansari-backend"
16+
Documentation = "https://github.com/ansari-project/ansari-backend"
17+
Source = "https://github.com/ansari-project/ansari-backend"
18+
Tracker = "https://github.com/ansari-project/ansari-backend/issues"
19+
20+
[tool.ruff]
21+
line-length = 88
22+
lint.select = ["E", "F", "W"]
23+
lint.ignore = ["E501"]

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
markers =
3+
asyncio: mark a test as asyncio

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
bcrypt
2+
build
23
discord.py
34
diskcache
45
fastapi
@@ -14,10 +15,13 @@ psycopg2-binary
1415
pydantic_settings
1516
pyislam
1617
pyjwt
18+
pytest-asyncio
1719
rich
1820
sendgrid
21+
setuptools
1922
tenacity
2023
tiktoken
2124
typer
2225
uvicorn
26+
wheel
2327
zxcvbn

src/ansari/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This file marks the directory as a Python package.
2+
from .config import Settings, get_settings
3+
4+
__all__ = ["Settings", "get_settings"]

src/ansari/agents/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .ansari import Ansari
2+
from .ansari_workflow import AnsariWorkflow
3+
4+
__all__ = ["Ansari", "AnsariWorkflow"]

agents/ansari.py renamed to src/ansari/agents/ansari.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import json
33
import logging
44
import os
5-
import re
6-
import sys
75
import time
86
import traceback
97
from datetime import date, datetime
@@ -12,11 +10,11 @@
1210
import litellm
1311
from langfuse.decorators import langfuse_context, observe
1412

15-
from config import get_settings
16-
from tools.search_hadith import SearchHadith
17-
from tools.search_vectara import SearchVectara
18-
from tools.search_quran import SearchQuran
19-
from util.prompt_mgr import PromptMgr
13+
from ansari.config import get_settings
14+
from ansari.tools.search_hadith import SearchHadith
15+
from ansari.tools.search_vectara import SearchVectara
16+
from ansari.tools.search_quran import SearchQuran
17+
from ansari.util.prompt_mgr import PromptMgr
2018

2119
logger = logging.getLogger(__name__ + ".Ansari")
2220
logging_level = get_settings().LOGGING_LEVEL.upper()
@@ -49,7 +47,7 @@ def __init__(self, settings, message_logger=None, json_format=False):
4947
sm.get_tool_name(): sm,
5048
}
5149
self.model = settings.MODEL
52-
self.pm = PromptMgr()
50+
self.pm = PromptMgr(src_dir=settings.PROMPT_PATH)
5351
self.sys_msg = self.pm.bind(settings.SYSTEM_PROMPT_FILE_NAME).render()
5452
self.tools = [
5553
x.get_tool_description() for x in self.tool_name_to_instance.values()

agents/ansari_workflow.py renamed to src/ansari/agents/ansari_workflow.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77
import litellm
88

9-
from tools.search_hadith import SearchHadith
10-
from tools.search_vectara import SearchVectara
11-
from tools.search_quran import SearchQuran
12-
from util.prompt_mgr import PromptMgr
9+
from ansari.tools.search_hadith import SearchHadith
10+
from ansari.tools.search_vectara import SearchVectara
11+
from ansari.tools.search_quran import SearchQuran
12+
from ansari.util.prompt_mgr import PromptMgr
1313

1414
logger = logging.getLogger(__name__ + ".AnsariWorkflow")
1515

ansari_db.py renamed to src/ansari/ansari_db.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
import logging
33
from contextlib import contextmanager
44
from datetime import datetime, timedelta, timezone
5-
5+
from typing import Union
66
import bcrypt
77
import jwt
88
import psycopg2
99
import psycopg2.pool
1010
from fastapi import HTTPException, Request
1111
from jwt import ExpiredSignatureError, InvalidTokenError
1212

13-
from config import Settings, get_settings
13+
from ansari.config import Settings, get_settings
1414

1515
logger = logging.getLogger(__name__)
1616
logging_level = get_settings().LOGGING_LEVEL.upper()
@@ -588,7 +588,7 @@ def store_quran_answer(self, surah: int, ayah: int, question: str, ansari_answer
588588
)
589589
conn.commit()
590590

591-
def get_quran_answer(self, surah: int, ayah: int, question: str) -> str | None:
591+
def get_quran_answer(self, surah: int, ayah: int, question: str) -> Union[str, None]:
592592
"""
593593
Retrieve the stored answer for a given surah, ayah, and question.
594594
@@ -612,7 +612,6 @@ def get_quran_answer(self, surah: int, ayah: int, question: str) -> str | None:
612612
"""
613613
cur.execute(select_cmd, (surah, ayah, question))
614614
result = cur.fetchone()
615-
616615
if result:
617616
return result[0]
618617
else:

0 commit comments

Comments
 (0)