Skip to content

Commit

Permalink
add demo
Browse files Browse the repository at this point in the history
  • Loading branch information
RexWzh committed Aug 19, 2024
1 parent 51107d3 commit c111890
Showing 1 changed file with 143 additions and 90 deletions.
233 changes: 143 additions & 90 deletions demo/one-api.ipynb
Original file line number Diff line number Diff line change
@@ -1,77 +1,83 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 7,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 导入变量,或者从环境变量中加载\n",
"from dotenv import load_dotenv\n",
"load_dotenv(override=True)"
"## 鉴权测试"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"设置变量:\n",
"设置 `.env` 或使用环境变量\n",
"\n",
"```bash\n",
"# ONE API URL\n",
"ONE_API_BASE_URL=\n",
"ONE_API_URL=\n",
"# ACCESS TOKEN at https://{one-api-url}/panel/profile\n",
"ONE_API_ACCESS_TOKEN=\n",
"```"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## 渠道管理"
"# 导入变量,或者从环境变量中加载\n",
"from dotenv import load_dotenv\n",
"load_dotenv(override=True)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from one_api_cli import Channel"
"import one_api_cli\n",
"from one_api_cli import OneAPI"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"23 perry_bailey\n",
"22 margarita_caban\n",
"21 jessica_hamilton\n"
"True\n",
"v0.6.8-alpha.2\n"
]
}
],
"source": [
"# 查看渠道\n",
"channels = Channel.get_channels()\n",
"for channel in channels[:3]:\n",
" print(channel.id, channel.name)"
"auth = OneAPI()\n",
"print(auth.is_valid())\n",
"print(auth.status()['data']['version'])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 渠道管理"
]
},
{
Expand All @@ -82,48 +88,90 @@
{
"data": {
"text/plain": [
"True"
"(Channel(25, john_olive), Channel(25, john_olive))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from one_api_cli import OneAPI, Channel\n",
"# 获取渠道及最新渠道\n",
"auth = OneAPI()\n",
"all_chs = auth.get_channels() # 所有渠道\n",
"ch = auth.latest_channel() # 最新渠道\n",
"ch, Channel(25)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Channel(28, test_channel)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 新增渠道\n",
"name = \"test_channel\"\n",
"Channel.create(name=name, key='sk-123', base_url = 'https://api.openai.com', models='gpt-test')"
"newch = auth.create_channel(name=name, key='sk-123', base_url = 'https://api.openai.com', models='gpt-test')\n",
"newch"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {},
"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"
]
"data": {
"text/plain": [
"{'id': 28,\n",
" 'type': 1,\n",
" 'key': '',\n",
" 'status': 1,\n",
" 'name': 'new_channel',\n",
" 'weight': 0,\n",
" 'created_time': 1724056380,\n",
" 'test_time': 0,\n",
" 'response_time': 0,\n",
" 'base_url': 'https://api.openai.com',\n",
" 'other': '',\n",
" 'balance': 0,\n",
" 'balance_updated_time': 0,\n",
" 'models': 'gpt-3.5-turbo',\n",
" 'group': 'default',\n",
" 'used_quota': 0,\n",
" 'model_mapping': '',\n",
" 'priority': 0,\n",
" 'config': '{}'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 查看刚刚创建的渠道\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())"
"newch.update(name=new_name, models='gpt-3.5-turbo')\n",
"newch.data"
]
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -132,14 +180,14 @@
"True"
]
},
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 删除频道\n",
"new_channel.delete(confim=False)"
"newch.delete(confim=False)"
]
},
{
Expand All @@ -151,95 +199,99 @@
},
{
"cell_type": "code",
"execution_count": 8,
"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 User, OneAPI\n",
"auth = OneAPI()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9 test2\n",
"1 rexwang\n",
"1 rexwang\n"
]
"data": {
"text/plain": [
"[User(11, test_user2), User(10, test_user), User(1, rexwang)]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 查看用户\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'])"
"users = auth.get_users()\n",
"users"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 3,
"metadata": {},
"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"
"\u001b[32m2024-08-19 16:43:54.460\u001b[0m | \u001b[31m\u001b[1mERROR \u001b[0m | \u001b[36mone_api_cli.auth\u001b[0m:\u001b[36mcreate_user\u001b[0m:\u001b[36m168\u001b[0m - \u001b[31m\u001b[1m输入不合法 Key: 'User.Password' Error:Field validation for 'Password' failed on the 'min' tag\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 新增用户\n",
"username = \"test2\"\n",
"display_name = \"test\"\n",
"password = \"complicated_password\"\n",
"create_user(username, display_name, password)"
"auth.create_user(username='test', display_name='test', password='123')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
"User(12, test)"
]
},
"execution_count": 12,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"auth.create_user(username='test', display_name='test', password='complicated_password')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'id': 12, 'username': 'test', 'password': '', 'display_name': 'test', 'role': 1, 'status': 1, 'email': '', 'github_id': '', 'wechat_id': '', 'lark_id': '', 'verification_code': '', 'access_token': '56696611a00c4442ae0b894bb3bb7cc0', 'quota': 0, 'used_quota': 0, 'request_count': 0, 'group': 'default', 'aff_code': 'cH8k', 'inviter_id': 0}\n",
"{'id': 12, 'username': 'test', 'password': 'sipmle_password', 'display_name': 'test2', 'role': 1, 'status': 1, 'email': '', 'github_id': '', 'wechat_id': '', 'lark_id': '', 'verification_code': '', 'access_token': '56696611a00c4442ae0b894bb3bb7cc0', 'quota': 0, 'used_quota': 0, 'request_count': 0, 'group': 'default', 'aff_code': 'cH8k', 'inviter_id': 0}\n"
]
}
],
"source": [
"# 修改用户信息\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)"
"user = User('test')\n",
"print(user.data)\n",
"user.update(display_name='test2', password='sipmle_password')\n",
"print(user.data)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -248,14 +300,15 @@
"True"
]
},
"execution_count": 13,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 删除用户\n",
"delete_user(username) # 或使用 ID"
"user.delete(confim=False)\n",
"# auth.delete_user('test', confim=False)"
]
}
],
Expand Down

0 comments on commit c111890

Please sign in to comment.