> 🎯 **适合人群:** Python 初学者、想入门 AI 应用开发的同学、毕设需要 AI 方向的大学生

> ⏱️ **阅读时间:** 约 15 分钟

> 💡 **学完能做:** 一个带上下文记忆 + 知识库检索的智能问答 Web 应用

## 一、前言

2026年,AI 大模型已经从"概念"走向了"落地"。不管是面试还是实际开发,**能调通大模型 API、搭建 AI 应用** 已经成为开发者的必备技能。

但很多同学一上来就去看 Transformer 源码、训练自己的模型……其实完全没必要!

**90% 的 AI 应用场景,只需要学会「调 API + 做应用层」就够了。**

今天这篇文章,我用 **Python** 带你从零搭建一个完整的智能问答系统,包含:

- ✅ 调用大模型 API(支持通义千问 / 文心一言 / GLM 等国产模型)

- ✅ Web 对话界面(基于 Gradio,5 行代码搞定)

- ✅ 上下文多轮对话记忆

- ✅ 知识库检索增强(RAG 简易版)

**全程 Python,无需前端基础,30 分钟跑通。**

---

## 二、环境准备

### 2.1 Python 版本

建议 Python 3.10 及以上:

```bash

python --version  # 确保 >= 3.10

```

### 2.2 安装依赖

```bash

pip install openai gradio chromadb tiktoken

```

| 库 | 作用 |

|---|------|

| `openai` | 统一调用各大模型 API(兼容 OpenAI 接口格式) |

| `gradio` | 快速搭建 Web 对话界面 |

| `chromadb` | 向量数据库,用于知识库检索 |

| `tiktoken` | Token 计数 |

### 2.3 获取 API Key

这里以 **通义千问(DashScope)** 为例,免费额度非常充足:

1. 打开 [DashScope 控制台](https://dashscope.console.aliyun.com/)

2. 注册/登录阿里云账号

3. 开通 DashScope 服务

4. 在「API-KEY 管理」中创建一个 Key

> 💡 **其他选择:**

> - 百度文心一言:https://console.bce.baidu.com/qianfan

> - 智谱 GLM:https://open.bigmodel.cn/

> - DeepSeek:https://platform.deepseek.com/

>

> 都有免费额度,接口格式基本兼容,换个 base_url 和 key 就行。

---

## 三、核心实现

### 3.1 基础对话(单轮)

先跑通最简单的调用:

```python

from openai import OpenAI

# 初始化客户端(以通义千问为例)

client = OpenAI(

    api_key="你的API_KEY",

    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"

)

def chat(user_message: str) -> str:

    """单轮对话"""

    response = client.chat.completions.create(

        model="qwen-plus",  # 通义千问模型

        messages=[

            {"role": "system", "content": "你是一个专业的技术助手,回答简洁准确。"},

            {"role": "user", "content": user_message}

        ],

        temperature=0.7,

        max_tokens=1024

    )

    return response.choices[0].message.content

# 测试

print(chat("Python 的 GIL 是什么?"))

```

运行后你会看到大模型的回复。**到这里,你已经会调 API 了!**

---

### 3.2 多轮对话(带上下文记忆)

关键在于把历史消息一起传给 API:

```python

class ChatBot:

    def __init__(self, model="qwen-plus"):

        self.client = OpenAI(

            api_key="你的API_KEY",

            base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"

        )

        self.model = model

        self.history = [

            {"role": "system", "content": "你是一个专业的技术助手,回答简洁准确,支持中英文。"}

        ]

    def chat(self, user_message: str) -> str:

        """多轮对话,自动维护上下文"""

        self.history.append({"role": "user", "content": user_message})

        response = self.client.chat.completions.create(

            model=self.model,

            messages=self.history,

            temperature=0.7,

            max_tokens=1024

        )

        assistant_message = response.choices[0].message.content

        self.history.append({"role": "assistant", "content": assistant_message})

        return assistant_message

    def clear_history(self):

        """清空对话历史"""

        self.history = self.history[:1]  # 只保留 system prompt


 

# 测试多轮对话

bot = ChatBot()

print(bot.chat("用Python怎么读取Excel文件?"))

print(bot.chat("那写入呢?"))  # 它会记住你在说 Excel

print(bot.chat("能给个完整示例吗?"))  # 继续上下文

```

> 🔑 **核心原理:** 每次请求把完整的历史消息列表发过去,模型就能"记住"之前聊了什么。

---

### 3.3 知识库检索增强(RAG 简易版)

这是进阶功能——让大模型能"查阅"你自己的文档,而不是只靠它自己的知识。

**RAG 流程:**

```

用户提问 → 从知识库中检索相关内容 → 把相关内容和问题一起发给大模型 → 生成回答

```

```python

import chromadb

from chromadb.utils import embedding_functions

class RAGChatBot(ChatBot):

    def __init__(self, model="qwen-plus"):

        super().__init__(model)

        # 使用默认的嵌入函数(基于 sentence-transformers)

        self.embed_fn = embedding_functions.DefaultEmbeddingFunction()

        self.chroma_client = chromadb.Client()

        self.collection = self.chroma_client.create_collection(

            name="knowledge_base",

            embedding_function=self.embed_fn

        )

    def add_documents(self, docs: list[str], ids: list[str] = None):

        """添加文档到知识库"""

        if ids is None:

            ids = [f"doc_{i}" for i in range(len(docs))]

        self.collection.add(documents=docs, ids=ids)

        print(f"✅ 已添加 {len(docs)} 条文档到知识库")

    def retrieve(self, query: str, top_k: int = 3) -> list[str]:

        """从知识库中检索相关文档"""

        results = self.collection.query(query_texts=[query], n_results=top_k)

        return results["documents"][0] if results["documents"] else []

    def chat_with_rag(self, user_message: str) -> str:

        """带知识库检索的对话"""

        # 1. 检索相关文档

        relevant_docs = self.retrieve(user_message)

        context = "\n".join(relevant_docs) if relevant_docs else "未找到相关内容"

        # 2. 构建增强 prompt

        enhanced_message = f"""请基于以下参考资料回答用户问题。如果参考资料中没有相关内容,请根据你的知识回答并说明。

【参考资料】

{context}

【用户问题】

{user_message}"""

        # 3. 调用大模型

        self.history.append({"role": "user", "content: enhanced_message"})

        response = self.client.chat.completions.create(

            model=self.model,

            messages=self.history,

            temperature=0.7,

            max_tokens=1024

        )

        assistant_message = response.choices[0].message.content

        self.history.append({"role": "assistant", "content": assistant_message})

        return assistant_message


 

# 测试 RAG

rag_bot = RAGChatBot()

# 添加知识库文档

rag_bot.add_documents([

    "张三,男,25岁,软件工程专业,擅长Python和Java,目前在某互联网公司担任后端开发工程师。",

    "公司规定:年假入职满一年后有5天,满三年后有10天,满五年后有15天。年假不可跨年累积。",

    "项目A的技术栈为:前端Vue3,后端Flask,数据库MySQL,部署在阿里云ECS上。",

    "2026年度KPI考核标准:代码质量占30%,项目交付占40%,团队协作占20%,技术创新占10%。",

])

# 测试

print(rag_bot.chat_with_rag("年假有几天?"))

print(rag_bot.chat_with_rag("项目A用了什么数据库?"))

```

---

## 四、Web 界面(Gradio)

现在把上面的功能包装成一个漂亮的 Web 界面:

```python

import gradio as gr

# 创建 RAG 机器人实例

bot = RAGChatBot()

# 预置知识库

bot.add_documents([

    "张三,男,25岁,软件工程专业,擅长Python和Java。",

    "公司规定:年假入职满一年后有5天,满三年后有10天,满五年后有15天。",

    "项目A的技术栈为:前端Vue3,后端Flask,数据库MySQL。",

    "2026年度KPI考核:代码质量30%,项目交付40%,团队协作20%,技术创新10%。",

])


 

def respond(message, chat_history):

    """Gradio 回调函数"""

    bot_response = bot.chat_with_rag(message)

    chat_history.append((message, bot_response))

    return "", chat_history


 

def clear_chat():

    """清空对话"""

    bot.clear_history()

    return []


 

# 构建界面

with gr.Blocks(title="AI 智能问答系统", theme=gr.themes.Soft()) as demo:

    gr.Markdown("# 🤖 AI 智能问答系统")

    gr.Markdown("基于大模型 + RAG 知识库检索 | 支持多轮对话")

    chatbot = gr.Chatbot(

        label="对话",

        height=500,

        bubble_full_width=False

    )

    with gr.Row():

        msg = gr.Textbox(

            label="输入你的问题",

            placeholder="请输入问题...",

            scale=4

        )

        submit_btn = gr.Button("发送", variant="primary", scale=1)

    clear_btn = gr.Button("🗑️ 清空对话")

    # 绑定事件

    msg.submit(respond, [msg, chatbot], [msg, chatbot])

    submit_btn.click(respond, [msg, chatbot], [msg, chatbot])

    clear_btn.click(clear_chat, outputs=[chatbot])

# 启动

demo.launch(server_name="0.0.0.0", server_port=7860)

```

运行后打开浏览器访问 `http://localhost:7860`,你就能看到一个完整的 AI 问答界面了!

---

## 五、效果展示

启动后界面如下:

```

┌─────────────────────────────────────┐

│  🤖 AI 智能问答系统                   │

│  基于大模型 + RAG 知识库检索           │

├─────────────────────────────────────┤

│                                     │

│  👤 年假有几天?                      │

│                                     │

│  🤖 根据公司规定,年假制度如下:        │

│     - 入职满1年:5天                  │

│     - 入职满3年:10天                 │

│     - 入职满5年:15天                 │

│     年假不可跨年累积。                │

│                                     │

├─────────────────────────────────────┤

│  [请输入问题...]              [发送]  │

│  [🗑️ 清空对话]                       │

└─────────────────────────────────────┘

```

---

## 六、完整项目结构

```

ai-qa-system/

├── app.py              # 主程序(上面的完整代码)

├── requirements.txt    # 依赖列表

└── knowledge/          # 知识库文档(可扩展)

    ├── company.txt

    └── project.txt

```

**requirements.txt:**

```

openai>=1.0.0

gradio>=4.0.0

chromadb>=0.4.0

tiktoken>=0.5.0

```

---

## 七、常见问题 & 踩坑记录

### Q1: 报错 `openai.APIConnectionError`

**原因:** 网络问题,API 地址连不上。

**解决:** 检查 `base_url` 是否正确,确认网络能访问对应服务。

### Q2: 模型回复全是英文

**解决:** 在 system prompt 中明确要求中文回复:

```python

{"role": "system", "content": "你是一个专业的技术助手,请始终使用中文回答。"}

```

### Q3: Token 超限

**原因:** 对话历史太长。

**解决:** 限制历史轮数:

```python

# 只保留最近 10 轮对话

if len(self.history) > 21:  # 1 system + 10 * 2 messages

    self.history = [self.history[0]] + self.history[-20:]

```

### Q4: ChromaDB 安装报错

**解决:**

```bash

pip install chromadb --no-cache-dir

# 如果还是报错,尝试:

pip install chromadb-client

```

---

## 八、扩展方向

学完本项目后,你可以继续拓展:

1. **接入微信/飞书机器人** — 用 `wechaty` 或飞书开放平台

2. **PDF/Word 文档解析** — 用 `PyPDF2` + `python-docx` 自动导入知识库

3. **多用户支持** — Flask/FastAPI 加上用户系统

4. **模型微调** — 用 LoRA 对小模型进行领域微调

5. **部署上线** — Docker + 云服务器,做成正式产品

---

## 九、总结

本文带你从零实现了:

| 功能 | 技术方案 |

|------|---------|

| 大模型调用 | OpenAI SDK(兼容接口) |

| 多轮对话 | 消息历史列表 |

| 知识库检索 | ChromaDB 向量数据库 |

| Web 界面 | Gradio |

**核心思想就一句话:大模型应用 = API 调用 + 应用层封装。**

不需要懂算法,不需要 GPU,一台普通电脑就能做出 AI 产品。

---

> 📌 **如果这篇文章对你有帮助,请点赞 👍 + 收藏 ⭐ + 关注,我会持续分享更多 AI 实战项目!**

>

> 📧 有问题欢迎评论区交流,我会一一回复。

---

**标签:** Python、AI大模型、智能问答系统、Gradio、RAG、通义千问、大模型应用开发、Python实战、AI应用、2026

更多推荐