Skip to content

Commit

Permalink
Use ruff instead of black for performance boost
Browse files Browse the repository at this point in the history
  • Loading branch information
abhahn committed Apr 16, 2024
1 parent ac47830 commit bbe46ef
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 28 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Ruff
on: [push, pull_request]
jobs:
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
17 changes: 8 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
repos:
# Using this mirror lets us use mypyc-compiled black, which is about 2x faster
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.3.0
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.3.7
hooks:
- id: black
# It is recommended to specify the latest version of Python
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
language_version: python3.10
# Run the linter.
- id: ruff
args: [ --fix ]
# Run the formatter.
- id: ruff-format
18 changes: 6 additions & 12 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,14 +1078,10 @@ async def delete_conversation():
raise Exception("CosmosDB is not configured or not working")

## delete the conversation messages from cosmos first
deleted_messages = await cosmos_conversation_client.delete_messages(
conversation_id, user_id
)
await cosmos_conversation_client.delete_messages(conversation_id, user_id)

## Now delete the conversation
deleted_conversation = await cosmos_conversation_client.delete_conversation(
user_id, conversation_id
)
await cosmos_conversation_client.delete_conversation(user_id, conversation_id)

await cosmos_conversation_client.cosmosdb_client.close()

Expand Down Expand Up @@ -1246,12 +1242,12 @@ async def delete_all_conversations():
# delete each conversation
for conversation in conversations:
## delete the conversation messages from cosmos first
deleted_messages = await cosmos_conversation_client.delete_messages(
await cosmos_conversation_client.delete_messages(
conversation["id"], user_id
)

## Now delete the conversation
deleted_conversation = await cosmos_conversation_client.delete_conversation(
await cosmos_conversation_client.delete_conversation(
user_id, conversation["id"]
)
await cosmos_conversation_client.cosmosdb_client.close()
Expand Down Expand Up @@ -1289,9 +1285,7 @@ async def clear_messages():
raise Exception("CosmosDB is not configured or not working")

## delete the conversation messages from cosmos
deleted_messages = await cosmos_conversation_client.delete_messages(
conversation_id, user_id
)
await cosmos_conversation_client.delete_messages(conversation_id, user_id)

return (
jsonify(
Expand Down Expand Up @@ -1367,7 +1361,7 @@ async def generate_title(conversation_messages):

title = json.loads(response.choices[0].message.content)["title"]
return title
except Exception as e:
except Exception:
return messages[-2]["content"]


Expand Down
12 changes: 6 additions & 6 deletions backend/history/cosmosdbservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ async def ensure(self):
return False, "CosmosDB client not initialized correctly"

try:
database_info = await self.database_client.read()
except:
await self.database_client.read()
except Exception:
return (
False,
f"CosmosDB database {self.database_name} on account {self.cosmosdb_endpoint} not found",
)

try:
container_info = await self.container_client.read()
except:
await self.container_client.read()
except Exception:
return False, f"CosmosDB container {self.container_name} not found"

return True, "CosmosDB client initialized successfully"
Expand Down Expand Up @@ -131,7 +131,7 @@ async def get_conversation(self, user_id, conversation_id):
{"name": "@conversationId", "value": conversation_id},
{"name": "@userId", "value": user_id},
]
query = f"SELECT * FROM c where c.id = @conversationId and c.type='conversation' and c.userId = @userId"
query = "SELECT * FROM c where c.id = @conversationId and c.type='conversation' and c.userId = @userId"
conversations = []
async for item in self.container_client.query_items(
query=query, parameters=parameters
Expand Down Expand Up @@ -187,7 +187,7 @@ async def get_messages(self, user_id, conversation_id):
{"name": "@conversationId", "value": conversation_id},
{"name": "@userId", "value": user_id},
]
query = f"SELECT * FROM c WHERE c.conversationId = @conversationId AND c.type='message' AND c.userId = @userId ORDER BY c.timestamp ASC"
query = "SELECT * FROM c WHERE c.conversationId = @conversationId AND c.type='message' AND c.userId = @userId ORDER BY c.timestamp ASC"
messages = []
async for item in self.container_client.query_items(
query=query, parameters=parameters
Expand Down
28 changes: 27 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ pytest = "7.4.0"
pytest-asyncio = "0.23.2"
chardet = "5.2.0"
pre-commit = "^3.7.0"
ruff = "^0.3.7"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
include = ["app.py", "backend/*.py", "backend/**/*.py"]

0 comments on commit bbe46ef

Please sign in to comment.