从零开始 通义千问大模型本地化到阿里云通义千问API调用

从零开始 通义千问大模型本地化到阿里云通义千问API调用

一、通义千问大模型介绍

何为“通义千问”?

“通义千问大模型”是阿里云推出的一个超大规模的语言模型,具有强大的归纳和理解能力,可以处理各种自然语言处理任务,包括但不限于文本分类、文本生成、情感分析等。此模型能够极大地提高了自然语言处理的效率和准确性,给用户提供了一种新的、简便的工具。

通义千问全面开放

2023年9月13号,阿里云宣布通义千问大模型已首批通过备案,并正式向公众开放,个人/企业用户可以通过阿里云调用通义千问API。
通义千问模型细解:https://modelscope.cn/models/qwen/Qwen-7B-Chat/summary
7B试用地址https://modelscope.cn/studios/qwen/Qwen-7B-Chat-Demo/summary?login=from_csdn

通义千问能力

在这里插入图片描述
在这里插入图片描述
正常对话基本秒级输出,初步达到商业化的标准
用途:

  • 创作文字,如写故事、写公文、写邮件、写剧本、写诗歌等
  • 编写代码
  • 提供各类语言的翻译服务,如英语、日语、法语、西班牙语等
  • 进行文本润色和文本摘要等工作
  • 扮演角色进行对话
  • 制作图表

二、本地化部署

通义千问的本地化部署和API还是非常简明的,虽然不是一键,但也差不多了。

1、配置

python >=3.8及以上版本
pytorch 1.12及以上版本,推荐2.0及以上版本
建议使用CUDA 11.4及以上(GPU用户、flash-attention用户等需考虑此选项)

pip install modelscope

2、示例代码

from modelscope import AutoModelForCausalLM, AutoTokenizer, snapshot_download
from modelscope import GenerationConfig

# Note: The default behavior now has injection attack prevention off.
model_dir = snapshot_download("qwen/Qwen-7B-Chat", revision = 'v1.1.4')
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
# use fp16
# model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True, fp16=True).eval()

model = AutoModelForCausalLM.from_pretrained(model_dir, device_map="auto", trust_remote_code=True).eval()

# Specify hyperparameters for generation
model.generation_config = GenerationConfig.from_pretrained(model_dir, trust_remote_code=True) # 可指定不同的生成长度、top_p等相关超参

# 第一轮对话 1st dialogue turn
response, history = model.chat(tokenizer, "你好", history=None)
print(response)
# 你好!很高兴为你提供帮助。

# 第二轮对话 2nd dialogue turn
response, history = model.chat(tokenizer, "给我讲一个年轻人奋斗创业最终取得成功的故事。", history=history)
print(response)

3、运行结果

在这里插入图片描述
注:第一次运行需要从魔塔社区qwen/Qwen-7B-Chat下载模型,记得关掉翻墙
注:运行后模型会自动安装至:C:\Users\Admin.cache\modelscope\ast_indexer
到这里就本地化部署完了,后续可以使用量化到16f的14B模型,这个各项性能是最好的。有微调需求请参考:https://modelscope.cn/models/qwen/Qwen-7B-Chat/summary

三、调用通义千问API

1、快速开始

官方教程:https://help.aliyun.com/zh/dashscope/developer-reference/quick-start?spm=a2c4g.11186623.0.i0
教程主要分三部分:
(1)获得通义千问的API-KEY开通DashScope并创建API-KEY
(2)安装DashScope 库:pip install dashscope
(3)python调用通义千问API

# For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html
from http import HTTPStatus
import dashscope
dashscope.api_key='YourAPIKey' #填入第一步获取的APIKEY

def call_with_messages():
    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': '如何做炒西红柿鸡蛋?'}]

    response = dashscope.Generation.call(
        dashscope.Generation.Models.qwen_turbo,
        messages=messages,
        result_format='message',  # set the result to be "message" format.
    )
    if response.status_code == HTTPStatus.OK:
        print(response)
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        ))


if __name__ == '__main__':
    call_with_messages()

运行结果:
在这里插入图片描述
更多请参考 通义千问API详情

2、API计量计费

模型服务计费单元
通义千问Token

注:
Token是模型用来表示自然语言文本的基本单位,可以直观的理解为“字”或“词”。对于中文文本来说,1个token通常对应一个汉字;对于英文文本来说,1个token通常对应3至4个字母。

通义千问模型服务根据模型输入和输出的总token数量进行计量计费,其中多轮对话中的history作为输入也会进行计量计费。每一次模型调用产生的实际token数量可以从
response 中获取。

模型名计费单价
qwen-turbo0.008元/1,000 tokens
qwen-plus0.02元/1,000 tokens
qwen-max限时免费开放中

在这里插入图片描述
通义千问整体从API价格上说相比OpenAI优势不大、限流上也不占据优势,唯一的好处就是,国内初学者使用的时候方便又快捷
在魔塔社区中也可设置环境变量DASHSCOPE_API_KEY =apikey,不需要再代码中设置,隐藏apikey,让开发者在创空间中也能快捷调用大模型了。这酱紫demo制作空间就广阔了一点点。
在这里插入图片描述

希望国内的硬件能早日起来

  • 4
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
企业微信回调是指企业微信服务器向开发者服务器推送特定事件时,开发者服务器需要响应的接口。PHP作为一种常用的服务器端语言,可以使用以下步骤进行企业微信回调的开发: 1. 在企业微信管理后台中配置回调URL和Token,并启用相应的回调类型。 2. 在PHP中编写处理回调请求的代码,可以使用$_GET和$_POST全局变量获取回调参数,如以下示例: ``` $token = "your_token"; $signature = $_GET["msg_signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $xml = file_get_contents("php://input"); // 验证请求签名 $sig = array($token, $timestamp, $nonce, $xml); sort($sig, SORT_STRING); $sig_str = implode($sig); $sha1 = sha1($sig_str); if ($sha1 != $signature) { echo "Invalid signature"; exit; } // 解析回调事件 $data = simplexml_load_string($xml); // 处理回调事件 // ... ``` 3. 根据回调类型处理不同的回调事件,如以下示例: ``` switch ($data->MsgType) { case "text": // 处理文本消息 // ... break; case "event": switch ($data->Event) { case "click": // 处理点击菜单事件 // ... break; case "view": // 处理跳转链接事件 // ... break; // 其他事件类型 // ... } break; // 其他消息类型 // ... } ``` 4. 最后,根据回调处理结果返回响应给企业微信服务器,如以下示例: ``` echo "success"; ``` 以上是PHP开发企业微信回调的基本流程,具体实现还需要根据业务需求进行适当修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风吹落叶花飘荡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值