Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cd1f95bb2c | |||
| 0c4cc96106 |
@@ -232,7 +232,7 @@ class LLMService:
|
||||
temperature: float = 0.7
|
||||
) -> str:
|
||||
"""调用API"""
|
||||
url = f"{api_base}/chat/completions"
|
||||
url = f"{api_base.rstrip('/')}/chat/completions"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {api_key}",
|
||||
"Content-Type": "application/json"
|
||||
@@ -244,13 +244,33 @@ class LLMService:
|
||||
"max_tokens": max_tokens
|
||||
}
|
||||
|
||||
# 打印请求详情(调试)
|
||||
logger.info(f"调用LLM: url={url}, model={model}")
|
||||
logger.info(f"消息数量: {len(messages)}, 第一条消息类型: {type(messages[0].get('content'))}")
|
||||
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
response = await client.post(url, headers=headers, json=payload)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
return data['choices'][0]['message']['content']
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
response = await client.post(url, headers=headers, json=payload)
|
||||
|
||||
# 检查HTTP状态
|
||||
if response.status_code != 200:
|
||||
logger.error(f"API返回错误: status={response.status_code}, body={response.text[:500]}")
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
|
||||
# 检查响应格式
|
||||
if 'choices' not in data or len(data['choices']) == 0:
|
||||
logger.error(f"API响应格式错误: {data}")
|
||||
raise ValueError("API响应格式错误:缺少choices")
|
||||
|
||||
return data['choices'][0]['message']['content']
|
||||
except httpx.HTTPStatusError as e:
|
||||
logger.error(f"HTTP错误: {e.response.status_code}, {e.response.text}")
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"API调用异常: {type(e).__name__}: {e}")
|
||||
raise
|
||||
|
||||
async def chat_stream(
|
||||
self,
|
||||
|
||||
@@ -146,6 +146,17 @@
|
||||
.modal-buttons { display: flex; gap: 12px; justify-content: flex-end; }
|
||||
.modal-buttons button { padding: 8px 16px; border-radius: 8px; cursor: pointer; }
|
||||
|
||||
/* 图片放大弹窗 */
|
||||
.image-lightbox { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.9); display: none; align-items: center; justify-content: center; z-index: 2000; cursor: zoom-out; }
|
||||
.image-lightbox.show { display: flex; }
|
||||
.image-lightbox img { max-width: 90%; max-height: 90%; border-radius: 8px; box-shadow: 0 0 30px rgba(255,255,255,0.2); }
|
||||
.image-lightbox-close { position: absolute; top: 20px; right: 20px; width: 40px; height: 40px; background: rgba(255,255,255,0.2); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: #fff; font-size: 20px; cursor: pointer; transition: background 0.2s; }
|
||||
.image-lightbox-close:hover { background: rgba(255,255,255,0.3); }
|
||||
|
||||
/* 对话中的图片可点击 */
|
||||
.uploaded-image img { cursor: zoom-in; transition: transform 0.2s; }
|
||||
.uploaded-image img:hover { transform: scale(1.02); }
|
||||
|
||||
.welcome { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; color: #666; }
|
||||
.welcome h2 { font-size: 28px; margin-bottom: 16px; color: #333; }
|
||||
|
||||
@@ -211,6 +222,12 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 图片放大弹窗 -->
|
||||
<div class="image-lightbox" id="imageLightbox" onclick="closeImageLightbox()">
|
||||
<div class="image-lightbox-close"><i class="ri-close-line"></i></div>
|
||||
<img id="lightboxImage" src="" alt="放大图片">
|
||||
</div>
|
||||
|
||||
<!-- Markdown渲染库 -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
|
||||
<script>
|
||||
@@ -858,7 +875,7 @@
|
||||
for (const f of files) {
|
||||
if (f.type.startsWith('image/')) {
|
||||
// 图片直接显示
|
||||
html += `<div class="uploaded-image" style="margin-bottom:8px"><img src="${f.content}" style="max-width:300px;border-radius:8px"></div>`;
|
||||
html += `<div class="uploaded-image" style="margin-bottom:8px"><img src="${f.content}" style="max-width:300px;border-radius:8px" onclick="openImageLightbox('${f.content}')"></div>`;
|
||||
} else {
|
||||
// 文本文件显示名称和内容摘要
|
||||
html += `<div class="uploaded-file" style="padding:8px;background:#f5f5f5;border-radius:6px;margin-bottom:8px">`;
|
||||
@@ -1014,6 +1031,26 @@
|
||||
}
|
||||
|
||||
document.getElementById('newPhraseInput').addEventListener('keydown', e => { if (e.key === 'Enter') addPhrase(); if (e.key === 'Escape') hidePhraseModal(); });
|
||||
|
||||
// 图片放大弹窗
|
||||
function openImageLightbox(imageSrc) {
|
||||
const lightbox = document.getElementById('imageLightbox');
|
||||
const lightboxImg = document.getElementById('lightboxImage');
|
||||
lightboxImg.src = imageSrc;
|
||||
lightbox.classList.add('show');
|
||||
}
|
||||
|
||||
function closeImageLightbox() {
|
||||
const lightbox = document.getElementById('imageLightbox');
|
||||
lightbox.classList.remove('show');
|
||||
}
|
||||
|
||||
// ESC键关闭图片弹窗
|
||||
document.addEventListener('keydown', e => {
|
||||
if (e.key === 'Escape') {
|
||||
closeImageLightbox();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user