Skip to content

Commit

Permalink
Move to ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
pamelafox committed May 14, 2024
1 parent ed4fcc6 commit 3e42e82
Show file tree
Hide file tree
Showing 16 changed files with 21 additions and 49 deletions.
3 changes: 1 addition & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"ms-python.python",
"ms-python.vscode-pylance",
"charliermarsh.ruff",
"ms-python.black-formatter",
"mtxr.sqltools",
"mtxr.sqltools-driver-pg",
"ms-vscode.vscode-node-azure-pack",
Expand All @@ -45,7 +44,7 @@
"editor.codeActionsOnSave": {
"source.fixAll": "explicit"
},
"editor.defaultFormatter": "ms-python.black-formatter"
"editor.defaultFormatter": "charliermarsh.ruff"
},
"files.exclude": {
".ruff_cache": true,
Expand Down
3 changes: 1 addition & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ See [CONTRIBUTING.md](https://github.com/Azure-Samples/rag-postgres-openai-pytho
- [ ] I added tests that prove my fix is effective or that my feature works
- [ ] I ran `python -m pytest --cov` to verify 100% coverage of added lines
- [ ] I ran `python -m mypy` to check for type errors
- [ ] I either used the pre-commit hooks or ran `ruff` and `black` manually on my code.

- [ ] I either used the pre-commit hooks or ran `ruff` manually on my code.
4 changes: 2 additions & 2 deletions .github/workflows/python-code-quality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ jobs:
pip install -r requirements-dev.txt
- name: Lint with ruff
run: ruff check .
- name: Check formatting with black
run: black . --check --verbose
- name: Check formatting with ruff
run: ruff format --check .
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.0
hooks:
- id: ruff
- repo: https://github.com/psf/black
rev: 23.9.1
hooks:
- id: black
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
10 changes: 5 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Compile the JavaScript:
This codebase includes several languages: TypeScript, Python, Bicep, Powershell, and Bash.
Code should follow the standard conventions of each language.

For Python, you can enforce the conventions using `ruff` and `black`.
For Python, you can enforce the conventions using `ruff`.

Install the development dependencies:

Expand All @@ -104,13 +104,13 @@ python3 -m pip install -r requirements-dev.txt
Run `ruff` to lint a file:

```
python3 -m ruff <path-to-file>
python3 -m ruff check <path-to-file>
```

Run `black` to format a file:
Run `ruff` to format a file:

```
python3 -m black <path-to-file>
python3 -m ruff format <path-to-file>
```

If you followed the steps above to install the pre-commit hooks, then you can just wait for those hooks to run `ruff` and `black` for you.
If you followed the steps above to install the pre-commit hooks, then you can just wait for those hooks to run `ruff` for you.
4 changes: 0 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ ignore = ["D203"]

[tool.ruff.lint.isort]
known-first-party = ["fastapi_app"]

[tool.black]
line-length = 120
target-version = ["py311"]
3 changes: 1 addition & 2 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-r src/requirements.txt
ruff
black
pre-commit
pip-tools
pip-tools
1 change: 0 additions & 1 deletion src/fastapi_app/api_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

@router.post("/chat")
async def chat_handler(chat_request: ChatRequest):

messages = [message.model_dump() for message in chat_request.messages]
overrides = chat_request.context.get("overrides", {})

Expand Down
1 change: 0 additions & 1 deletion src/fastapi_app/openai_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ async def create_openai_chat_client(azure_credential):


async def create_openai_embed_client(azure_credential):

OPENAI_EMBED_HOST = os.getenv("OPENAI_EMBED_HOST")
if OPENAI_EMBED_HOST == "azure":
token_provider = azure.identity.aio.get_bearer_token_provider(
Expand Down
1 change: 0 additions & 1 deletion src/fastapi_app/postgres_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@


async def create_postgres_engine(*, host, username, database, password, sslmode, azure_credential) -> AsyncEngine:

if host.endswith(".database.azure.com"):
logger.info("Authenticating to Azure Database for PostgreSQL using Azure Identity...")
if azure_credential is None:
Expand Down
2 changes: 0 additions & 2 deletions src/fastapi_app/postgres_searcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@


class PostgresSearcher:

def __init__(self, engine):
self.async_session_maker = async_sessionmaker(engine, expire_on_commit=False)

Expand All @@ -30,7 +29,6 @@ async def search(
query_top: int = 5,
filters: list[dict] | None = None,
):

filter_clause_where, filter_clause_and = self.build_filter_clause(filters)

vector_query = f"""
Expand Down
13 changes: 4 additions & 9 deletions src/fastapi_app/rag_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def __init__(
chat_model: str,
chat_deployment: str | None, # Not needed for non-Azure OpenAI
openai_embed_client: AsyncOpenAI,
embed_deployment: str
| None, # Not needed for non-Azure OpenAI or for retrieval_mode="text"
embed_deployment: str | None, # Not needed for non-Azure OpenAI or for retrieval_mode="text"
embed_model: str,
embed_dimensions: int,
):
Expand Down Expand Up @@ -60,8 +59,7 @@ async def run(
system_prompt=self.query_prompt_template,
new_user_content=original_user_query,
past_messages=past_messages,
max_tokens=self.chat_token_limit
- query_response_token_limit, # TODO: count functions
max_tokens=self.chat_token_limit - query_response_token_limit, # TODO: count functions
fallback_to_default=True,
)

Expand Down Expand Up @@ -93,17 +91,14 @@ async def run(

results = await self.searcher.search(query_text, vector, top, filters)

sources_content = [
f"[{(item.id)}]:{item.to_str_for_rag()}\n\n" for item in results
]
sources_content = [f"[{(item.id)}]:{item.to_str_for_rag()}\n\n" for item in results]
content = "\n".join(sources_content)

# Generate a contextual and content specific answer using the search results and chat history
response_token_limit = 1024
messages = build_messages(
model=self.chat_model,
system_prompt=overrides.get("prompt_template")
or self.answer_prompt_template,
system_prompt=overrides.get("prompt_template") or self.answer_prompt_template,
new_user_content=original_user_query + "\n\nSources:\n" + content,
past_messages=past_messages,
max_tokens=self.chat_token_limit - response_token_limit,
Expand Down
2 changes: 0 additions & 2 deletions src/fastapi_app/rag_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class SimpleRAGChat:

def __init__(
self,
*,
Expand Down Expand Up @@ -41,7 +40,6 @@ def __init__(
async def run(
self, messages: list[dict], overrides: dict[str, Any] = {}
) -> dict[str, Any] | AsyncGenerator[dict[str, Any], None]:

text_search = overrides.get("retrieval_mode") in ["text", "hybrid", None]
vector_search = overrides.get("retrieval_mode") in ["vectors", "hybrid", None]
top = overrides.get("top", 3)
Expand Down
3 changes: 0 additions & 3 deletions src/fastapi_app/setup_postgres_azurerole.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


async def assign_role_for_webapp(engine, app_identity_name):

async with engine.begin() as conn:
identities = await conn.execute(
text(f"select * from pgaadauth_list_principals(false) WHERE rolname = '{app_identity_name}'")
Expand Down Expand Up @@ -39,7 +38,6 @@ async def assign_role_for_webapp(engine, app_identity_name):


async def main():

parser = argparse.ArgumentParser(description="Create database schema")
parser.add_argument("--host", type=str, help="Postgres host")
parser.add_argument("--username", type=str, help="Postgres username")
Expand All @@ -64,7 +62,6 @@ async def main():


if __name__ == "__main__":

logging.basicConfig(level=logging.WARNING)
logger.setLevel(logging.INFO)
load_dotenv(override=True)
Expand Down
2 changes: 0 additions & 2 deletions src/fastapi_app/setup_postgres_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async def create_db_schema(engine):


async def main():

parser = argparse.ArgumentParser(description="Create database schema")
parser.add_argument("--host", type=str, help="Postgres host")
parser.add_argument("--username", type=str, help="Postgres username")
Expand All @@ -45,7 +44,6 @@ async def main():


if __name__ == "__main__":

logging.basicConfig(level=logging.WARNING)
logger.setLevel(logging.INFO)
load_dotenv(override=True)
Expand Down
8 changes: 2 additions & 6 deletions src/fastapi_app/setup_postgres_seeddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ async def seed_data(engine):
)
)
if not result.scalar():
logger.error(
"Items table does not exist. Please run the database setup script first."
)
logger.error("Items table does not exist. Please run the database setup script first.")
return

async with async_sessionmaker(engine, expire_on_commit=False)() as session:
Expand All @@ -38,9 +36,7 @@ async def seed_data(engine):
with open(os.path.join(current_dir, "seed_data.json")) as f:
catalog_items = json.load(f)
for catalog_item in catalog_items:
item = await session.execute(
select(Item).filter(Item.id == catalog_item["Id"])
)
item = await session.execute(select(Item).filter(Item.id == catalog_item["Id"]))
if item.scalars().first():
continue
item = Item(
Expand Down

0 comments on commit 3e42e82

Please sign in to comment.