Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 186 additions & 19 deletions ChatBot/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,196 @@
<!DOCTYPE html>
<html lang="en">
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Chatbot</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>智能聊天机器人</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

body {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}

.chat-wrapper {
max-width: 900px;
margin: 0 auto;
height: calc(100vh - 40px);
display: flex;
flex-direction: column;
background: #fff;
border-radius: 15px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
overflow: hidden;
}

.chat-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 20px;
text-align: center;
font-size: 24px;
font-weight: 600;
}

.chat-container {
max-height: 3000px; /* 设置对话框的最大高度 */
overflow-y: auto; /* 当内容溢出时添加垂直滚动条 */
flex: 1;
overflow-y: auto;
padding: 25px;
background: #f8f9fa;
min-height: 500px;
}

.message {
margin-bottom: 20px;
display: flex;
flex-direction: column;
}

.message.user {
align-items: flex-end;
}

.message.bot {
align-items: flex-start;
}

.message-content {
max-width: 75%;
padding: 15px 20px;
border-radius: 18px;
font-size: 15px;
line-height: 1.6;
word-wrap: break-word;
}

.message.user .message-content {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-bottom-right-radius: 5px;
}

.message.bot .message-content {
background: white;
color: #333;
border: 1px solid #e0e0e0;
border-bottom-left-radius: 5px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.message-label {
font-size: 12px;
color: #888;
margin-bottom: 5px;
font-weight: 500;
}

.input-area {
padding: 20px;
background: white;
border-top: 1px solid #e0e0e0;
}

.input-wrapper {
display: flex;
gap: 12px;
max-width: 100%;
}

#user_input {
flex: 1;
padding: 15px 20px;
border: 2px solid #e0e0e0;
border-radius: 25px;
font-size: 15px;
outline: none;
transition: border-color 0.3s;
}

#user_input:focus {
border-color: #667eea;
}

.submit-btn {
padding: 15px 30px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 25px;
font-size: 15px;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
}

.submit-btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
}

.submit-btn:active {
transform: translateY(0);
}

.chat-container::-webkit-scrollbar {
width: 8px;
}

.chat-container::-webkit-scrollbar-track {
background: #f1f1f1;
}

.chat-container::-webkit-scrollbar-thumb {
background: #c1c1c1;
border-radius: 4px;
}

.chat-container::-webkit-scrollbar-thumb:hover {
background: #a8a8a8;
}
</style>
</head>
<body>
<div class="chat-container">
{% for item in history %}
{% if item.role == 'user' %}
<p>User: {{ item.content }}</p>
{% elif item.role == 'system' %}
<p>Bot: {{ item.content }}</p>
{% endif %}
{% endfor %}
</div>
<form action="/" method="post">
<label for="user_input">User Input:</label><br>
<input type="text" id="user_input" name="user_input" required><br>
<input type="submit" value="Submit">
</form>
<div class="chat-wrapper">
<div class="chat-header">
🤖 智能聊天助手
</div>
<div class="chat-container" id="chatContainer">
{% for item in history %}
{% if item.role == 'user' %}
<div class="message user">
<span class="message-label">您</span>
<div class="message-content">{{ item.content }}</div>
</div>
{% elif item.role == 'assistant' %}
<div class="message bot">
<span class="message-label">助手</span>
<div class="message-content">{{ item.content }}</div>
</div>
{% endif %}
{% endfor %}
</div>
<div class="input-area">
<form action="/" method="post" class="input-wrapper">
<input type="text" id="user_input" name="user_input" placeholder="输入您的问题..." required autocomplete="off">
<button type="submit" class="submit-btn">发送</button>
</form>
</div>
</div>

<script>
// 自动滚动到底部
const chatContainer = document.getElementById('chatContainer');
chatContainer.scrollTop = chatContainer.scrollHeight;

// 聚焦到输入框
document.getElementById('user_input').focus();
</script>
</body>
</html>
</html>
17 changes: 17 additions & 0 deletions ChatBot/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from openai import OpenAI
from dotenv import load_dotenv, find_dotenv

# 加载 .env 到环境变量
_ = load_dotenv(find_dotenv())

client = OpenAI()

try:
response = client.chat.completions.create(
messages=[{"role": "user", "content": "Hello, this is a test!"}],
model="gpt-4",
)
print("API测试成功!")
print(f"响应: {response.choices[0].message.content}")
except Exception as e:
print(f"API测试失败: {str(e)}")