-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from redhog/throttle
Throttle
- Loading branch information
Showing
29 changed files
with
1,235 additions
and
940 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
name: Create or Update PR from staging to main | ||
|
||
on: | ||
push: | ||
branches: | ||
- staging | ||
pull_request: | ||
types: | ||
- closed | ||
branches: | ||
- staging | ||
|
||
jobs: | ||
create-or-update-pr: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Check for existing PR | ||
id: check_pr | ||
uses: actions/github-script@v6 | ||
with: | ||
github-token: ${{secrets.GITHUB_TOKEN}} | ||
script: | | ||
const { data: pullRequests } = await github.rest.pulls.list({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
state: 'open', | ||
head: 'staging', | ||
base: 'main' | ||
}); | ||
return pullRequests.length > 0 ? 'true' : 'false'; | ||
- name: Create Pull Request | ||
if: steps.check_pr.outputs.result == 'false' | ||
uses: repo-sync/pull-request@v2 | ||
with: | ||
source_branch: "staging" | ||
destination_branch: "main" | ||
pr_title: "Merge staging into main" | ||
pr_body: "This PR was automatically created to merge changes from staging into main." | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Update Pull Request | ||
if: steps.check_pr.outputs.result == 'true' | ||
uses: actions/github-script@v6 | ||
with: | ||
github-token: ${{secrets.GITHUB_TOKEN}} | ||
script: | | ||
const { data: pullRequests } = await github.rest.pulls.list({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
state: 'open', | ||
head: 'staging', | ||
base: 'main' | ||
}); | ||
if (pullRequests.length > 0) { | ||
const prNumber = pullRequests[0].number; | ||
await github.rest.pulls.update({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
pull_number: prNumber, | ||
body: 'This PR has been automatically updated with the latest changes from staging.' | ||
}); | ||
console.log(`Updated PR #${prNumber}`); | ||
} |
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,68 @@ | ||
import os | ||
from docetl.utils import load_config | ||
from typing import Any, Dict, List, Optional, Tuple, Union | ||
from docetl.operations.utils import APIWrapper | ||
from rich.console import Console | ||
import pyrate_limiter | ||
from inspect import isawaitable | ||
import math | ||
|
||
|
||
class BucketCollection(pyrate_limiter.BucketFactory): | ||
def __init__(self, **buckets): | ||
self.clock = pyrate_limiter.TimeClock() | ||
self.buckets = buckets | ||
|
||
def wrap_item(self, name: str, weight: int = 1) -> pyrate_limiter.RateItem: | ||
now = self.clock.now() | ||
|
||
async def wrap_async(): | ||
return pyrate_limiter.RateItem(name, await now, weight=weight) | ||
|
||
def wrap_sync(): | ||
return pyrate_limiter.RateItem(name, now, weight=weight) | ||
|
||
return wrap_async() if isawaitable(now) else wrap_sync() | ||
|
||
def get(self, item: pyrate_limiter.RateItem) -> pyrate_limiter.AbstractBucket: | ||
if item.name not in self.buckets: | ||
return self.buckets["unknown"] | ||
return self.buckets[item.name] | ||
|
||
|
||
class ConfigWrapper(object): | ||
@classmethod | ||
def from_yaml(cls, yaml_file: str, **kwargs): | ||
config = load_config(yaml_file) | ||
return cls(config, **kwargs) | ||
|
||
def __init__(self, config: Dict, max_threads: int = None): | ||
self.config = config | ||
self.default_model = self.config.get("default_model", "gpt-4o-mini") | ||
self.console = Console() | ||
self.max_threads = max_threads or (os.cpu_count() or 1) * 4 | ||
self.status = None | ||
|
||
buckets = { | ||
param: pyrate_limiter.InMemoryBucket( | ||
[ | ||
pyrate_limiter.Rate( | ||
param_limit["count"], | ||
param_limit["per"] | ||
* getattr( | ||
pyrate_limiter.Duration, | ||
param_limit.get("unit", "SECOND").upper(), | ||
), | ||
) | ||
for param_limit in param_limits | ||
] | ||
) | ||
for param, param_limits in self.config.get("rate_limits", {}).items() | ||
} | ||
buckets["unknown"] = pyrate_limiter.InMemoryBucket( | ||
[pyrate_limiter.Rate(math.inf, 1)] | ||
) | ||
bucket_factory = BucketCollection(**buckets) | ||
self.rate_limiter = pyrate_limiter.Limiter(bucket_factory, max_delay=math.inf) | ||
|
||
self.api = APIWrapper(self) |
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
Oops, something went wrong.