Skip to content

Commit 0ddc604

Browse files
committed
Add a new LLM: 'Moonshot'
1 parent ca62f61 commit 0ddc604

32 files changed

+1574
-642
lines changed

README.md

+40
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Quickly launch an intelligent customer service system with Flask, LLM, RAG, incl
1818
- [Using OpenAI as the LLM base](#using-openai-as-the-llm-base)
1919
- [Using ZhipuAI as the LLM base](#using-zhipuai-as-the-llm-base)
2020
- [Using DeepSeek as the LLM base](#using-deepseek-as-the-llm-base)
21+
- [Using Moonshot as the LLM base](#using-moonshot-as-the-llm-base)
2122
- [Using local LLMs](#using-local-llms)
2223
- [Step 3: Deploy RAG-GPT](#step-3-deploy-rag-gpt)
2324
- [Deploy RAG-GPT using Docker](#deploy-rag-gpt-using-docker)
@@ -168,6 +169,45 @@ LLAMA_CLOUD_API_KEY="xxxx"
168169
- Adjust **`URL_PREFIX`** to match your website's domain. This is mainly for generating accessible URL links for uploaded local files. Such as `http://127.0.0.1:7000/web/download_dir/2024_05_20/d3a01d6a-90cd-4c2a-b926-9cda12466caf/openssl-cookbook.pdf`.
169170
- For more information about the meanings and usages of constants, you can check under the `server/constant` directory.
170171

172+
173+
#### Using Moonshot as the LLM base
174+
175+
If you cannot use OpenAI's API services, consider using Moonshot as an alternative.
176+
177+
> [!NOTE]
178+
> Moonshot does not provide an `Embedding API`, so here we use ZhipuAI's `Embedding API`.
179+
180+
181+
```shell
182+
cp env_of_moonshot .env
183+
```
184+
185+
The variables in .env
186+
187+
```shell
188+
LLM_NAME="Moonshot"
189+
ZHIPUAI_API_KEY="xxxx"
190+
MOONSHOT_API_KEY="xxxx"
191+
MOONSHOT_MODEL_NAME="moonshot-v1-8k"
192+
MIN_RELEVANCE_SCORE=0.4
193+
BOT_TOPIC="xxxx"
194+
URL_PREFIX="http://127.0.0.1:7000/"
195+
USE_PREPROCESS_QUERY=1
196+
USE_RERANKING=1
197+
USE_DEBUG=0
198+
USE_LLAMA_PARSE=0
199+
LLAMA_CLOUD_API_KEY="xxxx"
200+
```
201+
202+
- Don't modify **`LLM_NAME`**
203+
- Modify the **`ZHIPUAI_API_KEY`** with your own key. Please log in to the [ZhipuAI website](https://open.bigmodel.cn/usercenter/apikeys) to view your API Key.
204+
- Modify the **`MOONSHOT_API_KEY`** with your own key. Please log in to the [Moonshot website](https://platform.moonshot.cn/console/api-keys) to view your API Key.
205+
- Update the **`MOONSHOT_MODEL_NAME `** setting if you want to use other models of Moonshot.
206+
- Change **`BOT_TOPIC`** to reflect your Bot's name. This is very important, as it will be used in `Prompt Construction`. Please try to use a concise and clear word, such as `OpenIM`, `LangChain`.
207+
- Adjust **`URL_PREFIX`** to match your website's domain. This is mainly for generating accessible URL links for uploaded local files. Such as `http://127.0.0.1:7000/web/download_dir/2024_05_20/d3a01d6a-90cd-4c2a-b926-9cda12466caf/openssl-cookbook.pdf`.
208+
- For more information about the meanings and usages of constants, you can check under the `server/constant` directory.
209+
210+
171211
#### Using local LLMs
172212

173213
If your knowledge base involves **sensitive information** and you prefer not to use cloud-based LLMs, consider using `Ollama` to deploy large models locally.

env_of_moonshot

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
LLM_NAME="Moonshot"
2+
ZHIPUAI_API_KEY="xxxx"
3+
MOONSHOT_API_KEY="xxxx"
4+
MOONSHOT_MODEL_NAME="moonshot-v1-8k"
5+
MIN_RELEVANCE_SCORE=0.4
6+
BOT_TOPIC="xxxx"
7+
URL_PREFIX="http://127.0.0.1:7000/"
8+
USE_PREPROCESS_QUERY=1
9+
USE_RERANKING=1
10+
USE_DEBUG=0
11+
USE_LLAMA_PARSE=0
12+
LLAMA_CLOUD_API_KEY="xxxx"

server/app/account.py

+89-23
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
from server.app.utils.token_helper import TokenHelper
88
from server.logger.logger_config import my_logger as logger
99

10-
11-
account_bp = Blueprint('account_config', __name__, url_prefix='/open_kf_api/account')
10+
account_bp = Blueprint('account_config',
11+
__name__,
12+
url_prefix='/open_kf_api/account')
1213

1314

1415
@account_bp.route('/login', methods=['POST'])
@@ -18,36 +19,65 @@ def login():
1819
password = data.get('password')
1920

2021
if not account_name or not password:
21-
return {'retcode': -20000, 'message': 'Account name and password are required', 'data': {}}
22+
return {
23+
'retcode': -20000,
24+
'message': 'Account name and password are required',
25+
'data': {}
26+
}
2227

2328
conn = None
2429
try:
2530
conn = get_db_connection()
2631
cur = conn.cursor()
2732

2833
# Check if the account exists and verify the password
29-
cur.execute('SELECT id, password_hash FROM t_account_tab WHERE account_name = ?', (account_name,))
34+
cur.execute(
35+
'SELECT id, password_hash FROM t_account_tab WHERE account_name = ?',
36+
(account_name, ))
3037
account = cur.fetchone()
3138

3239
if account and check_password_hash(account['password_hash'], password):
3340
# Generate token with account_name in the payload
3441
token = TokenHelper.generate_token(account_name)
3542
logger.info(f"Generate token: '{token}'")
36-
43+
3744
# Set is_login to 1 and update mtime to the current Unix timestamp
3845
try:
3946
with diskcache_lock.lock():
40-
cur.execute('UPDATE t_account_tab SET is_login = 1, mtime = ? WHERE account_name = ?', (int(time.time()), account_name,))
47+
cur.execute(
48+
'UPDATE t_account_tab SET is_login = 1, mtime = ? WHERE account_name = ?',
49+
(
50+
int(time.time()),
51+
account_name,
52+
))
4153
conn.commit()
4254
except Exception as e:
4355
logger.error(f"Process discache_lock exception: {e}")
44-
return {'retcode': -30000, 'message': f'An error occurred: {e}', 'data': {}}
45-
46-
return {'retcode': 0, 'message': 'Login successful', 'data': {'token': token}}
56+
return {
57+
'retcode': -30000,
58+
'message': f'An error occurred: {e}',
59+
'data': {}
60+
}
61+
62+
return {
63+
'retcode': 0,
64+
'message': 'Login successful',
65+
'data': {
66+
'token': token
67+
}
68+
}
4769
else:
48-
return {'retcode': -20001, 'message': 'Invalid credentials', 'data': {}}
70+
return {
71+
'retcode': -20001,
72+
'message': 'Invalid credentials',
73+
'data': {}
74+
}
4975
except Exception as e:
50-
return {'retcode': -30000, 'message': f'An error occurred during login, exception: {e}', 'data': {}}
76+
return {
77+
'retcode': -30000,
78+
'message': f'An error occurred during login, exception: {e}',
79+
'data': {}
80+
}
5181
finally:
5282
if conn:
5383
conn.close()
@@ -62,11 +92,18 @@ def update_password():
6292
new_password = data.get('new_password')
6393

6494
if None in (account_name, current_password, new_password):
65-
return {'retcode': -20000, 'message': 'Account name, current password, and new password are required', 'data': {}}
95+
return {
96+
'retcode': -20000,
97+
'message':
98+
'Account name, current password, and new password are required',
99+
'data': {}
100+
}
66101

67102
token_user_id = request.user_payload['user_id']
68103
if token_user_id != account_name:
69-
logger.error(f"account_name:'{account_name}' does not match with token_user_id: '{token_user_id}'")
104+
logger.error(
105+
f"account_name:'{account_name}' does not match with token_user_id: '{token_user_id}'"
106+
)
70107
return {'retcode': -20001, 'message': 'Token is invalid!', 'data': {}}
71108

72109
conn = None
@@ -75,26 +112,55 @@ def update_password():
75112
cur = conn.cursor()
76113

77114
# Check if the account exists and verify the current password
78-
cur.execute('SELECT id, password_hash FROM t_account_tab WHERE account_name = ?', (account_name,))
115+
cur.execute(
116+
'SELECT id, password_hash FROM t_account_tab WHERE account_name = ?',
117+
(account_name, ))
79118
account = cur.fetchone()
80119

81-
if not account or not check_password_hash(account['password_hash'], current_password):
82-
logger.error(f"Invalid account_name: '{account_name}' or current_password: '{current_password}'")
83-
return {'retcode': -20001, 'message': 'Invalid account name or password', 'data': {}}
120+
if not account or not check_password_hash(account['password_hash'],
121+
current_password):
122+
logger.error(
123+
f"Invalid account_name: '{account_name}' or current_password: '{current_password}'"
124+
)
125+
return {
126+
'retcode': -20001,
127+
'message': 'Invalid account name or password',
128+
'data': {}
129+
}
84130

85131
# Update the password
86-
new_password_hash = generate_password_hash(new_password, method='pbkdf2:sha256', salt_length=10)
132+
new_password_hash = generate_password_hash(new_password,
133+
method='pbkdf2:sha256',
134+
salt_length=10)
87135
try:
88136
with diskcache_lock.lock():
89-
cur.execute('UPDATE t_account_tab SET password_hash = ?, mtime = ? WHERE account_name = ?', (new_password_hash, int(time.time()), account_name,))
137+
cur.execute(
138+
'UPDATE t_account_tab SET password_hash = ?, mtime = ? WHERE account_name = ?',
139+
(
140+
new_password_hash,
141+
int(time.time()),
142+
account_name,
143+
))
90144
conn.commit()
91145
except Exception as e:
92146
logger.error(f"Process discache_lock exception: {e}")
93-
return {'retcode': -30000, 'message': f'An error occurred: {e}', 'data': {}}
94-
95-
return {'retcode': 0, 'message': 'Password updated successfully', 'data': {}}
147+
return {
148+
'retcode': -30000,
149+
'message': f'An error occurred: {e}',
150+
'data': {}
151+
}
152+
153+
return {
154+
'retcode': 0,
155+
'message': 'Password updated successfully',
156+
'data': {}
157+
}
96158
except Exception as e:
97-
return {'retcode': -20001, 'message': f'An error occurred: {e}', 'data': {}}
159+
return {
160+
'retcode': -20001,
161+
'message': f'An error occurred: {e}',
162+
'data': {}
163+
}
98164
finally:
99165
if conn:
100166
conn.close()

server/app/auth.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from server.logger.logger_config import my_logger as logger
33
from server.app.utils.token_helper import TokenHelper
44

5-
65
auth_bp = Blueprint('auth', __name__, url_prefix='/open_kf_api/auth')
76

87

@@ -11,13 +10,19 @@ def get_token():
1110
data = request.json
1211
user_id = data.get('user_id')
1312
if not user_id:
14-
return {'retcode': -20000, 'message': 'user_id is required', 'data': {}}
13+
return {
14+
'retcode': -20000,
15+
'message': 'user_id is required',
16+
'data': {}
17+
}
1518

1619
try:
1720
# generate token
1821
token = TokenHelper.generate_token(user_id)
1922
logger.success(f"Generate token: '{token}' with user_id: '{user_id}'")
2023
return {"retcode": 0, "message": "success", "data": {"token": token}}
2124
except Exception as e:
22-
logger.error(f"Generate token with user_id: '{user_id}' is failed, the exception is {e}")
25+
logger.error(
26+
f"Generate token with user_id: '{user_id}' is failed, the exception is {e}"
27+
)
2328
return {'retcode': -20001, 'message': str(e), 'data': {}}

0 commit comments

Comments
 (0)