Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
黄南光
/
wiki_knowledge
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 501ade99
authored
Jun 24, 2026
by
黄南光
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
修复wiki回答偶发无回复问题
1 parent
e4759251
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
28 additions
and
15 deletions
.gitignore
src/api/routes/query.py
src/database/models.py
src/utils/llm_tracker.py
src/utils/logger.py
.gitignore
View file @
501ade9
# Claude Code
# Claude Code
AGENTS.md
CLAUDE.md
CLAUDE.md
.claude/
.claude/
.Codex/
# Python
# Python
__pycache__/
__pycache__/
...
@@ -29,4 +31,12 @@ logs/
...
@@ -29,4 +31,12 @@ logs/
GEMINI.md
GEMINI.md
# .env
# .env
.env
\ No newline at end of file
\ No newline at end of file
.env
# ref
/src/ref/
#AgentTool
/CLAUDE.md
/AGENTS.md
/GEMINI.md
src/api/routes/query.py
View file @
501ade9
This diff is collapsed.
Click to expand it.
src/database/models.py
View file @
501ade9
import
uuid
import
uuid
from
datetime
import
datetime
from
datetime
import
datetime
from
sqlalchemy
import
Column
,
Integer
,
String
,
BigInteger
,
Text
,
DateTime
,
Numeric
,
ForeignKey
from
sqlalchemy
import
Column
,
Integer
,
String
,
BigInteger
,
Text
,
DateTime
,
Numeric
,
ForeignKey
,
Index
,
UniqueConstraint
from
sqlalchemy.orm
import
relationship
from
sqlalchemy.orm
import
relationship
from
src.database.session
import
Base
from
src.database.session
import
Base
...
@@ -22,19 +22,19 @@ class WikiFile(Base):
...
@@ -22,19 +22,19 @@ class WikiFile(Base):
updated_at
=
Column
(
DateTime
,
default
=
datetime
.
now
,
onupdate
=
datetime
.
now
)
updated_at
=
Column
(
DateTime
,
default
=
datetime
.
now
,
onupdate
=
datetime
.
now
)
# 关联关系(用于 ORM 级联,数据库层约束由应用层手动管理以保持异步兼容性)
# 关联关系(用于 ORM 级联,数据库层约束由应用层手动管理以保持异步兼容性)
pages
=
relationship
(
"WikiFilePage"
,
back_populates
=
"wiki_file"
,
cascade
=
"all, delete-orphan"
,
lazy
=
"noload"
)
pages
=
relationship
(
"WikiFilePage"
,
back_populates
=
"wiki_file"
,
cascade
=
"all, delete-orphan"
,
lazy
=
"noload"
,
primaryjoin
=
"WikiFile.id == WikiFilePage.file_id"
,
foreign_keys
=
"[WikiFilePage.file_id]"
)
logs
=
relationship
(
"WikiOperationLog"
,
back_populates
=
"wiki_file"
,
cascade
=
"all, delete-orphan"
,
lazy
=
"noload"
)
logs
=
relationship
(
"WikiOperationLog"
,
back_populates
=
"wiki_file"
,
cascade
=
"all, delete-orphan"
,
lazy
=
"noload"
,
primaryjoin
=
"WikiFile.id == WikiOperationLog.file_id"
,
foreign_keys
=
"[WikiOperationLog.file_id]"
)
class
WikiFilePage
(
Base
):
class
WikiFilePage
(
Base
):
__tablename__
=
'wiki_file_pages'
__tablename__
=
'wiki_file_pages'
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
file_id
=
Column
(
String
(
36
),
ForeignKey
(
"wiki_files.id"
,
ondelete
=
"CASCADE"
),
nullable
=
False
,
index
=
Tru
e
)
file_id
=
Column
(
String
(
36
),
nullable
=
Fals
e
)
page_path
=
Column
(
String
(
512
),
nullable
=
False
)
page_path
=
Column
(
String
(
512
),
nullable
=
False
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
)
wiki_file
=
relationship
(
"WikiFile"
,
back_populates
=
"pages"
)
wiki_file
=
relationship
(
"WikiFile"
,
back_populates
=
"pages"
,
primaryjoin
=
"WikiFilePage.file_id == WikiFile.id"
,
foreign_keys
=
[
file_id
]
)
class
WikiOperationLog
(
Base
):
class
WikiOperationLog
(
Base
):
...
@@ -42,20 +42,20 @@ class WikiOperationLog(Base):
...
@@ -42,20 +42,20 @@ class WikiOperationLog(Base):
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
# file_id 允许为 NULL(查询日志等无关联文件的操作),关联文件删除时日志保留(SET NULL)
# file_id 允许为 NULL(查询日志等无关联文件的操作),关联文件删除时日志保留(SET NULL)
file_id
=
Column
(
String
(
36
),
ForeignKey
(
"wiki_files.id"
,
ondelete
=
"SET NULL"
),
nullable
=
True
,
index
=
True
)
file_id
=
Column
(
String
(
36
),
nullable
=
True
)
file_name
=
Column
(
String
(
255
),
nullable
=
True
)
file_name
=
Column
(
String
(
255
),
nullable
=
True
)
operation
=
Column
(
String
(
50
),
nullable
=
False
)
operation
=
Column
(
String
(
50
),
nullable
=
False
)
details
=
Column
(
Text
,
nullable
=
True
)
details
=
Column
(
Text
,
nullable
=
True
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
)
wiki_file
=
relationship
(
"WikiFile"
,
back_populates
=
"logs"
)
wiki_file
=
relationship
(
"WikiFile"
,
back_populates
=
"logs"
,
primaryjoin
=
"WikiOperationLog.file_id == WikiFile.id"
,
foreign_keys
=
[
file_id
]
)
class
WikiQueryLog
(
Base
):
class
WikiQueryLog
(
Base
):
__tablename__
=
'wiki_query_logs'
__tablename__
=
'wiki_query_logs'
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
session_id
=
Column
(
String
(
100
),
nullable
=
True
,
index
=
True
)
session_id
=
Column
(
String
(
100
),
nullable
=
True
)
query_text
=
Column
(
Text
,
nullable
=
False
)
query_text
=
Column
(
Text
,
nullable
=
False
)
history
=
Column
(
Text
,
nullable
=
True
)
# To store conversation history as JSON
history
=
Column
(
Text
,
nullable
=
True
)
# To store conversation history as JSON
answer_text
=
Column
(
Text
,
nullable
=
True
)
answer_text
=
Column
(
Text
,
nullable
=
True
)
...
...
src/utils/llm_tracker.py
View file @
501ade9
import
asyncio
import
asyncio
import
sys
import
os
import
os
import
functools
import
functools
import
contextlib
import
contextlib
...
@@ -7,6 +6,7 @@ import contextvars
...
@@ -7,6 +6,7 @@ import contextvars
import
agentscope
import
agentscope
from
agentscope.model
import
DashScopeChatModel
from
agentscope.model
import
DashScopeChatModel
from
config
import
SKILLS_DIR
,
API_KEY
,
is_multimodal
,
is_thinking
from
config
import
SKILLS_DIR
,
API_KEY
,
is_multimodal
,
is_thinking
from
src.utils.logger
import
StreamTranscript
# ==================================================================================
# ==================================================================================
# 核心组件:Token 消耗跟踪器 (Token Tracker)
# 核心组件:Token 消耗跟踪器 (Token Tracker)
...
@@ -43,6 +43,7 @@ class TrackingDashScopeChatModel(DashScopeChatModel):
...
@@ -43,6 +43,7 @@ class TrackingDashScopeChatModel(DashScopeChatModel):
last_input
=
0
last_input
=
0
last_output
=
0
last_output
=
0
last_reasoning_len
=
0
last_reasoning_len
=
0
transcript
=
StreamTranscript
(
f
"{getattr(self, 'model_name', 'model')} thinking"
)
# 逐块(Chunk)读取大模型流式响应
# 逐块(Chunk)读取大模型流式响应
async
for
chunk
in
res
:
async
for
chunk
in
res
:
# 并非所有的 Chunk 都会携带 usage 字段,通常只有最后一个 Chunk 包含总计 Token
# 并非所有的 Chunk 都会携带 usage 字段,通常只有最后一个 Chunk 包含总计 Token
...
@@ -59,16 +60,15 @@ class TrackingDashScopeChatModel(DashScopeChatModel):
...
@@ -59,16 +60,15 @@ class TrackingDashScopeChatModel(DashScopeChatModel):
if
len
(
current_thinking
)
>
last_reasoning_len
:
if
len
(
current_thinking
)
>
last_reasoning_len
:
delta
=
current_thinking
[
last_reasoning_len
:]
delta
=
current_thinking
[
last_reasoning_len
:]
if
print_thinking
.
get
():
if
print_thinking
.
get
():
sys
.
stdout
.
write
(
delta
)
transcript
.
write
(
delta
)
sys
.
stdout
.
flush
()
last_reasoning_len
=
len
(
current_thinking
)
last_reasoning_len
=
len
(
current_thinking
)
# 将当前 Chunk 原封不动 yield 给上层业务
# 将当前 Chunk 原封不动 yield 给上层业务
yield
chunk
yield
chunk
if
last_reasoning_len
>
0
and
print_thinking
.
get
():
if
last_reasoning_len
>
0
and
print_thinking
.
get
():
sys
.
stdou
t
.
write
(
"
\n
"
)
transcrip
t
.
write
(
"
\n
"
)
sys
.
stdout
.
flush
()
transcript
.
close
()
# 流式输出全部结束后,计算总 Token 并累加到当前任务的 ContextVar 计数器中
# 流式输出全部结束后,计算总 Token 并累加到当前任务的 ContextVar 计数器中
counter
=
current_task_tokens
.
get
(
None
)
counter
=
current_task_tokens
.
get
(
None
)
...
@@ -130,4 +130,7 @@ async def token_counter_context():
...
@@ -130,4 +130,7 @@ async def token_counter_context():
try
:
try
:
yield
lambda
:
counter
[
0
]
yield
lambda
:
counter
[
0
]
finally
:
finally
:
current_task_tokens
.
reset
(
token
)
try
:
current_task_tokens
.
reset
(
token
)
except
ValueError
:
pass
src/utils/logger.py
View file @
501ade9
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
Attach a file
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 post a comment