Update api/utils.py
Browse files- api/utils.py +14 -16
api/utils.py
CHANGED
@@ -7,7 +7,7 @@ import uuid
|
|
7 |
import httpx
|
8 |
from api import validate
|
9 |
from api.config import MODEL_MAPPING, headers, AGENT_MODE, TRENDING_AGENT_MODE
|
10 |
-
from fastapi import Depends
|
11 |
from fastapi.security import HTTPAuthorizationCredentials
|
12 |
|
13 |
from api.config import APP_SECRET, BASE_URL
|
@@ -41,33 +41,31 @@ def verify_app_secret(credentials: HTTPAuthorizationCredentials = Depends(securi
|
|
41 |
return credentials.credentials
|
42 |
|
43 |
def message_to_dict(message):
|
|
|
|
|
|
|
|
|
44 |
if isinstance(message.content, str):
|
45 |
-
|
46 |
elif isinstance(message.content, list) and len(message.content) == 2:
|
47 |
-
|
48 |
-
"role": message.role,
|
49 |
-
"content": message.content[0]["text"],
|
50 |
-
"data": {
|
51 |
-
"imageBase64": message.content[1]["image_url"]["url"],
|
52 |
-
"fileText": "",
|
53 |
-
"title": "snapshot",
|
54 |
-
},
|
55 |
-
}
|
56 |
else:
|
57 |
-
|
|
|
|
|
58 |
|
59 |
async def process_streaming_response(request: ChatRequest):
|
60 |
agent_mode = AGENT_MODE.get(request.model, {})
|
61 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
62 |
|
63 |
-
# Log
|
64 |
logger.info(
|
65 |
f"Streaming request for model: '{request.model}', "
|
66 |
f"agent mode: {agent_mode}, trending agent mode: {trending_agent_mode}"
|
67 |
)
|
68 |
|
69 |
json_data = {
|
70 |
-
"messages": [
|
71 |
"previewToken": None,
|
72 |
"userId": None,
|
73 |
"codeModelMode": True,
|
@@ -126,14 +124,14 @@ async def process_non_streaming_response(request: ChatRequest):
|
|
126 |
agent_mode = AGENT_MODE.get(request.model, {})
|
127 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
128 |
|
129 |
-
# Log
|
130 |
logger.info(
|
131 |
f"Non-streaming request for model: '{request.model}', "
|
132 |
f"agent mode: {agent_mode}, trending agent mode: {trending_agent_mode}"
|
133 |
)
|
134 |
|
135 |
json_data = {
|
136 |
-
"messages": [
|
137 |
"previewToken": None,
|
138 |
"userId": None,
|
139 |
"codeModelMode": True,
|
|
|
7 |
import httpx
|
8 |
from api import validate
|
9 |
from api.config import MODEL_MAPPING, headers, AGENT_MODE, TRENDING_AGENT_MODE
|
10 |
+
from fastapi import Depends
|
11 |
from fastapi.security import HTTPAuthorizationCredentials
|
12 |
|
13 |
from api.config import APP_SECRET, BASE_URL
|
|
|
41 |
return credentials.credentials
|
42 |
|
43 |
def message_to_dict(message):
|
44 |
+
"""
|
45 |
+
Convert a Message object to a dictionary.
|
46 |
+
Mask the content to prevent logging sensitive information.
|
47 |
+
"""
|
48 |
if isinstance(message.content, str):
|
49 |
+
masked_content = "[HIDDEN]"
|
50 |
elif isinstance(message.content, list) and len(message.content) == 2:
|
51 |
+
masked_content = "[HIDDEN]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
else:
|
53 |
+
masked_content = "[HIDDEN]"
|
54 |
+
|
55 |
+
return {"role": message.role, "content": masked_content}
|
56 |
|
57 |
async def process_streaming_response(request: ChatRequest):
|
58 |
agent_mode = AGENT_MODE.get(request.model, {})
|
59 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
60 |
|
61 |
+
# Log only non-sensitive information
|
62 |
logger.info(
|
63 |
f"Streaming request for model: '{request.model}', "
|
64 |
f"agent mode: {agent_mode}, trending agent mode: {trending_agent_mode}"
|
65 |
)
|
66 |
|
67 |
json_data = {
|
68 |
+
"messages": [message_to_dict(msg) for msg in request.messages],
|
69 |
"previewToken": None,
|
70 |
"userId": None,
|
71 |
"codeModelMode": True,
|
|
|
124 |
agent_mode = AGENT_MODE.get(request.model, {})
|
125 |
trending_agent_mode = TRENDING_AGENT_MODE.get(request.model, {})
|
126 |
|
127 |
+
# Log only non-sensitive information
|
128 |
logger.info(
|
129 |
f"Non-streaming request for model: '{request.model}', "
|
130 |
f"agent mode: {agent_mode}, trending agent mode: {trending_agent_mode}"
|
131 |
)
|
132 |
|
133 |
json_data = {
|
134 |
+
"messages": [message_to_dict(msg) for msg in request.messages],
|
135 |
"previewToken": None,
|
136 |
"userId": None,
|
137 |
"codeModelMode": True,
|