Skip to content

Commit

Permalink
update demo
Browse files Browse the repository at this point in the history
  • Loading branch information
RexWzh committed Aug 10, 2024
1 parent 57c87c4 commit 42d4e9e
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 223 deletions.
235 changes: 177 additions & 58 deletions demo/one-api.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 导入变量,或者从环境变量中加载\n",
"from dotenv import load_dotenv\n",
Expand All @@ -15,134 +26,242 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"## 用户管理"
"设置变量:\n",
"\n",
"```bash\n",
"# ONE API URL\n",
"ONE_API_BASE_URL=\n",
"# ACCESS TOKEN at https://{one-api-url}/panel/profile\n",
"ONE_API_ACCESS_TOKEN=\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 渠道管理"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from one_api_cli import get_users, get_user, update_user, delete_user, create_user"
"from one_api_cli import Channel"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"23 perry_bailey\n",
"22 margarita_caban\n",
"21 jessica_hamilton\n"
]
}
],
"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)) # 不存在的用户"
"# 查看渠道\n",
"channels = Channel.get_channels()\n",
"for channel in channels[:3]:\n",
" print(channel.id, channel.name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 新增用户\n",
"username = \"test2\"\n",
"display_name = \"test\"\n",
"password = \"complicated_password\"\n",
"create_user(username, display_name, password)"
"# 新增渠道\n",
"name = \"test_channel\"\n",
"Channel.create(name=name, key='sk-123', base_url = 'https://api.openai.com', models='gpt-test')"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'id': 24, 'type': 1, 'key': '', 'status': 1, 'name': 'test_channel', 'weight': 0, 'created_time': 1723269632, 'test_time': 0, 'response_time': 0, 'base_url': 'https://api.openai.com', 'other': '', 'balance': 0, 'balance_updated_time': 0, 'models': 'gpt-test', 'group': 'default', 'used_quota': 0, 'model_mapping': '', 'priority': 0, 'config': '{}'}\n",
"{'id': 24, 'type': 1, 'key': '', 'status': 1, 'name': 'new_channel', 'weight': 0, 'created_time': 1723269632, 'test_time': 0, 'response_time': 0, 'base_url': 'https://api.openai.com', 'other': '', 'balance': 0, 'balance_updated_time': 0, 'models': 'gpt-test', 'group': 'default', 'used_quota': 0, 'model_mapping': '', 'priority': 0, 'config': '{}'}\n"
]
}
],
"source": [
"# 修改用户信息\n",
"username = \"test2\"\n",
"new_password = \"new_password_233\"\n",
"update_user(username, password=new_password) # 也可以指定 ID"
"# 查看刚刚创建的渠道\n",
"new_channel = Channel.get_channels(page=0)[0]\n",
"print(new_channel.dumps())\n",
"\n",
"# 修改渠道\n",
"new_name = \"new_channel\"\n",
"new_channel.update(name=new_name)\n",
"print(new_channel.dumps())"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 删除用户\n",
"delete_user(username) # 或使用 ID"
"# 删除频道\n",
"new_channel.delete(confim=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 渠道管理"
"## 用户管理"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from one_api_cli import get_channels, get_channel, create_channel, update_channel, delete_channel"
"from one_api_cli import get_users, get_user, update_user, delete_user, create_user"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9 test2\n",
"1 rexwang\n",
"1 rexwang\n"
]
}
],
"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)) # 不存在的频道"
"# 查看用户\n",
"users = get_users()\n",
"for user in users:\n",
" print(user['id'], user['username'])\n",
"user = get_user(1)\n",
"print(user['id'], user['username'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m2024-08-10 13:54:04.080\u001b[0m | \u001b[31m\u001b[1mERROR \u001b[0m | \u001b[36mone_api_cli.account\u001b[0m:\u001b[36mcreate_user\u001b[0m:\u001b[36m82\u001b[0m - \u001b[31m\u001b[1mUNIQUE constraint failed: users.username\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 新增频道\n",
"name = \"test_channel\"\n",
"create_channel(name, 'sk-123', 'https://api.openai.com', 'gpt-test')"
"# 新增用户\n",
"username = \"test2\"\n",
"display_name = \"test\"\n",
"password = \"complicated_password\"\n",
"create_user(username, display_name, password)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"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)"
"# 修改用户信息\n",
"username = \"test2\"\n",
"userid = [user['id'] for user in users if user['username'] == username][0]\n",
"new_password = \"new_password_233\"\n",
"update_user(userid, password=new_password)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 删除频道\n",
"delete_channel(channel_id)"
"# 删除用户\n",
"delete_user(username) # 或使用 ID"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -160,5 +279,5 @@
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}
9 changes: 0 additions & 9 deletions setup.cfg

This file was deleted.

2 changes: 1 addition & 1 deletion src/one_api_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
__version__ = '0.2.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
from .channel import Channel
Loading

0 comments on commit 42d4e9e

Please sign in to comment.