-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathconfig.py
More file actions
219 lines (179 loc) · 7.57 KB
/
Copy pathconfig.py
File metadata and controls
219 lines (179 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import logging
import sys
from typing import Optional, List, Dict, Any, Generator
from openai import OpenAI
from langchain_huggingface import HuggingFaceEmbeddings
# API配置
API_BASE_URL = "YOUR_API_URL"
API_KEY = "YOUR_API_KEY"
TTS_GROUP_ID = "YOUR_MINIMAX_GROUP_ID"
TTS_API_KEY = "YOUR_MINIMAX_API_KEY"
# 嵌入模型配置
EMBEDDING_MODEL_NAME = "BAAI/bge-m3"
# 日志配置
def setup_logging():
"""设置日志配置为控制台输出"""
# 设置日志格式
log_format = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# 创建一个根日志记录器
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
# 创建并配置控制台处理器
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(log_format)
# 清除任何现有的处理器
root_logger.handlers.clear()
# 添加控制台处理器
root_logger.addHandler(console_handler)
# LLM客户端
class LLMClient:
_instance: Optional['LLMClient'] = None
def __new__(cls, *args, **kwargs):
"""单例模式实现"""
if cls._instance is None:
cls._instance = super(LLMClient, cls).__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self, api_key=None, base_url=None):
"""初始化LLM客户端"""
if self._initialized:
return
self.api_key = api_key or API_KEY
self.base_url = base_url or API_BASE_URL
self.client = OpenAI(
api_key=self.api_key,
base_url=self.base_url
)
self._initialized = True
def chat(self, messages: List[Dict[str, Any]], temperature=0.5, stream=True) -> str:
"""与LLM交互
Args:
messages: 消息列表
temperature: 温度参数,控制随机性
stream: 是否使用流式输出
Returns:
str: LLM响应内容
"""
try:
response = self.client.chat.completions.create(
model="deepseek-v3-250324",
messages=messages,
temperature=temperature,
stream=stream
)
if stream:
full_response = ""
for chunk in response:
if chunk.choices[0].delta.content is not None:
content = chunk.choices[0].delta.content
print(content, end='', flush=True)
full_response += content
print()
return full_response
else:
return response.choices[0].message.content
except Exception as e:
print(f"LLM调用出错: {str(e)}")
raise
def chat_stream_by_sentence(self, messages: List[Dict[str, Any]], temperature=0.5) -> Generator[str, None, str]:
"""与LLM交互,按句子流式返回结果
Args:
messages: 消息列表
temperature: 温度参数,控制随机性
Yields:
str: 每个完整句子
Returns:
str: 完整响应
"""
try:
response = self.client.chat.completions.create(
model="deepseek-v3-250324",
messages=messages,
temperature=temperature,
stream=True
)
full_response = ""
current_sentence = ""
# 中文的结束标点 - 这些可以直接作为句子结束符
cn_end_marks = '。!?'
# 英文的结束标点 - 这些需要检查后续字符
en_end_marks = '.!?;'
for chunk in response:
if chunk.choices[0].delta.content is not None:
content = chunk.choices[0].delta.content
current_sentence += content
full_response += content
# 情况1: 包含中文结束标点,直接作为句子结束
if any(char in cn_end_marks for char in content):
sentence = current_sentence.strip()
# 只有句子长度超过10字才yield
if sentence and len(sentence) >= 10:
yield sentence
current_sentence = ""
# 情况2: 检查英文结束标点后是否跟着空格或换行符
elif any(char in en_end_marks for char in content):
# 检查当前积累的句子中是否有 "英文结束标点+空格/换行" 的模式
import re
# 匹配 句点/感叹号/问号/分号 后跟空白字符的模式
matches = list(re.finditer(r'[.!?;][\s\n]', current_sentence))
if matches:
# 找到最后一个匹配,在该位置分割句子
last_match = matches[-1]
end_position = last_match.end() - 1 # 减1是为了不包含空格/换行符
sentence = current_sentence[:end_position].strip()
remaining = current_sentence[end_position:].strip()
# 只有句子长度超过10字才yield
if sentence and len(sentence) >= 10:
yield sentence
current_sentence = remaining
# 处理剩余内容
if current_sentence.strip():
sentence = current_sentence.strip()
if sentence:
yield sentence
return full_response
except Exception as e:
print(f"LLM调用出错: {str(e)}")
yield f"生成回复时出错: {str(e)}"
raise
# 嵌入模型
class EmbeddingModel:
_instance: Optional[HuggingFaceEmbeddings] = None
@classmethod
def get_instance(cls) -> HuggingFaceEmbeddings:
"""获取嵌入模型单例"""
if cls._instance is None:
# 检查CUDA可用性
try:
import torch
device = "cuda" if torch.cuda.is_available() else "cpu"
except ImportError:
device = "cpu"
logging.info(f"初始化嵌入模型: {EMBEDDING_MODEL_NAME},使用设备: {device}")
cls._instance = HuggingFaceEmbeddings(
model_name=EMBEDDING_MODEL_NAME,
model_kwargs={"device": device},
encode_kwargs={"normalize_embeddings": True}
)
return cls._instance
# 使用示例
if __name__ == "__main__":
# 设置日志
setup_logging()
logger = logging.getLogger(__name__)
# LLM客户端示例
logger.info("测试LLM客户端...")
llm = LLMClient()
messages = [
{"role": "user", "content": "你好"}
]
response = llm.chat(messages)
logger.info(f"LLM响应: {response}")
# 嵌入模型示例
logger.info("测试嵌入模型...")
text = "这是一个测试文本"
embedding_model = EmbeddingModel.get_instance()
embedding = embedding_model.embed_query(text)
logger.info(f"嵌入向量维度: {len(embedding)}")