Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
J
jinchat-server
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
aigc-pioneer
jinchat-server
Commits
07ff81a1
提交
07ff81a1
authored
5月 04, 2023
作者:
imClumsyPanda
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update torch_gc
上级
b03634fb
隐藏空白字符变更
内嵌
并排
正在显示
7 个修改的文件
包含
29 行增加
和
43 行删除
+29
-43
api.py
api.py
+3
-9
local_doc_qa.py
chains/local_doc_qa.py
+6
-13
cli_demo.py
cli_demo.py
+1
-7
model_config.py
configs/model_config.py
+10
-1
chatglm_llm.py
models/chatglm_llm.py
+4
-2
__init__.py
utils/__init__.py
+4
-4
webui.py
webui.py
+1
-7
没有找到文件。
api.py
浏览文件 @
07ff81a1
...
...
@@ -16,16 +16,10 @@ from typing_extensions import Annotated
from
chains.local_doc_qa
import
LocalDocQA
from
configs.model_config
import
(
API_UPLOAD_ROOT_PATH
,
EMBEDDING_DEVICE
,
EMBEDDING_MODEL
,
LLM_MODEL
)
nltk
.
data
.
path
=
[
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
"nltk_data"
)]
+
nltk
.
data
.
path
# return top-k text chunk from vector store
VECTOR_SEARCH_TOP_K
=
6
# LLM input history length
LLM_HISTORY_LEN
=
3
EMBEDDING_MODEL
,
LLM_MODEL
,
NLTK_DATA_PATH
,
VECTOR_SEARCH_TOP_K
,
LLM_HISTORY_LEN
)
nltk
.
data
.
path
=
[
NLTK_DATA_PATH
]
+
nltk
.
data
.
path
class
BaseResponse
(
BaseModel
):
code
:
int
=
pydantic
.
Field
(
200
,
description
=
"HTTP status code"
)
...
...
chains/local_doc_qa.py
浏览文件 @
07ff81a1
...
...
@@ -10,11 +10,6 @@ from langchain.docstore.document import Document
import
numpy
as
np
from
utils
import
torch_gc
# return top-k text chunk from vector store
VECTOR_SEARCH_TOP_K
=
6
# LLM input history length
LLM_HISTORY_LEN
=
3
DEVICE_
=
EMBEDDING_DEVICE
DEVICE_ID
=
"0"
if
torch
.
cuda
.
is_available
()
else
None
...
...
@@ -109,7 +104,7 @@ def similarity_search_with_score_by_vector(
if
not
isinstance
(
doc
,
Document
):
raise
ValueError
(
f
"Could not find document for id {_id}, got {doc}"
)
docs
.
append
((
doc
,
scores
[
0
][
j
]))
torch_gc
(
DEVICE
)
torch_gc
()
return
docs
...
...
@@ -181,13 +176,13 @@ class LocalDocQA:
if
vs_path
and
os
.
path
.
isdir
(
vs_path
):
vector_store
=
FAISS
.
load_local
(
vs_path
,
self
.
embeddings
)
vector_store
.
add_documents
(
docs
)
torch_gc
(
DEVICE
)
torch_gc
()
else
:
if
not
vs_path
:
vs_path
=
os
.
path
.
join
(
VS_ROOT_PATH
,
f
"""{os.path.splitext(file)[0]}_FAISS_{datetime.datetime.now().strftime("
%
Y
%
m
%
d_
%
H
%
M
%
S")}"""
)
vector_store
=
FAISS
.
from_documents
(
docs
,
self
.
embeddings
)
torch_gc
(
DEVICE
)
torch_gc
()
vector_store
.
save_local
(
vs_path
)
return
vs_path
,
loaded_files
...
...
@@ -206,6 +201,7 @@ class LocalDocQA:
related_docs_with_score
=
vector_store
.
similarity_search_with_score
(
query
,
k
=
self
.
top_k
)
related_docs
=
get_docs_with_score
(
related_docs_with_score
)
torch_gc
()
prompt
=
generate_prompt
(
related_docs
,
query
)
# if streaming:
...
...
@@ -220,11 +216,13 @@ class LocalDocQA:
for
result
,
history
in
self
.
llm
.
_call
(
prompt
=
prompt
,
history
=
chat_history
,
streaming
=
streaming
):
torch_gc
()
history
[
-
1
][
0
]
=
query
response
=
{
"query"
:
query
,
"result"
:
result
,
"source_documents"
:
related_docs
}
yield
response
,
history
torch_gc
()
if
__name__
==
"__main__"
:
...
...
@@ -244,9 +242,4 @@ if __name__ == "__main__":
for
inum
,
doc
in
enumerate
(
resp
[
"source_documents"
])]
print
(
"
\n\n
"
+
"
\n\n
"
.
join
(
source_text
))
# for resp, history in local_doc_qa.get_knowledge_based_answer(query=query,
# vs_path=vs_path,
# chat_history=[],
# streaming=False):
# print(resp["result"])
pass
cli_demo.py
浏览文件 @
07ff81a1
...
...
@@ -3,13 +3,7 @@ from chains.local_doc_qa import LocalDocQA
import
os
import
nltk
nltk
.
data
.
path
=
[
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
"nltk_data"
)]
+
nltk
.
data
.
path
# return top-k text chunk from vector store
VECTOR_SEARCH_TOP_K
=
6
# LLM input history length
LLM_HISTORY_LEN
=
3
nltk
.
data
.
path
=
[
NLTK_DATA_PATH
]
+
nltk
.
data
.
path
# Show reply with source text from input document
REPLY_WITH_SOURCE
=
True
...
...
configs/model_config.py
浏览文件 @
07ff81a1
...
...
@@ -49,4 +49,12 @@ PROMPT_TEMPLATE = """已知信息:
根据上述已知信息,简洁和专业的来回答用户的问题。如果无法从中得到答案,请说 “根据已知信息无法回答该问题” 或 “没有提供足够的相关信息”,不允许在答案中添加编造成分,答案请使用中文。 问题是:{question}"""
# 匹配后单段上下文长度
CHUNK_SIZE
=
500
CHUNK_SIZE
=
250
# LLM input history length
LLM_HISTORY_LEN
=
3
# return top-k text chunk from vector store
VECTOR_SEARCH_TOP_K
=
5
NLTK_DATA_PATH
=
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
"nltk_data"
)
\ No newline at end of file
models/chatglm_llm.py
浏览文件 @
07ff81a1
...
...
@@ -69,12 +69,13 @@ class ChatGLM(LLM):
max_length
=
self
.
max_token
,
temperature
=
self
.
temperature
,
)):
torch_gc
(
DEVICE
)
torch_gc
()
if
inum
==
0
:
history
+=
[[
prompt
,
stream_resp
]]
else
:
history
[
-
1
]
=
[
prompt
,
stream_resp
]
yield
stream_resp
,
history
torch_gc
()
else
:
response
,
_
=
self
.
model
.
chat
(
self
.
tokenizer
,
...
...
@@ -83,9 +84,10 @@ class ChatGLM(LLM):
max_length
=
self
.
max_token
,
temperature
=
self
.
temperature
,
)
torch_gc
(
DEVICE
)
torch_gc
()
history
+=
[[
prompt
,
response
]]
yield
response
,
history
torch_gc
()
# def chat(self,
# prompt: str) -> str:
...
...
utils/__init__.py
浏览文件 @
07ff81a1
import
torch
def
torch_gc
(
DEVICE
):
def
torch_gc
():
if
torch
.
cuda
.
is_available
():
with
torch
.
cuda
.
device
(
DEVICE
):
torch
.
cuda
.
empty_cache
()
torch
.
cuda
.
ipc_collect
()
#
with torch.cuda.device(DEVICE):
torch
.
cuda
.
empty_cache
()
torch
.
cuda
.
ipc_collect
()
elif
torch
.
backends
.
mps
.
is_available
():
try
:
from
torch.mps
import
empty_cache
...
...
webui.py
浏览文件 @
07ff81a1
...
...
@@ -5,13 +5,7 @@ from chains.local_doc_qa import LocalDocQA
from
configs.model_config
import
*
import
nltk
nltk
.
data
.
path
=
[
os
.
path
.
join
(
os
.
path
.
dirname
(
__file__
),
"nltk_data"
)]
+
nltk
.
data
.
path
# return top-k text chunk from vector store
VECTOR_SEARCH_TOP_K
=
6
# LLM input history length
LLM_HISTORY_LEN
=
3
nltk
.
data
.
path
=
[
NLTK_DATA_PATH
]
+
nltk
.
data
.
path
def
get_vs_list
():
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论