微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B

1、实践流程

(1)使用Fastchat框架搭建运行ChatGLM模型openai兼容API
(2)测试AutoGen加载ChatGLM2

2、部署FastChat

2.1、创建虚拟环境
# Python version >= 3.8, < 3.12
conda create -n fastchat python=3.10 -y
conda activate fastchat 
2.2、下载FastChat
git clone https://github.com/lm-sys/FastChat.git
2.3、依赖框架安装
pip3 install torch==2.0.0+cu117 torchvision==0.15.1+cu117 torchaudio==2.0.1 --index-url https://download.pytorch.org/whl/cu117
pip3 install cpm_kernels -i https://pypi.mirrors.ustc.edu.cn/simple --trusted-host=pypi.mirrors.ustc.edu.cn
pip3 install --upgrade pip -i https://pypi.mirrors.ustc.edu.cn/simple --trusted-host=pypi.mirrors.ustc.edu.cn
cd FastChat & pip3 install -e ".[model_worker,webui]" -i https://pypi.mirrors.ustc.edu.cn/simple --trusted-host=pypi.mirrors.ustc.edu.cn
2.4、加载模型

需要分别启动控制器、模型执行器和API服务。

# controller
cd FastChat
conda activate fastchat 
python -m fastchat.serve.controller --host 0.0.0.0

微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第1张图片

# model_worker 
cd FastChat
conda activate fastchat 
python -m fastchat.serve.model_worker --model-path ./ChatGLM-6B --host 0.0.0.0

微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第2张图片

# api_server
cd FastChat
conda activate fastchat 
python -m fastchat.serve.openai_api_server --host 0.0.0.0 --port 8001

在这里插入图片描述

3、Autogen测试

安装Autogen框架:

pip install pyautogen

编写如下代码:

from autogen import oai
from autogen import AssistantAgent, UserProxyAgent, config_list_from_json

def TestAutoGen():
    config_list = [
        {
            "model": "chatglm2-6b",
            "base_url": "http://127.0.0.1:8001/v1",
            #"api_type": "open_ai",#该行要注释掉,不然报错
            "api_key": "NULL"
        }
    ]
    assistant = AssistantAgent("assistant", llm_config={
                               "config_list": config_list})
    user_proxy = UserProxyAgent(
        "user_proxy", code_execution_config={"work_dir": "coding"})
    user_proxy.initiate_chat(
        assistant, message="用react.js写一个用户登录程序")


if __name__ == '__main__':
    TestAutoGen()

测试效果:
微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第3张图片
微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第4张图片

Web查询:查询XX的天气
import autogen

def TestGroupChat():
    config_list_gpt = [
        {
            "model": "chatglm2-6b",
            "base_url": "http://127.0.0.1:8001/v1",
            "api_key": "NULL"
        }
    ]
    llm_config = {"config_list": config_list_gpt, "seed": 42}
    # create an AssistantAgent instance named "assistant"
    assistant = autogen.AssistantAgent(
        name="assistant",
        llm_config=llm_config
    )
    # create a UserProxyAgent instance named "user_proxy"
    user_proxy = autogen.UserProxyAgent(
        name="user_proxy",
        human_input_mode="TERMINATE",
        max_consecutive_auto_reply=10,
        is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
        code_execution_config={"work_dir": "web","use_docker":""},
        llm_config=llm_config,
        system_message="""Reply TERMINATE if the task has been solved at full satisfaction.Otherwise, reply CONTINUE, or the reason why the task is not solved yet."""
    )
    user_proxy.initiate_chat(
        assistant,
        message="""在baidu.com上查找今天石家庄的天气,并找到其明天的天气预报""",
    )

if __name__ == '__main__':
    TestGroupChat()

可以看到模型编写了python代码尝试去查询。可以看到它很努力的去查了,很遗憾并没有成功返回我要的信息。感觉是受限于基座模型的能力

微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第5张图片
微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第6张图片
微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第7张图片
微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第8张图片

使用ChatGLM3模型同样运行不出来(api不对):
微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第9张图片

4、问题解决:

问题1:

启动FastChat的controller时报错:

ERROR: [Errno 99] error while attempting to bind on address ('::1', 21001, 0, 0): cannot assign requested address

这时,需要在需要在启动命令后加 - -host 0.0.0.0

python -m fastchat.serve.controller --host 0.0.0.0
问题2:
AttributeError: 'ChatGLMTokenizer' object has no attribute 'tokenizer'. Did you mean: 'tokenize'

修改transformers的版本:pip install transformers == 4.33.2

问题3:

有文档里autogen测试代码中ChatGLM的请求地址前参数写的是api_base,运行代码会报错,不能识别该参数。

Completions.create() got an unexpected keyword argument 'api_base'

这时,需要把api_base要改成base_url。

问题4:

有文档里autogen测试代码中ChatGLM配置里包括api_type参数,运行代码会报错,不能识别该参数。

Completions.create() got an unexpected keyword argument 'api_type'

这时,需要把该参数注释掉。

问题5:

运行代码会报错,测试代码中ChatGLM配置里model参数不能识别。

openai.BadRequestError: Error code: 400 - {'object': 'error', 'message': 'Only chatglm2-6b allowed now, your model ChatGLM-6B', 'code': 40301}

这时,需要修改模型的名称,需要与FastChat的model_worker启动时的模型名称相同才会识别。
微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第10张图片

问题6:

当给大模型的任务需要执行python代码时,程序会使用到docker,没有安装docker的话就会报错。

AttributeError: module 'docker' has no attribute 'from_env'

这时,执行pip3 install docker。

问题7:

问题6安装docker后,执行仍报错。

docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

在代码的code_execution_config中添加"use_docker":”python:3”,use_docker的值可以填docker镜像,填写镜像即是在镜像中执行模型自动生成的python代码,也可以什么都不填,什么都不填即是在本机运行,我这里因为是测试,就没有填实际的镜像。

具体解释可参考:
https://github.com/microsoft/autogen/issues/142
https://microsoft.github.io/autogen/docs/reference/agentchat/conversable_agent/#init
微软Autogen框架加载本地ChatGLM2-6B&ChatGLM3-6B_第11张图片

5、参考

https://microsoft.github.io/autogen/docs/Getting-Started
https://github.com/microsoft/autogen
https://zhuanlan.zhihu.com/p/663031095
https://microsoft.github.io/autogen/docs/Examples

你可能感兴趣的:(大模型,chatglm,autogen)