【书生·浦语】internlm2-chat-1.8b实战教程用Ollama构建本地化AI客服原型本文介绍如何使用Ollama快速部署internlm2-chat-1.8b模型构建一个本地化的AI客服系统原型无需复杂配置10分钟即可上手。1. 环境准备与快速部署在开始之前我们先简单了解一下今天要用到的工具和模型。Ollama是一个开源的本地大模型运行框架它让普通用户也能轻松在个人电脑上运行各种AI模型不需要复杂的技术背景。就像在手机上下载APP一样简单。internlm2-chat-1.8b是书生·浦语团队开发的18亿参数对话模型专门针对聊天场景优化。它虽然体积小巧但在理解指令、多轮对话方面表现不错特别适合做客服机器人这类应用。1.1 系统要求要顺利运行这个模型你的电脑需要满足以下配置操作系统Windows 10/11、macOS 10.15 或 Linux Ubuntu 18.04内存至少8GB RAM推荐16GB以获得更好体验存储空间5GB可用空间用于存放模型文件显卡可选有独立显卡如NVIDIA GTX 1060以上会运行更快1.2 安装Ollama根据你的操作系统选择对应的安装方式Windows系统访问Ollama官网下载Windows版本安装包双击安装包按照提示完成安装安装完成后Ollama会自动在后台运行macOS系统# 使用Homebrew安装 brew install ollama # 或者下载dmg安装包手动安装Linux系统# 使用一键安装脚本 curl -fsSL https://ollama.com/install.sh | sh安装完成后打开命令行终端输入ollama --version如果显示版本号说明安装成功。2. 部署internlm2-chat-1.8b模型现在我们来部署核心的AI模型这个过程非常简单。2.1 拉取模型文件在命令行中执行以下命令ollama pull internlm2:1.8b这个命令会从Ollama的模型库中下载internlm2-chat-1.8b模型。下载时间取决于你的网络速度通常需要5-15分钟。2.2 验证模型安装下载完成后运行以下命令测试模型是否正常工作ollama run internlm2:1.8b然后在出现的提示符后输入你好如果模型能够正常回复说明安装成功。按CtrlD退出测试。3. 构建基础客服系统有了运行正常的模型我们现在来构建一个简单的客服系统原型。3.1 创建客服对话脚本创建一个名为customer_service.py的文件输入以下代码import requests import json class AICustomerService: def __init__(self): self.api_url http://localhost:11434/api/generate self.model_name internlm2:1.8b def generate_response(self, user_input, conversation_history[]): 生成客服回复 # 构建客服专用的提示词 prompt f你是一个专业的在线客服助手请用友好、专业的态度回答用户问题。 对话历史 {conversation_history} 当前问题{user_input} 请提供有帮助的回复 payload { model: self.model_name, prompt: prompt, stream: False } try: response requests.post(self.api_url, jsonpayload) result response.json() return result[response] except Exception as e: return f抱歉服务暂时不可用{str(e)} # 使用示例 if __name__ __main__: 客服 AICustomerService() # 模拟客服对话 questions [ 你们公司的退货政策是什么, 商品质量有问题怎么办, 运费怎么计算 ] for question in questions: print(f用户{question}) response 客服.generate_response(question) print(f客服{response}) print(- * 50)3.2 运行客服系统首先确保Ollama服务正在运行然后在命令行中执行python customer_service.py你会看到模型针对不同的客服常见问题生成了相应的回复。4. 进阶功能实现基础客服系统运行起来后我们可以添加一些实用功能来提升体验。4.1 添加多轮对话记忆真实的客服对话需要记住之前的交流内容修改我们的脚本class AICustomerService: def __init__(self): self.api_url http://localhost:11434/api/generate self.model_name internlm2:1.8b self.conversation_history [] def add_to_history(self, role, message): 添加对话到历史记录 self.conversation_history.append(f{role}{message}) # 保持最近10轮对话避免过长 if len(self.conversation_history) 20: self.conversation_history self.conversation_history[-20:] def generate_response(self, user_input): 生成考虑对话历史的回复 self.add_to_history(用户, user_input) prompt f作为客服助手请根据对话历史回应用户问题。 对话历史 {\n.join(self.conversation_history)} 请提供专业、有帮助的回复 payload { model: self.model_name, prompt: prompt, stream: False } try: response requests.post(self.api_url, jsonpayload) result response.json() ai_response result[response] self.add_to_history(客服, ai_response) return ai_response except Exception as e: return f系统暂时无法响应{str(e)}4.2 添加简单Web界面为了让客服系统更易用我们可以创建一个简单的网页界面。创建app.pyfrom flask import Flask, render_template, request, jsonify import requests app Flask(__name__) app.route(/) def index(): return render_template(chat.html) app.route(/chat, methods[POST]) def chat(): user_message request.json.get(message) # 调用Ollama API payload { model: internlm2:1.8b, prompt: f作为客服助手请专业地回答{user_message}, stream: False } try: response requests.post(http://localhost:11434/api/generate, jsonpayload) result response.json() return jsonify({response: result[response]}) except Exception as e: return jsonify({response: f服务异常{str(e)}}) if __name__ __main__: app.run(debugTrue, port5000)创建templates/chat.html!DOCTYPE html html head titleAI客服系统/title style .chat-container { max-width: 800px; margin: 0 auto; } .message { padding: 10px; margin: 5px; border-radius: 5px; } .user { background: #e3f2fd; text-align: right; } .assistant { background: #f5f5f5; } /style /head body div classchat-container h2AI客服助手/h2 div idchat-messages/div input typetext iduser-input placeholder请输入您的问题... button onclicksendMessage()发送/button /div script async function sendMessage() { const input document.getElementById(user-input); const message input.value.trim(); if (!message) return; // 添加用户消息 addMessage(user, message); input.value ; // 获取AI回复 const response await fetch(/chat, { method: POST, headers: { Content-Type: application/json }, body: JSON.stringify({ message: message }) }); const data await response.json(); addMessage(assistant, data.response); } function addMessage(role, text) { const div document.createElement(div); div.className message ${role}; div.textContent text; document.getElementById(chat-messages).appendChild(div); } /script /body /html运行Web应用python app.py然后在浏览器中访问http://localhost:5000就可以通过网页界面与客服AI对话了。5. 实用技巧与优化建议在实际使用中这里有一些提升客服效果的建议5.1 提示词优化技巧好的提示词能让模型表现更好针对客服场景可以这样优化def create_customer_service_prompt(user_input, history): return f你是{公司名称}的专业客服助手请遵循以下准则 1. 始终保持友好、耐心、专业的态度 2. 准确理解用户问题提供具体解决方案 3. 如无法解决引导用户联系人工客服 4. 回答要简洁明了避免冗长 公司信息 - 退货期限30天内 - 客服时间9:00-18:00 - 紧急联系400-123-4567 对话历史 {history} 用户问题{user_input} 请提供有帮助的回复5.2 常见问题处理对于客服常见问题可以设置标准回答模板common_questions { 退货政策: 我们提供30天无理由退货服务请保持商品完好并携带购买凭证。, 运费问题: 订单满99元免运费不满99元收取10元运费。, 客服时间: 人工客服工作时间是工作日9:00-18:00其他时间可留言。 } def check_common_questions(user_input): for keyword, response in common_questions.items(): if keyword in user_input: return response return None6. 常见问题解答在实际部署和使用过程中你可能会遇到以下问题Q模型运行速度很慢怎么办A可以尝试量化版本使用ollama pull internlm2:1.8b-q4下载4位量化版本体积更小速度更快。Q如何提高回答质量A在提示词中提供更多上下文信息明确客服角色和回答要求设置回答格式模板。Q支持中文对话吗Ainternlm2-chat-1.8b原生支持中文在中文对话方面表现良好。Q可以部署到服务器吗A完全可以在云服务器上安装Ollama后通过修改API地址如http://服务器IP:11434即可远程调用。Q如何扩展更多功能A可以集成知识库检索、情感分析、多模态支持等功能打造更智能的客服系统。7. 总结通过本教程我们成功使用Ollama和internlm2-chat-1.8b构建了一个本地化的AI客服系统原型。这个方案有几个显著优势部署简单只需要几条命令就能完成环境搭建无需复杂配置成本低廉完全本地运行无需支付API调用费用隐私安全所有数据都在本地处理不会泄露对话内容可定制性强可以根据具体业务需求调整提示词和功能虽然1.8B的模型在某些复杂场景下可能还有局限但对于大多数基础客服需求已经足够使用。你可以在此基础上继续扩展比如添加知识库检索、多轮对话管理、情感分析等功能打造更加强大的智能客服系统。最重要的是这个方案让你完全掌控自己的AI客服系统无需依赖外部服务商既经济又安全。获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。