当前位置: 首页 > wzjs >正文

淘客网站必须备案么中国黄冈网

淘客网站必须备案么,中国黄冈网,河北省和城乡建设厅网站首页,wordpress 地址背景 公司想对接AI智能体,用于客服系统,经过调研和实施,觉得DashScope 符合需求。 阿里云推出的DashScope灵积模型服务为开发者提供了便捷高效的大模型接入方案。本文将详细介绍如何基于DashScope API构建一个功能完善的智能对话系统&#x…

背景

公司想对接AI智能体,用于客服系统,经过调研和实施,觉得DashScope 符合需求。
阿里云推出的DashScope灵积模型服务为开发者提供了便捷高效的大模型接入方案。本文将详细介绍如何基于DashScope API构建一个功能完善的智能对话系统,包含流式对话、工具调用等高级特性。

项目背景与技术选型

我们的项目目标是构建一个企业级智能客服系统,需要满足以下核心需求:

  • 支持多轮自然语言对话
  • 实现低延迟的流式响应
  • 可扩展的工具调用能力
  • 稳定的生产环境部署

经过技术评估,我们选择了阿里云DashScope服务,主要基于以下优势:

  1. 模型多样性:提供QWEN系列等多种大语言模型
  2. API兼容性:兼容OpenAI API格式,降低迁移成本
  3. 性能保障:阿里云基础设施确保服务稳定性
  4. 成本效益:相比自建模型集群更具性价比

模型地址:https://help.aliyun.com/zh/model-studio/videos/yi-large-quick-start

在这里插入图片描述

核心代码实现与优化

基础对话功能实现

我们首先实现了基础的对话功能模块,这是整个系统的核心:


import json
from typing import List, Dict, Optional
import requests
from pydantic import BaseModel
from utils.LogHandler import log# 配置管理使用Pydantic模型,便于验证和文档化
class DashScopeConfig(BaseModel):base_url: str = "https://dashscope.aliyuncs.com/compatible-mode/v1"api_key: strdefault_model: str = "qwen-plus"timeout: int = 30max_retries: int = 3class GPTChatResponse(BaseModel):content: strtool_calls: Optional[List[Dict]] = Nonedef gpt_chat(messages: List[Dict[str, str]],config: DashScopeConfig,model: Optional[str] = None,temperature: Optional[float] = None,max_tokens: Optional[int] = 512
) -> str:"""标准对话API实现:param messages: 对话消息列表:param config: 服务配置:param model: 指定模型,默认使用配置中的default_model:param temperature: 生成多样性控制:param max_tokens: 最大输出token数:return: 模型生成的文本内容"""headers = {'Content-Type': 'application/json','Authorization': f'Bearer {config.api_key}'}payload = {'model': model or config.default_model,'messages': messages,'max_tokens': max_tokens,}if temperature is not None:payload['temperature'] = temperaturefor attempt in range(config.max_retries):try:resp = requests.post(f"{config.base_url}/chat/completions",headers=headers,json=payload,timeout=config.timeout)resp.raise_for_status()json_data = resp.json()if choices := json_data.get('choices'):if len(choices) > 0:return choices[0]['message']['content']raise ValueError("No valid response from model")except requests.exceptions.RequestException as e:log.error(f"Attempt  {attempt + 1} failed: {str(e)}")if attempt == config.max_retries - 1:raiseif  __name__ == "__main__":config = DashScopeConfig(api_key="sk-xxxxxx")messages = [{"role": "system", "content": "你是一名专业客服人员"},{"role": "user", "content": "怎么处理客户争吵问题"}]response = gpt_chat(messages, config)print(response)

在这里插入图片描述

流式对话高级实现

为提升用户体验,我们实现了流式对话功能,并进行了多项优化:


import json
from typing import List, Dict, Optional
import requests
from pydantic import BaseModel
from utils.LogHandler import log# 配置管理使用Pydantic模型,便于验证和文档化
class DashScopeConfig(BaseModel):base_url: str = "https://dashscope.aliyuncs.com/compatible-mode/v1"api_key: strdefault_model: str = "qwen-plus"timeout: int = 30max_retries: int = 3class GPTChatResponse(BaseModel):content: strtool_calls: Optional[List[Dict]] = Nonedef gpt_stream_chat(messages: List[Dict[str, str]],config: DashScopeConfig,model: Optional[str] = None,tools: Optional[List[Dict]] = None,on_content: Optional[callable] = None
) -> GPTChatResponse:"""流式对话实现,支持实时内容处理和工具调用:param messages: 对话消息列表:param config: 服务配置:param model: 指定模型:param tools: 可用工具列表:param on_content: 内容回调函数:return: GPTChatResponse对象"""headers = {'Content-Type': 'application/json','Authorization': f'Bearer {config.api_key}'}payload = {'model': model or config.default_model,'messages': messages,'stream': True}if tools:payload['tools'] = toolscontent_list = []tool_calls = Nonetry:resp = requests.post(f"{config.base_url}/chat/completions",headers=headers,json=payload,stream=True,timeout=config.timeout)resp.raise_for_status()for line in resp.iter_lines():if not line or line == b'data: [DONE]':continuetry:chunk = json.loads(line.decode('utf-8')[6:])delta = chunk['choices'][0]['delta']# 处理推理过程内容if content := delta.get('reasoning_content'):if on_content:on_content(content, 'reasoning')content_list.append(content)# 处理最终回复内容if content := delta.get('content'):if on_content:on_content(content, 'content')content_list.append(content)# 处理工具调用if 'tool_calls' in delta:tool_calls = delta['tool_calls']if tool_calls and 'name' in str(tool_calls):breakexcept json.JSONDecodeError:log.warning(f"Failed  to decode chunk: {line}")continueexcept requests.exceptions.RequestException as e:log.error(f"Stream  request failed: {str(e)}")raisereturn GPTChatResponse(content=''.join(content_list), tool_calls=tool_calls)def handle_stream_content(content: str, content_type: str):print(content, end='', flush=True)  # 实时输出,不换行if __name__ == "__main__":config = DashScopeConfig(api_key="sk-xxxxxxxxxxxxxxxxxxxxxx")messages = [{"role": "system", "content": "你是一名专业客服人员"},{"role": "user", "content": "你好?"}]response = gpt_stream_chat(messages, config, on_content=handle_stream_content)# print(response.content)

在这里插入图片描述

结束

我们成功构建了基于阿里云DashScope的高效智能对话系统。这套方案不仅适用于客服场景,也可扩展应用于智能助手、内容生成等多种AI应用场景。DashScope服务的稳定性和易用性为中小企业快速部署AI能力提供了可靠选择。


文章转载自:

http://RMO4zsRX.nyjgm.cn
http://skZkQ9My.nyjgm.cn
http://5vmLVPqe.nyjgm.cn
http://3Tev6FKm.nyjgm.cn
http://MidTSfec.nyjgm.cn
http://eHLJgi0c.nyjgm.cn
http://t1hngoDR.nyjgm.cn
http://l6ps4COq.nyjgm.cn
http://3EX3Y3CM.nyjgm.cn
http://FWMxbEiB.nyjgm.cn
http://9dILtNUm.nyjgm.cn
http://VsvSpcO4.nyjgm.cn
http://XYIBKcIj.nyjgm.cn
http://CkFLcjre.nyjgm.cn
http://7nJgwI12.nyjgm.cn
http://TDT0x6Aj.nyjgm.cn
http://M3VScC7K.nyjgm.cn
http://8Q1H7nD2.nyjgm.cn
http://40D2QHWk.nyjgm.cn
http://98742JSg.nyjgm.cn
http://PDEYWrmf.nyjgm.cn
http://jx9vtORS.nyjgm.cn
http://5MGCYsKk.nyjgm.cn
http://8qHlkTb2.nyjgm.cn
http://9Qp2p4Yp.nyjgm.cn
http://E1WkRBey.nyjgm.cn
http://KfHly5VE.nyjgm.cn
http://SiimUqI7.nyjgm.cn
http://LQwQ6FrI.nyjgm.cn
http://rPsnTGre.nyjgm.cn
http://www.dtcms.com/wzjs/689049.html

相关文章:

  • 网站建设常见问题解决方案wordpress提示框插件
  • 文山网站开发工厂外发订单哪里去找
  • 郑州哪个公司专业做网站深圳人社局官网
  • 房产经纪人怎么做网站如何刷关键词指数
  • 株洲做网站那家好常见的微信营销方式有哪些
  • 自适用网站的建设营销型网站和展示型网站的区别
  • 网站建设收获与体会网站图片自动下载
  • 高校英文网站建设大连seo
  • 刚刚廊坊发生大事了沈阳网站推广优化
  • 网站建设的规划和设计可视化设计最重要的是确定网站的
  • 网站免费正能量软件下载视频福清市百度seo
  • 0元建设黑网站微信h5网站开发
  • 做美团旅游网站多少钱制作公众号的编辑器
  • 网站为什么做301企业信用信息查询公示系统官网
  • 海北州网站建设公司做商城网站要哪些流程
  • 网站设计小技巧网站建设的技能有哪些方面
  • 专门做团购的网站企业网站管理系统设计与实现
  • 深圳做响应式网站设计专门教做甜品的网站
  • 新手做亚马逊要逛哪些网站营销型网站建设公司是干嘛的
  • wordpress导航站的源码建设银行网站查询余额
  • 做影视网站犯法吗品牌建设与质量培训
  • 招聘网站排行榜wordpress维护服务器
  • 合众商道网站开发上传网站内容
  • 网站图片怎么做白色背景创建地址怎么弄
  • 郑州做网站和域名公司里面php开发一个网站的流程
  • 网站建设哪些推广小程序的营销策略
  • 学做视频的网站有哪些内容wordpress注册页面404
  • 企业招聘网站排行榜表白网页在线生成网站源码
  • h5购物网站模板wordpress 导入html
  • 请人制作一个网站需要多少钱网站报价单