Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yunheBot
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
范立洲
yunheBot
Commits
d972c690
Commit
d972c690
authored
Apr 25, 2023
by
范立洲
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: add yunheBot.server
parent
229bbccf
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
87 行增加
和
71 行删除
+87
-71
Dockerfile
+13
-12
src/yunhebot/api/v1/completion.py
+34
-1
src/yunhebot/core/common.py
+29
-0
src/yunhebot/core/intent.py
+6
-29
src/yunhebot/core/qa.py
+5
-29
没有找到文件。
Dockerfile
查看文件 @
d972c690
FROM
python:3.9-slim AS compile-image
FROM
python:3.9-slim AS compile-image
RUN
apt-get upgrade &
\
WORKDIR
/yunheBot
apt-get install
-y
--no-install-recommends
build-essential gcc &
\
COPY
. .
python
-m
venv /opt/venv
RUN
apt-get update
&&
\
ENV
PATH="/opt/venv/bin:$PATH"
apt-get install
-y
--no-install-recommends
git
&&
\
COPY
requirements.txt .
apt-get purge
-y
--auto-remove
&&
\
RUN
pip install
-r
requirements.txt
rm
-rf
/var/lib/apt/lists/
*
&&
\
pip install
-r
requirements.txt
RUN
tox
-e
build
FROM
python:3.9-slim AS build-image
FROM
python:3.9-slim
COPY
--from=compile-image /opt/venv /opt/venv
WORKDIR
/yunheBot
ENV
PATH="/opt/venv/bin:$PATH" FLASK_ENV="PRD"
COPY
--from=compile-image /yunheBot/dist/*.whl /yunheBot/dist/
WORKDIR
/yuheBot
RUN
pip install /yunheBot/dist/
*
.whl
COPY
./src/ .
CMD
["gunicorn","-w","4","-b","0.0.0.0:9000","--log-level=info","--access-logfile","./log/access.log","--error-logfile","./log/error.log","yunheBot.server:app"]
CMD
["gunicorn","-w","4","-b","0.0.0.0:9000","--log-level=info","--access-logfile","./log/access.log","--error-logfile","./log/error.log","wsgi:app"]
src/yunhebot/api/v1/completion.py
查看文件 @
d972c690
...
@@ -56,9 +56,15 @@ class CompletionResource(Resource):
...
@@ -56,9 +56,15 @@ class CompletionResource(Resource):
@validator.validator
(
POST_SCHEMA
)
@validator.validator
(
POST_SCHEMA
)
def
post
(
self
):
def
post
(
self
):
data
=
request
.
get_json
(
force
=
True
)
data
=
request
.
get_json
(
force
=
True
)
# req: Dict = json.loads(data)
req
:
Dict
=
data
req
:
Dict
=
data
# if not req["stream"]:
# return self._completion(req)
# else:
# pass
# 1. intent check
# 1. intent check
intent
=
self
.
intent_bot
.
complete
(
req
[
"prompt"
])
intent
=
self
.
intent_bot
.
complete
(
req
[
"prompt"
])
...
@@ -78,3 +84,30 @@ class CompletionResource(Resource):
...
@@ -78,3 +84,30 @@ class CompletionResource(Resource):
return
response
.
OK
(
""
,
resp
.
to_dict
())
return
response
.
OK
(
""
,
resp
.
to_dict
())
except
Exception
as
e
:
except
Exception
as
e
:
return
response
.
InternalServerError
(
str
(
e
))
return
response
.
InternalServerError
(
str
(
e
))
def
_completion
(
self
,
req
:
Dict
):
# 1. intent check
intent
=
self
.
intent_bot
.
complete
(
req
[
"prompt"
])
# 1.1 intent ==> qa
if
intent
is
not
None
and
intent
.
answer
==
const
.
INTENT_QA
:
try
:
answer
=
self
.
qa_bot
.
complete
(
**
req
)
if
answer
is
None
:
return
response
.
NotFound
(
"No Answer Found"
)
return
response
.
OK
(
"OK"
,
answer
.
to_dict
())
except
Exception
as
e
:
return
response
.
InternalServerError
(
str
(
e
))
# 1.2 intent ==> chatgpt
try
:
resp
=
self
.
chatgpt_bot
.
complete
(
req
[
"prompt"
])
return
response
.
OK
(
""
,
resp
.
to_dict
())
except
Exception
as
e
:
return
response
.
InternalServerError
(
str
(
e
))
def
_stream_completion
(
self
,
req
):
# # 1. intent check
# intent = self.intent_bot.complete(req["prompt"])
pass
src/yunhebot/core/common.py
查看文件 @
d972c690
# coding=utf-8
# coding=utf-8
from
.datasource
import
QDDataSource
class
BaseQDBot
:
def
__init__
(
self
,
data_source
:
QDDataSource
):
self
.
data_source
=
data_source
def
create_collection
(
self
):
"""创建数据集
Args:
size (int): 向量大小
distance (QdrantClient.Distance): 损失函数
"""
try
:
self
.
data_source
.
create_collection
(
collection
=
self
.
collection
)
except
Exception
as
e
:
raise
e
def
upload_csv
(
self
,
filepath
:
str
):
"""导入csv数据集
Args:
filepath: 文件路径
"""
self
.
data_source
.
upload_csv
(
self
.
collection
,
self
.
embdder
,
filepath
,
self
.
formatter
)
class
BotPayload
:
class
BotPayload
:
@classmethod
@classmethod
...
...
src/yunhebot/core/intent.py
查看文件 @
d972c690
...
@@ -4,11 +4,10 @@ from collections import Counter
...
@@ -4,11 +4,10 @@ from collections import Counter
from
typing
import
List
,
Dict
from
typing
import
List
,
Dict
from
.datasource
import
QDDataSource
from
.datasource
import
QDDataSource
from
.embedding
import
Embedding
from
.embedding
import
Embedding
from
.const
import
INTENT_QA
from
.common
import
BotPayload
,
BaseQDBot
from
.common
import
BotPayload
class
IntentBot
:
class
IntentBot
(
BaseQDBot
)
:
def
__init__
(
def
__init__
(
self
,
self
,
data_source
:
QDDataSource
,
data_source
:
QDDataSource
,
...
@@ -17,38 +16,16 @@ class IntentBot:
...
@@ -17,38 +16,16 @@ class IntentBot:
score
:
float
,
score
:
float
,
limit
:
int
,
limit
:
int
,
):
):
self
.
data_source
=
data_source
super
(
IntentBot
,
self
)
.
__init__
(
data_source
)
# self.data_source = data_source
self
.
embdder
=
embedder
self
.
embdder
=
embedder
self
.
collection
=
collection
self
.
collection
=
collection
self
.
score
=
float
(
score
)
self
.
score
=
float
(
score
)
self
.
limit
=
int
(
limit
)
self
.
limit
=
int
(
limit
)
def
create_collection
(
self
):
"""创建数据集
Args:
size (int): 向量大小
distance (QdrantClient.Distance): 损失函数
"""
try
:
self
.
data_source
.
create_collection
(
collection
=
self
.
collection
)
except
Exception
as
e
:
raise
e
def
formatter
(
self
,
line
:
List
)
->
Dict
:
def
formatter
(
self
,
line
:
List
)
->
Dict
:
# line format: 问题,答案(意图)
# line format: 问题,答案, type, 意图
# return {"question": line[0], "answer": INTENT_QA}
return
BotPayload
(
question
=
line
[
0
],
answer
=
int
(
line
[
3
]))
.
to_dict
()
return
BotPayload
(
question
=
line
[
0
],
answer
=
INTENT_QA
)
.
to_dict
()
def
upload_csv
(
self
,
filepath
:
str
):
"""导入csv数据集
Args:
filepath: 文件路径
"""
self
.
data_source
.
upload_csv
(
self
.
collection
,
self
.
embdder
,
filepath
,
self
.
formatter
)
def
complete
(
self
,
prompt
:
str
)
->
BotPayload
:
def
complete
(
self
,
prompt
:
str
)
->
BotPayload
:
points
=
self
.
data_source
.
search
(
points
=
self
.
data_source
.
search
(
...
...
src/yunhebot/core/qa.py
查看文件 @
d972c690
...
@@ -3,10 +3,10 @@
...
@@ -3,10 +3,10 @@
from
typing
import
List
,
Dict
from
typing
import
List
,
Dict
from
.datasource
import
QDDataSource
from
.datasource
import
QDDataSource
from
.embedding
import
Embedding
from
.embedding
import
Embedding
from
.common
import
BotPayload
from
.common
import
BotPayload
,
BaseQDBot
class
QABot
:
class
QABot
(
BaseQDBot
)
:
def
__init__
(
def
__init__
(
self
,
self
,
data_source
:
QDDataSource
,
data_source
:
QDDataSource
,
...
@@ -15,39 +15,15 @@ class QABot:
...
@@ -15,39 +15,15 @@ class QABot:
score
:
int
,
score
:
int
,
limit
:
int
,
limit
:
int
,
):
):
s
elf
.
data_source
=
data_source
s
uper
(
QABot
,
self
)
.
__init__
(
data_source
)
self
.
embdder
=
embedder
self
.
embdder
=
embedder
self
.
collection
=
collection
self
.
collection
=
collection
self
.
score
=
float
(
score
)
self
.
score
=
float
(
score
)
self
.
limit
=
int
(
limit
)
self
.
limit
=
int
(
limit
)
def
create_collection
(
self
):
"""创建数据集
Args:
size (int): 向量大小
distance (QdrantClient.Distance): 损失函数
"""
try
:
self
.
data_source
.
create_collection
(
collection
=
self
.
collection
)
except
Exception
as
e
:
raise
e
def
formatter
(
self
,
line
:
List
)
->
Dict
:
def
formatter
(
self
,
line
:
List
)
->
Dict
:
# line format: 问题,答案,跳转类型
# line format: 问题,答案, type, 意图
# return {"question": line[0], "answer": line[1], "type": line[2]}
return
BotPayload
(
question
=
line
[
0
],
answer
=
line
[
1
],
type
=
line
[
2
])
.
to_dict
()
# return {"question": line[0], "answer": line[1], "type": "demo"}
return
BotPayload
(
question
=
line
[
0
],
answer
=
line
[
1
],
type
=
"demo"
)
.
to_dict
()
def
upload_csv
(
self
,
filepath
:
str
):
"""导入csv数据集
Args:
filepath: 文件路径
"""
self
.
data_source
.
upload_csv
(
self
.
collection
,
self
.
embdder
,
filepath
,
self
.
formatter
)
def
complete
(
self
,
prompt
:
str
,
score
:
int
=
0
,
limit
:
int
=
0
)
->
BotPayload
:
def
complete
(
self
,
prompt
:
str
,
score
:
int
=
0
,
limit
:
int
=
0
)
->
BotPayload
:
_limit
=
self
.
limit
if
limit
==
0
else
limit
_limit
=
self
.
limit
if
limit
==
0
else
limit
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论