"""
农技智能问答
基于 Qwen3.5 大模型的农业技术知识问答应用
"""
import httpx
import streamlit as st
from config import CHAT_API_URL, CHAT_MODEL, HEADERS
# ─── Page Config ────────────────────────────────────────────────────────────
st.set_page_config(
page_title="农技智能问答",
page_icon="🌾",
layout="wide",
initial_sidebar_state="expanded",
)
# ─── Custom CSS ──────────────────────────────────────────────────────────────
st.markdown("""
""", unsafe_allow_html=True)
# ─── Quick Questions ─────────────────────────────────────────────────────────
QUICK_QUESTIONS = [
"水稻稻瘟病怎么防治?什么时候打药效果最好?",
"小麦叶片上出现铁锈色的粉状物是什么病?怎么处理?",
"如何判断玉米是否需要灌溉?有哪些判断方法?",
"如何种榴莲?怎样成熟快?",
]
# ─── Sidebar ─────────────────────────────────────────────────────────────────
with st.sidebar:
st.markdown('
🌾 农技问答
', unsafe_allow_html=True)
st.markdown('AGRICULTURE QA SYSTEM v1.0
', unsafe_allow_html=True)
st.markdown("---")
st.markdown('', unsafe_allow_html=True)
temperature = st.slider("Temperature", 0.0, 1.0, 0.7, 0.1)
top_p = st.slider("Top P", 0.0, 1.0, 0.8, 0.1)
enable_thinking = st.checkbox("开启思维链", value=True)
st.markdown('', unsafe_allow_html=True)
for q in QUICK_QUESTIONS:
if st.button(q, key=q):
st.session_state.user_input = q
# ─── Main Layout ─────────────────────────────────────────────────────────────
st.markdown("""
Powered by Qwen3.5 | 支持思维链推理的农业技术知识问答
""", unsafe_allow_html=True)
st.markdown("
", unsafe_allow_html=True)
# 输入区域
user_input = st.text_area(
"请输入您的农业问题:",
value=st.session_state.get("user_input", ""),
height=120,
placeholder="例如:水稻稻瘟病怎么防治?",
)
if st.button("提交", type="primary") and user_input.strip():
with st.spinner("模型正在思考中..."):
try:
payload = {
"model": CHAT_MODEL,
"messages": [{"role": "user", "content": user_input.strip()}],
"temperature": temperature,
"top_p": top_p,
"presence_penalty": 1.5,
"chat_template_kwargs": {"enable_thinking": enable_thinking},
}
resp = httpx.post(
CHAT_API_URL, headers=HEADERS, json=payload, timeout=120
)
resp.raise_for_status()
data = resp.json()
choice = data["choices"][0]["message"]
reasoning = choice.get("reasoning_content", "")
content = choice["content"]
usage = data["usage"]
# 显示回答
st.markdown(f"""
""", unsafe_allow_html=True)
# 显示思考过程(可折叠)
if reasoning and enable_thinking:
with st.expander("🧠 查看思考过程"):
st.markdown(reasoning)
# Token 用量
st.markdown(f"""
Token 用量 — 输入: {usage['prompt_tokens']} | \
输出: {usage['completion_tokens']} | \
合计: {usage['total_tokens']}
""", unsafe_allow_html=True)
except httpx.HTTPStatusError as e:
st.markdown(
f'请求失败 (HTTP {e.response.status_code}): {e.response.text}
',
unsafe_allow_html=True,
)
except Exception as e:
st.markdown(
f'请求异常: {e}
',
unsafe_allow_html=True,
)
# ─── Footer ───────────────────────────────────────────────────────────────────
st.markdown("
", unsafe_allow_html=True)
st.markdown("""
Qwen3.5 + Thinking Chain | 农技智能问答系统
""", unsafe_allow_html=True)