Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
open_ai_api
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
haojie
open_ai_api
Commits
0b0b41b0
Commit
0b0b41b0
authored
Apr 13, 2023
by
wangfa
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加api 发送
parent
5e2a1b48
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
269 additions
and
45 deletions
+269
-45
.env.example
+3
-0
app/Admin/Metrics/Examples/NewDevices.php
+1
-0
app/Http/Controllers/Api/User/ConfigController.php
+6
-5
app/Http/Controllers/Api/User/TaskController.php
+20
-0
app/Jobs/SendImagineJob.php
+28
-0
app/Service/AdminConfigService.php
+3
-2
app/Service/ExternalApiService.php
+98
-0
app/Service/TaskService.php
+99
-37
config/common.php
+9
-0
routes/api.php
+2
-1
No files found.
.env.example
View file @
0b0b41b0
...
...
@@ -56,3 +56,6 @@ VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
PYTHON_API=
app/Admin/Metrics/Examples/NewDevices.php
View file @
0b0b41b0
...
...
@@ -16,6 +16,7 @@ protected function init()
{
parent
::
init
();
$color
=
Admin
::
color
();
$colors
=
[
$color
->
primary
(),
$color
->
alpha
(
'blue2'
,
0.5
)];
...
...
app/Http/Controllers/Api/User/ConfigController.php
View file @
0b0b41b0
...
...
@@ -28,19 +28,20 @@ public function show(Request $request)
/**
* 上传策略
*
* @param \Illuminate\Http\Request $request
* @return mixed
*/
public
function
policy
(
Request
$request
)
public
function
policy
()
{
$user
=
$request
->
user
();
$module
=
$request
->
input
(
'module'
,
'user'
);
# 区分图片上传路径和视频上传路径
# 10M
$size
=
1024
*
1024
*
5
;
$module
=
'ai_avatar'
;
$ossConfig
=
app
(
AdminConfigService
::
class
)
->
getAdminUserOssConfig
();
$result
=
(
new
OssService
(
$ossConfig
))
->
policy
(
'files/'
.
$module
.
'/'
.
'admin'
.
'/'
,
$size
);
$result
=
[];
if
(
filled
(
$ossConfig
))
{
$result
=
(
new
OssService
(
$ossConfig
))
->
policy
(
'files/'
.
$module
.
'/'
.
'admin'
.
'/'
,
$size
);
}
return
$this
->
success
(
'success'
,
$result
);
}
}
app/Http/Controllers/Api/User/TaskController.php
View file @
0b0b41b0
...
...
@@ -9,6 +9,12 @@
class
TaskController
extends
Controller
{
/**
* 提交任务
*
* @param Request $request
* @return mixed
*/
public
function
SubmitTask
(
Request
$request
)
{
$data
=
$request
->
input
();
...
...
@@ -18,6 +24,20 @@ public function SubmitTask(Request $request)
}
/**
* api 提交任务
*
* @param Request $request
* @return mixed
*/
public
function
apiSubmit
(
Request
$request
)
{
$data
=
$request
->
input
();
$user
=
$request
->
user
();
$result
=
app
(
TaskService
::
class
)
->
apiSubmit
(
$user
->
id
,
$data
);
return
$this
->
success
(
'success'
,
$result
);
}
/**
* python-gpt任务回调
*/
public
function
gpt_callback
(
Request
$request
)
...
...
app/Jobs/SendImagineJob.php
0 → 100644
View file @
0b0b41b0
<?php
namespace
App\Jobs
;
use
App\Service\ExternalApiService
;
use
Illuminate\Bus\Queueable
;
use
Illuminate\Contracts\Queue\ShouldQueue
;
use
Illuminate\Foundation\Bus\Dispatchable
;
use
Illuminate\Queue\InteractsWithQueue
;
use
Illuminate\Queue\SerializesModels
;
class
SendImagineJob
implements
ShouldQueue
{
use
Dispatchable
,
InteractsWithQueue
,
Queueable
,
SerializesModels
;
public
array
$data
=
[];
public
function
__construct
(
array
$data
=
[])
{
$this
->
queue
=
"
{
send-imagine
}
"
;
$this
->
data
=
$data
;
}
public
function
handle
()
{
app
(
ExternalApiService
::
class
)
->
imagine
(
$this
->
data
);
}
}
app/Service/AdminConfigService.php
View file @
0b0b41b0
...
...
@@ -4,7 +4,6 @@
use
App\Models\AdminConfig
;
use
Illuminate\Support\Arr
;
use
Illuminate\Support\Facades\Log
;
class
AdminConfigService
{
...
...
@@ -24,7 +23,9 @@ class AdminConfigService
*/
public
function
getAdminUserOssConfig
()
{
$config
=
AdminConfig
::
where
(
'type'
,
self
::
TYPE_DEFAULT
)
->
where
(
'status'
,
self
::
STATUS_OPEN
)
$oldConfig
=
[];
$config
=
AdminConfig
::
where
(
'type'
,
self
::
TYPE_DEFAULT
)
->
where
(
'status'
,
self
::
STATUS_OPEN
)
->
first
();
if
(
filled
(
$config
))
{
$oldConfig
=
$config
->
config
;
...
...
app/Service/ExternalApiService.php
0 → 100644
View file @
0b0b41b0
<?php
namespace
App\Service
;
use
GuzzleHttp\Client
;
class
ExternalApiService
{
public
string
$domain
;
public
function
__construct
(
string
$domain
=
''
)
{
$this
->
setDomain
(
$domain
);
}
/**
* 执行生成图片命令
*
* @param array $data
* @return array|mixed
*/
public
function
imagine
(
array
$data
=
[])
{
$url
=
$this
->
getPath
(
'/api/imagine'
);
return
$this
->
httpPost
(
$url
,
$data
);
}
/**
* @param string $path
* @return string
*/
public
function
getPath
(
string
$path
=
''
)
{
return
$this
->
getDomain
()
.
$path
;
}
/**
*
* @return string
*/
public
function
getDomain
()
{
return
$this
->
domain
;
}
/**
* @param string $domain
*/
public
function
setDomain
(
string
$domain
=
''
)
{
$this
->
domain
=
$domain
;
if
(
blank
(
$domain
))
{
$this
->
domain
=
config
(
'common.python_api'
);
}
}
/**
* 发送post请求
*
* @param string $url
* @param array $data
* @return array|mixed
*/
public
function
httpPost
(
string
$url
=
''
,
array
$data
=
[])
{
$requestData
=
[
'headers'
=>
[
'Content-Type'
=>
'application/json'
],
'json'
=>
[
'data'
=>
$data
],
];
$client
=
new
Client
();
try
{
$response
=
$client
->
post
(
$url
,
$requestData
);
$data
=
json_decode
(
$response
->
getBody
()
->
getContents
(),
true
);
return
$data
??
[];
}
catch
(
\Throwable
$throwable
)
{
return
[];
}
}
/**
* 下载图片
*
* @param array $data
* @return array|mixed
*/
public
function
downloadImage
(
array
$data
=
[])
{
$url
=
$this
->
getPath
(
'/api/download'
);
return
$this
->
httpPost
(
$url
,
$data
);
}
}
app/Service/TaskService.php
View file @
0b0b41b0
...
...
@@ -3,10 +3,10 @@
namespace
App\Service
;
use
App\Exceptions\UserException
;
use
App\Jobs\SendImagineJob
;
use
App\Models\PromptTask
;
use
App\Models\Task
;
use
Illuminate\Support\Arr
;
use
Illuminate\Support\Facades\Log
;
use
Illuminate\Support\Facades\Redis
;
class
TaskService
...
...
@@ -16,25 +16,28 @@ class TaskService
public
const
GPT_prompt_num
=
1
;
// 指令数量
public
function
submit
(
$user_id
,
array
$data
=
[])
/**
* @param array $data
*/
public
function
validateSubmitData
(
array
$data
=
[])
{
# task表新增一条记录
$type
=
$data
[
'type'
]
??
''
;
$prompt
=
$data
[
'prompt'
]
??
''
;
$prompt_img
=
$data
[
'prompt_img'
]
??
[];
$prompt_num
=
$data
[
'prompt_num'
]
??
''
;
$promptImg
=
$data
[
'prompt_img'
]
??
[];
$promptNum
=
$data
[
'prompt_num'
]
??
''
;
if
(
empty
(
$type
)
||
empty
(
$prompt
))
{
throw
new
UserException
(
'任务类型不能为空'
);
}
# 生成指令数量只能在1-10之间
if
(
$prompt
_num
<
1
||
$prompt_n
um
>
10
)
{
if
(
$prompt
Num
<
1
||
$promptN
um
>
10
)
{
throw
new
UserException
(
'生成指令数量只能在1-10之间'
);
}
# 判断类型
if
(
$type
==
self
::
TYPE_IMG_TO_IMG
)
{
# 图片转图片
if
(
!
count
(
$prompt
_i
mg
))
{
if
(
!
count
(
$prompt
I
mg
))
{
throw
new
UserException
(
'图片不能为空'
);
}
}
elseif
(
$type
==
self
::
TYPE_TEXT_TO_IMG
)
{
...
...
@@ -42,39 +45,101 @@ public function submit($user_id, array $data = [])
}
else
{
throw
new
UserException
(
'任务类型错误'
);
}
$list
=
[
'type'
=>
$type
,
'prompt'
=>
$prompt
,
'prompt_img'
=>
json_encode
(
$prompt_img
),
'user_id'
=>
$user_id
,
'prompt_num'
=>
self
::
GPT_prompt_num
,
];
# 保存任务
$task_id
=
Task
::
query
()
->
create
(
$list
)
->
id
;
# old
# self::setRedis($task_id, $list);
# 改成直接提交到自动化任务队列
$list
=
[
'prompt'
=>
$prompt
,
'user_id'
=>
$user_id
,
'task_id'
=>
$task_id
,
'prompt_img'
=>
json_encode
(
$prompt_img
),
'type'
=>
$type
,
'callback'
=>
config
(
'common.prompt_callback'
),
'policy'
=>
config
(
'common.policy_callback'
),
];
$prompt_id
=
promptTask
::
query
()
->
create
(
$list
)
->
id
;
$list
[
'prompt_id'
]
=
$prompt_id
;
}
/**
* 提交任务
*
* @param int $user_id
* @param array $data
* @return array
*/
public
function
submit
(
int
$user_id
=
0
,
array
$data
=
[])
{
$this
->
validateSubmitData
(
$data
);
$task
=
$this
->
createTask
(
$user_id
,
$data
);
$promptTask
=
$this
->
createPromptTask
(
$user_id
,
$task
->
id
,
$data
);
$list
[
'prompt_id'
]
=
$promptTask
->
id
;
# 插入redis,这是自动化的任务
Redis
::
rpush
(
'midjourney_prompt'
,
json_encode
(
$list
));
# 返回任务id
return
[
'task_id'
=>
$task
_
id
,
'task_id'
=>
$task
->
id
,
'prompt_num'
=>
self
::
GPT_prompt_num
,
];
}
/**
* 创建任务
*
* @param int $userId
* @param array $data
* @return Task
*/
public
function
createTask
(
int
$userId
=
0
,
array
$data
=
[])
{
$taskData
=
[
'type'
=>
(
string
)(
$data
[
'type'
]
??
''
),
'prompt'
=>
(
string
)(
$data
[
'prompt'
]
??
''
),
'prompt_img'
=>
json_encode
(
$data
[
'prompt_img'
]
??
[]),
'user_id'
=>
$userId
,
'prompt_num'
=>
self
::
GPT_prompt_num
,
];
return
Task
::
create
(
$taskData
);
}
/**
* 创建提示任务
*
* @param int $userId
* @param int $taskId
* @param array $data
* @return PromptTask
*/
public
function
createPromptTask
(
int
$userId
=
0
,
int
$taskId
=
0
,
array
$data
=
[])
{
$promptTask
=
[
'prompt'
=>
(
string
)(
$data
[
'prompt'
]
??
''
),
'user_id'
=>
$userId
,
'task_id'
=>
$taskId
,
'prompt_img'
=>
json_encode
(
$data
[
'prompt_img'
]
??
[]),
'type'
=>
(
string
)(
$data
[
'type'
]),
'callback'
=>
config
(
'common.prompt_callback'
),
'policy'
=>
config
(
'common.policy_callback'
),
];
return
PromptTask
::
create
(
$promptTask
);
}
/**
* api 提交任务
*
* @param int $userId
* @param array $data
* @return array
*/
public
function
apiSubmit
(
int
$userId
=
0
,
array
$data
=
[])
{
$this
->
validateSubmitData
(
$data
);
$task
=
$this
->
createTask
(
$userId
,
$data
);
$promptTask
=
$this
->
createPromptTask
(
$userId
,
$task
->
id
,
$data
);
$sendData
=
[
'token'
=>
config
(
'common.imagine_token'
),
'guild_id'
=>
config
(
'common.imagine_guild_id'
),
'channel_id'
=>
config
(
'common.imagine_channel_id'
),
'prompt'
=>
$task
->
prompt
,
'prompt_img'
=>
$task
->
prompt_img
,
'prompt_id'
=>
$promptTask
->
id
,
'task_id'
=>
$task
->
id
,
];
dispatch
(
new
SendImagineJob
(
$sendData
));
return
[
'task_id'
=>
$task
->
id
,
'prompt_num'
=>
$task
->
prompt_num
];
}
/**
* redis队列插入--这是gpt生成指令的任务
*/
...
...
@@ -176,8 +241,7 @@ public function prompt_callback($data = [])
/**
* python - 下载图片完成-回调
*/
public
function
img_download_callback
(
$data
=
[])
public
function
img_download_callback
(
$data
=
[])
{
$credentials
=
Arr
::
only
(
$data
,
[
'prompt_id'
,
'result_img'
,
'message'
]);
# 必须字段
...
...
@@ -214,8 +278,7 @@ function img_download_callback($data = [])
/**
* 轮询接口
*/
public
function
task_callback
(
$user_id
,
$data
=
[])
public
function
task_callback
(
$user_id
,
$data
=
[])
{
$credentials
=
Arr
::
only
(
$data
,
[
'task_id'
]);
# 必须字段
...
...
@@ -242,8 +305,7 @@ function task_callback($user_id, $data = [])
/**
* 创建切割图片任务
*/
public
function
create_split_img_task
(
$user_id
,
$data
=
[])
public
function
create_split_img_task
(
$user_id
,
$data
=
[])
{
$credentials
=
Arr
::
only
(
$data
,
[
'prompt_id'
,
'cut_id'
]);
# 必须字段
...
...
config/common.php
View file @
0b0b41b0
...
...
@@ -12,4 +12,13 @@
'split_img_callback'
=>
env
(
'SPLIT_IMG_CALLBACK'
,
'http://test.phpgpt.com/api/users/split/callback'
),
# 下载图片的回调地址
'img_download_callback'
=>
env
(
'IMG_DOWNLOAD_CALLBACK'
,
'http://test.phpgpt.com/api/users/img/callback'
),
// python API
'python_api'
=>
env
(
'PYTHON_API'
,
'http://127.0.0.1:9001'
),
// 生成图片参数
'imagine_token'
=>
env
(
'IMAGINE_TOKEN'
,
'MTA3NTYwNTUxMjAwMDkwNTI5Nw.GbEb6_.9-H-5kf501rRc0SJQa5mB-2eV1Fh2u4-rWWG_0'
),
'imagine_guild_id'
=>
env
(
'IMAGINE_GUILD_ID'
,
'1095628793860857957'
),
'imagine_channel_id'
=>
env
(
'IMAGINE_CHANNEL_ID'
,
'1095628793860857960'
),
];
routes/api.php
View file @
0b0b41b0
<?php
use
Illuminate\Http\Request
;
use
Illuminate\Support\Facades\Route
;
/*
...
...
@@ -48,6 +47,8 @@
Route
::
get
(
'config/policy'
,
'ConfigController@policy'
);
// 提交任务
Route
::
post
(
'submit'
,
'TaskController@SubmitTask'
);
// Api 提交任务
Route
::
post
(
'api-submit'
,
'TaskController@apiSubmit'
);
# 轮询请求任务回调
Route
::
get
(
'task/callback'
,
'TaskController@task_callback'
);
# 创建切割图片任务
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment