Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add python version #1457

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions team-sharing-py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
59 changes: 59 additions & 0 deletions team-sharing-py/README-TW.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Chatbox: 團隊共享 OpenAI API 資源的解決方案
[English](./README.md) | [中文介紹](./README_zh.md)

Chatbox 能夠讓你的團隊成員在共享同一個 OpenAI API 賬號資源的同時,保護你的 API KEY 不被暴露。這個 Python 工具不僅簡單地轉發請求,還能記錄團隊成員的發送紀錄,控制高消費(如圖片生成)等功能,並設置每日調用上限,有效防止團隊中濫用的情況。

## 使用說明

### 1. 搭建 MySQL 數據庫
首先需要在本地搭建 MySQL 數據庫,並創建以下數據表:

```sql
CREATE TABLE user_queries (
id INT AUTO_INCREMENT PRIMARY KEY,
user_ip VARCHAR(255) NOT NULL,
model_name VARCHAR(255) NOT NULL,
query TEXT NOT NULL,
datetime DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE ip_model_usage (
user_ip VARCHAR(15) NOT NULL,
model_name VARCHAR(50) NOT NULL,
usage_count INT NOT NULL DEFAULT '0',
datetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_ip, model_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```

### 2. 啟用數據庫事件
在 MySQL 配置文件中啟用事件調度器:

```ini
[mysqld]
event_scheduler = ON
```

### 3. 設置每日清空使用紀錄
創建一個每日清空 `ip_model_usage` 表的事件:

```sql
CREATE EVENT IF NOT EXISTS clear_ip_model_usage
ON SCHEDULE EVERY 1 DAY STARTS TIMESTAMP(CURRENT_DATE, '00:00:00')
DO
TRUNCATE TABLE chatbox_server.ip_model_usage;
```

### 4. 配置文件
所有相關設置都在 `config.py` 文件中。你可以使用環境變數來替換 API_KEY,例如:

```bash
export API_KEY="sk-xxxx"
```

### 5. 安裝所需套件
運行以下命令來安裝所需的 Python 套件:

```bash
pip install -r requirements.txt
```
59 changes: 59 additions & 0 deletions team-sharing-py/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Chatbox: Team Shared OpenAI API Resource Solution
[English](./README.md) | [中文介紹](./README_zh.md)

Chatbox allows your team members to share the same OpenAI API account resources while protecting your API KEY from being exposed. This Python tool not only forwards requests but also records the sending history of team members, controls high-cost activities (such as image generation), and sets daily call limits to effectively prevent abuse within the team.

## Instructions

### 1. Setup MySQL Database
First, set up a MySQL database locally and create the following tables:

```sql
CREATE TABLE user_queries (
id INT AUTO_INCREMENT PRIMARY KEY,
user_ip VARCHAR(255) NOT NULL,
model_name VARCHAR(255) NOT NULL,
query TEXT NOT NULL,
datetime DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE ip_model_usage (
user_ip VARCHAR(15) NOT NULL,
model_name VARCHAR(50) NOT NULL,
usage_count INT NOT NULL DEFAULT '0',
datetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_ip, model_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```

### 2. Enable Database Events
Enable the event scheduler in the MySQL configuration file:

```ini
[mysqld]
event_scheduler = ON
```

### 3. Set Up Daily Usage Record Clearing
Create an event to clear the `ip_model_usage` table daily:

```sql
CREATE EVENT IF NOT EXISTS clear_ip_model_usage
ON SCHEDULE EVERY 1 DAY STARTS TIMESTAMP(CURRENT_DATE, '00:00:00')
DO
TRUNCATE TABLE chatbox_server.ip_model_usage;
```

### 4. Configuration File
All related settings are in the `config.py` file. You can use environment variables to replace the API_KEY, for example:

```bash
export API_KEY="sk-xxxx"
```

### 5. Install Required Packages
Run the following command to install the required Python packages:

```bash
pip install -r requirements.txt
```
23 changes: 23 additions & 0 deletions team-sharing-py/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 允許的模型及單日使用次數
ALLOW_MODEL = {
"gpt-4o": 30,
"gpt-4o-mini": 100
}

# 資料庫連接設定
DB_CONFIG = {
'host': '127.0.0.1',
'port': 3306,
'user': 'chatbox_server',
'password': 'xxxxxxxx',
'db': 'chatbox_server',
}

# API端點
API_URL = "https://api.openai.com/v1/chat/completions"

# API金鑰
CONFIG_API_KEY = None

# 禁用非文字功能
DISABLE_NON_TEXT = True
Loading