Skip to content

Commit

Permalink
add main scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
RexWzh committed Aug 4, 2024
1 parent 4bc079b commit 03f8a36
Show file tree
Hide file tree
Showing 12 changed files with 577 additions and 59 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# ONE API URL
ONE_API_BASE_URL=

# ACCESS TOKEN at https://{one-api-url}/panel/profile
ONE_API_ACCESS_TOKEN=

11 changes: 4 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,10 @@ jobs:
python -m pip install --upgrade pip
python -m pip install .[test]
python -m pip install -r requirements_dev.txt
- name: Test with pytest and coverage
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_BASE_URL: ${{ secrets.OPENAI_API_BASE_URL }}
run: |
pip install coverage
coverage run -m pytest tests/
# - name: Test with pytest and coverage
# run: |
# pip install coverage
# coverage run -m pytest tests/
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,24 @@
</a>
</div>

# Installation
A Python CLI for the [One API](https://github.com/songquanpeng/one-api) project.

## Setup

Install the package using pip:

```bash
pip install one-api-cli
```

Setup environment variables:

```bash
export ONE_API_BASE_URL=https://your_base_url
export ONE_API_ACCESS_TOKEN=your_access_token # Optional
export ONE_API_SESSION_TOKEN=your_session_token # Optional
```

## Usage

See examples in [Jupyter notebooks](demo/one-api.ipynb).
171 changes: 171 additions & 0 deletions demo/one-api.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 导入变量,或者从环境变量中加载\n",
"from dotenv import load_dotenv\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 用户管理"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from one_api_cli import get_users, get_user, update_user, delete_user, create_user"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 查看用户\n",
"users = get_users()\n",
"for user in users:\n",
" print(user['id'], user['username'])\n",
"print(get_user(1))\n",
"print(get_user(100)) # 不存在的用户"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 新增用户\n",
"username = \"test2\"\n",
"display_name = \"test\"\n",
"password = \"complicated_password\"\n",
"create_user(username, display_name, password)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 修改用户信息\n",
"username = \"test2\"\n",
"new_password = \"new_password_233\"\n",
"update_user(username, password=new_password) # 也可以指定 ID"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 删除用户\n",
"delete_user(username) # 或使用 ID"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 渠道管理"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from one_api_cli import get_channels, get_channel, create_channel, update_channel, delete_channel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 查看频道\n",
"channels = get_channels()\n",
"for channel in channels:\n",
" print(channel['id'], channel['name'])\n",
"print(get_channel(1))\n",
"print(get_channel(100)) # 不存在的频道"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 新增频道\n",
"name = \"test_channel\"\n",
"create_channel(name, 'sk-123', 'https://api.openai.com', 'gpt-test')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 修改频道信息\n",
"channel_id = [channel['id'] for channel in channels if channel['name'] == name][0]\n",
"new_name = \"new_channel\"\n",
"update_channel(channel_id, name=new_name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 删除频道\n",
"delete_channel(channel_id)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
54 changes: 54 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[project]
name = "one-api-cli"
version = "0.1.0"
authors = [
{ name="Rex Wang", email="[email protected]" },
]
description = "A CLI tool for the One API project."
keywords = ["oneapi", "cli", "tool"]
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.9,<3.12"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: MacOS",
"Operating System :: POSIX :: Linux",
]
dependencies = [
"loguru",
"click",
"requests"
]

[project.optional-dependencies]
dev = [
"ipython",
"notebook",
]
test = [
"pytest",
]
docs = [
"sphinx",
"sphinx-rtd-theme",
]
lint = [
"mypy",
]
all = [
"ipython",
"notebook",
"pytest",
"sphinx",
"sphinx-rtd-theme",
"mypy",
]

[project.urls]
"Bug Tracker" = "https://github.com/rexwzh/one-api-cli/issues"

[tool.pytest.ini_options]
addopts = [
"--import-mode=importlib",
]
3 changes: 1 addition & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
pip==19.2.3
bump2version==0.5.11
wheel==0.33.6
watchdog==0.9.0
flake8==3.7.8
tox==3.14.0
coverage==4.5.4
coverage
Sphinx==1.8.5
twine==1.14.0
pytest==6.2.4
44 changes: 0 additions & 44 deletions setup.py

This file was deleted.

5 changes: 0 additions & 5 deletions src/one-api-cli/__init__.py

This file was deleted.

8 changes: 8 additions & 0 deletions src/one_api_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""Top-level package for one-api-cli."""

__author__ = """Rex Wang"""
__email__ = '[email protected]'
__version__ = '0.1.0'

from .account import get_users, update_user, get_user, create_user, delete_user
from .channel import get_channels, update_channel, delete_channel, create_channel, get_channel
Loading

0 comments on commit 03f8a36

Please sign in to comment.