Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
db_gpt
Project
Project
Details
Activity
Releases
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
linyangyang
db_gpt
Commits
b2a61834
Commit
b2a61834
authored
Sep 10, 2024
by
于飞
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
敏感词拦截
parent
7b5c5015
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
2 deletions
+26
-2
api_v1.py
dbgpt/app/openapi/api_v1/api_v1.py
+2
-2
api_v2.py
dbgpt/app/openapi/api_v2.py
+24
-0
No files found.
dbgpt/app/openapi/api_v1/api_v1.py
View file @
b2a61834
...
...
@@ -472,7 +472,7 @@ async def no_stream_generator(chat):
msg
=
await
chat
.
nostream_call
()
yield
f
"data: {msg}
\n\n
"
async
def
stream_call_sensitive_run_one
(
self
):
async
def
stream_call_sensitive_run_one
():
"""
只运行一次
判断出用户输入有敏感词的时候,不需要调用大模型
...
...
@@ -489,7 +489,7 @@ async def stream_generator_sensitive(
当用户输入有敏感词的时候,统一回答
"""
span
=
root_tracer
.
start_span
(
"stream_generator_sensitive"
)
async
for
_
in
stream_call_sensitive_run_one
(
1
):
async
for
_
in
stream_call_sensitive_run_one
():
if
incremental
:
yield
f
"data: {dfa_result}
\n\n
"
else
:
...
...
dbgpt/app/openapi/api_v2.py
View file @
b2a61834
...
...
@@ -16,6 +16,7 @@ from dbgpt.app.openapi.api_v1.api_v1 import (
get_chat_instance
,
get_executor
,
stream_generator
,
stream_generator_sensitive
,
)
from
dbgpt.app.scene
import
BaseChat
,
ChatScene
from
dbgpt.client.schema
import
ChatCompletionRequestBody
,
ChatMode
...
...
@@ -38,6 +39,10 @@ from dbgpt.serve.flow.service.service import Service as FlowService
from
dbgpt.util.executor_utils
import
blocking_func_to_async
from
dbgpt.util.tracer
import
SpanType
,
root_tracer
from
dbgpt.app.apps.utils.filter
import
mydfafiter
from
dbgpt.app.apps.vadmin.auth.utils.current
import
AllUserAuth
,
FullAdminAuth
,
OpenAuth
from
dbgpt.app.apps.vadmin.auth.utils.validation.auth
import
Auth
router
=
APIRouter
()
api_settings
=
APISettings
()
get_bearer_token
=
HTTPBearer
(
auto_error
=
False
)
...
...
@@ -74,6 +79,7 @@ async def check_api_key(
@
router
.
post
(
"/v2/chat/completions"
,
dependencies
=
[
Depends
(
check_api_key
)])
async
def
chat_completions
(
request
:
ChatCompletionRequestBody
=
Body
(),
auth
:
Auth
=
Depends
(
OpenAuth
()),
):
"""Chat V2 completions
Args:
...
...
@@ -91,6 +97,24 @@ async def chat_completions(
"Connection"
:
"keep-alive"
,
"Transfer-Encoding"
:
"chunked"
,
}
# 从数据库中加载 并且初始化敏感词-->到内存中
await
mydfafiter
.
parse_from_db
(
auth
.
db
)
# 先判断敏感词
dfa_result
,
is_sensitive
,
matched_sensitives
=
mydfafiter
.
filter
(
request
.
user_input
,
"*"
)
print
(
f
"--->敏感词替换后的问题:{dfa_result}"
)
print
(
f
"--->匹配到的敏感词列表:{matched_sensitives}"
)
if
is_sensitive
:
print
(
'用户输入有敏感词'
)
result
=
f
"非常抱歉,您的提问中包含了部分敏感词汇:{matched_sensitives},我作为AI,在回答时需遵守相关法律法规和政策,我们系统对特定敏感内容进行了限制。请您尝试调整或重新组织您的问题,避开这些敏感点,我将非常乐意为您提供准确且有帮助的回答。"
return
StreamingResponse
(
stream_generator_sensitive
(
request
.
incremental
,
result
),
headers
=
headers
,
media_type
=
"text/plain"
,
)
# check chat request
check_chat_request
(
request
)
if
request
.
conv_uid
is
None
:
...
...
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