不需要本地部署大模型,modelscope-agent加qwen-max免费搭建自己的定制机器人

发布时间 2023-12-28 15:29:16作者: HaruSuzumiya

最近阿里开源了通用大模型qwen-72b,正在为怎么本地化部署发愁,转眼看到提供了qwen-max相关接口的免费试用(据说就是基于qwen-72b大模型),这就来体验一番。

1.前期准备

开通阿里云灵积平台,并创建api-key

image.png

2.环境搭建

python:3.10+;

pydantic 2.0以上,老版本pydantic会报一个tool_schema.model_dump_json的函数错误,可以通过相关命令升级:

 
css
复制代码
pip install --upgrade pydantic
pip install --upgrade fastapi

3.项目部署

拉取项目代码,并安装相关依赖

 
bash
复制代码
git clone https://github.com/modelscope/modelscope-agent.git
cd modelscope-agent
pip install -r requirements.txt

将\apps\agentfabric目录下面的所有文件复制到项目根目录,并安装相关依赖

 
复制代码
pip install -r requirements.txt

发现有两个文件app.py和appBot.py,先启动app.py(实际差不多,appBot.py没有左边的配置栏),打开创建的网址可以发现提示api_key错误,我们将阿里云灵积平台创建的api-key填写到环境变量中并重启服务。

这时候可以正常使用了,点击config可以修改配置信息(根据自己需求填写)

image.png 实际上在/config/build_config.json文件里直接填写效果更好,删除缓存也不怕丢失

 
json
复制代码
{
  "name": "学业规划助手",
  "avatar": "custom_bot_avatar.png",
  "description": "一个专为学生设计的学业规划助手,通过个性化的测试和分析帮助你找到最适合的专业和学校。",
  "instruction": "1. 理解并回应用户的指令;2. 提供专业的学术知识和技能的简易测试;3. 根据用户的需求和测试结果生成个性化适配报告;4. 基于用户需求和偏好推荐适合的专业和学校;5. 考虑用户对地区和国家的偏好进行更精准的推荐;6.当用户需要完整智能评测时提供完整智能评测的网址;7.给出用户日常学习和生活的建议辅导",
  "language": "zh",
  "prompt_recommend": [
    "你可以帮我生成一个关于职业能力的简易测试吗?",
    "你会推荐我学习哪个专业的课程?",
    "你能分析一下我对专业的适配度吗?",
    "能否推荐一些大学给我?",
    "完整智能评估"
  ],
  "knowledge": [],
  "tools": {
    "get_test_url":{
      "name": "完整智能评估",
      "is_active": true,
      "use": true 
    }
  },
  "model": "qwen-max"
}

保存重启并体验一下

image.png

4.自定义工具类加载

在tool文件夹内创建get_test_url.py

 
python
复制代码
from .tool import Tool
class GetTestUrl(Tool):
    name = "get_test_url"
    description = "该工具用户返回智能评估的网址。"
    description += """
    下面是一些对话场景:
    场景1:
    <用户>: 智能评估
    <助手>: 好的,正在查询中
    
    场景2:
    <用户>:查看智能评估的网站
    <助手>: 好的,正在查询中
    """
    parameters = [
        {
            'name': 'test_url',
            'description': '查询智能评估的网址',
            'required': False
        }
    ]
    def __call__(self, remote=False, *args, **kwargs):
        if self.is_remote_tool or remote:
            return self._remote_call(*args, **kwargs)
        else:
            return self._local_call(*args, **kwargs)

    def _remote_call(self, *args, **kwargs):
        pass

    def _local_call(self, *args, **kwargs):
        test_url = kwargs.get("test_url", "")
        result = {
            "test_url": 'http://www.baidu.com',
        }
        return {"result": result}

修改对应的config文件/config/tool_config.json

 
json
复制代码
  "modelscope_text-translation-en2zh": {
      "name": "英译中",
      "url": "https://api-inference.modelscope.cn/api-inference/v1/models/damo/nlp_csanmt_translation_en2zh",
      "use": false,
      "is_active": false,
      "is_remote_tool": true
  },
  "modelscope_text-translation-zh2en": {
      "name": "中译英",
      "url": "https://api-inference.modelscope.cn/api-inference/v1/models/damo/nlp_csanmt_translation_zh2en",
      "use": false,
      "is_active": false,
      "is_remote_tool": true
  },
  "get_test_url":{
    "name": "完整智能评估",
    "is_active": true,
    "use": true 
  }

修改/config/build_config.json(上文已经添加过了,不在赘述)。

修改/tools/init.py

 
javascript
复制代码
from .amap_weather import AMAPWeather
from .code_interperter import CodeInterpreter
from .code_interpreter_jupyter import CodeInterpreterJupyter
from .hf_tool import HFTool
from .image_chat_tool import ImageChatTool
from .pipeline_tool import ModelscopePipelineTool
from .plugin_tool import LangchainTool
from .qwen_vl import QWenVL
from .style_repaint import StyleRepaint
from .text_address_tool import TextAddressTool
from .text_ie_tool import TextInfoExtractTool
from .text_ner_tool import TextNerTool
from .text_to_image_tool import TextToImageTool
from .text_to_speech_tool import TexttoSpeechTool
from .text_to_video_tool import TextToVideoTool
from .tool import Tool
from .translation_en2zh_tool import TranslationEn2ZhTool
from .translation_zh2en_tool import TranslationZh2EnTool
from .web_browser import WebBrowser
from .web_search import WebSearch
from .wordart_tool import WordArtTexture
from .get_test_url import GetTestUrl
TOOL_INFO_LIST = {
    'modelscope_text-translation-zh2en': 'TranslationZh2EnTool',
    'modelscope_text-translation-en2zh': 'TranslationEn2ZhTool',
    'modelscope_text-ie': 'TextInfoExtractTool',
    'modelscope_text-ner': 'TextNerTool',
    'modelscope_text-address': 'TextAddressTool',
    'image_gen': 'TextToImageTool',
    'modelscope_video-generation': 'TextToVideoTool',
    'modelscope_image-chat': 'ImageChatTool',
    'modelscope_speech-generation': 'TexttoSpeechTool',
    'amap_weather': 'AMAPWeather',
    'code_interpreter': 'CodeInterpreterJupyter',
    'wordart_texture_generation': 'WordArtTexture',
    'web_search': 'WebSearch',
    'web_browser': 'WebBrowser',
    'qwen_vl': 'QWenVL',
    'style_repaint': 'StyleRepaint',
    'get_test_url': 'GetTestUrl',
}

重启服务看下效果

image.png 完工~


作者:suzumiyahr
链接:https://juejin.cn/post/7317252368546054184
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。