通义千问2-0.5BF-functioncall微调

我要开发同款
匿名用户2024年07月31日
55阅读
开发技术qwen2、Pytorch
所属分类ai
开源地址https://modelscope.cn/models/longdream/qwen2-0.5B-functioncall
授权协议Apache License 2.0

作品详情

模型介绍

模型概述 我们基于 qwen2 0.5B 模型进行了微调,使其具备了 function call 功能。下面示例使用了llamafactory 运行模型来提供api进行测试

您可以通过如下git clone命令,或者ModelScope SDK来下载模型

您可以

SDK下载

#安装ModelScope
pip  install  modelscope
#SDK模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('longdream/qwen2-0.5B-functioncall')

Git下载

#Git模型下载
git clone https://www.modelscope.cn/longdream/qwen2-0.5B-functioncall.git
#fucntion call 进行测试
import os
import json
from openai import OpenAI
from typing import Sequence
import requests

os.environ["OPENAI_BASE_URL"] = "http://192.168.1.103:38000/v1"
os.environ["OPENAI_API_KEY"] = "0"


def get_rubbish_category(keyword):
    url = f"https://api.timelessq.com/garbage?keyword={keyword}"
    response = requests.request("GET", url)
    output_str_list = []
    for item in response.json()['data']:
        output_str_list.append(f"{item['name']}: {item['categroy']}")
    return '\n'.join(output_str_list)


def get_song_information(keyword):
    url = f"https://api.timelessq.com/music/tencent/search?keyword={keyword}"
    response = requests.request("GET", url)
    song_infor = response.json()['data']['list'][0]
    singer = '' if not song_infor['singer'] else song_infor['singer'][0]['name']
    return f"歌曲: {keyword}\n歌手: {singer}\n时长: {song_infor['interval']}秒\n专辑名称: {song_infor['albumname']}"


def get_cartoon_information(title):
    url = f"https://api.timelessq.com/bangumi?title={title}"
    response = requests.request("GET", url)
    data = response.json()['data'][0]
    return f"标题: {data['title']}\n类型:{data['type']}\n语言:{data['lang']}\n出品方:{data['officialSite']}\n上映时间:{data['begin']}\n完结事件:{data['end']}"


tool_map = {"get_rubbish_category": get_rubbish_category,
            "get_song_information": get_song_information,
            "get_cartoon_information": get_cartoon_information}


if __name__ == "__main__":
    client = OpenAI()
    tools = [
        {
            "type": "function",
            "function": {
                            "name": "get_cartoon_information",
                            "description": "根据用户提供的动漫标题,查询该动漫的相关信息。",
                            "parameters": {
                                "type": "object",
                                "properties": {
                                    "title": {
                                        "type": "string",
                                        "description": "动漫",
                                    },
                                },
                                "required": ["title"],
                            }
                        }
        },
        {
            "type": "function",
            "function": {
                            "name": "get_song_information",
                            "description": "根据用户提供的歌曲名称,查询歌曲相关信息,包括歌手、时长、专辑名称等。",
                            "parameters": {
                                "type": "object",
                                "properties": {
                                    "keyword": {
                                        "type": "string",
                                        "description": "歌曲名称",
                                    },
                                },
                                "required": ["keyword"],
                            }
                        }
        }
    ]

    messages = []
    messages.append({"role": "system", "content": "你是一个有用的小助手,请调用下面的工具来回答用户的问题,参考工具输出进行回答。"})
    # messages.append({"role": "user", "content": "鸡蛋壳属于哪种类型的垃圾?"})
    # messages.append({"role": "user", "content": "爱在西元前是谁唱的,来自哪张专辑?"})
    messages.append({"role": "user", "content": "歌曲《七里香》是哪个国家的,歌手是谁?"})
    result = client.chat.completions.create(messages=messages, model="Qwen2-0.5B-agent", tools=tools)
    print(result)
    tool_call = result.choices[0].message.tool_calls[0].function
    print(tool_call)
    name, arguments = tool_call.name, json.loads(tool_call.arguments)
    messages.append({"role": "function", "content": json.dumps({"name": name, "argument": arguments}, ensure_ascii=False)})
    tool_result = tool_map[name](**arguments)
    messages.append({"role": "tool", "content": "工具输出结果为: " + tool_result})
    for msg in messages:
        print('--->', msg)
    result = client.chat.completions.create(messages=messages, model="Qwen2-0.5B-agent")
    print("Answer: ", result.choices[0].message.content)
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论