【大模型API调用初尝试一】智谱AI && 通义千问

大模型API调用初尝试一

调用大模型API能干什么

大模型的参数非常庞大,功能非常强大,但是训练成本高昂,因此个人或者小企业自己去训练一个大模型是不可能的。我们可以通过直接调用大模型的API,将大模型集成到自己的应用中。 大模型的API就是一个接口,类似MaaS,用户通过调用API访问大模型,获得大模型针对用户prompt(问题)的输出,一般输出是json格式的,然后我们利用这个输出进行后续的操作。

但是大模型是一个已经训练好的模型,类似一个封装好的盒子,其能够运用的知识是有限的,比如chatgpt的知识截至2021年9月,让它提供实时的天气预报是不可行的,因此现在的大模型API中还增加了外部API/函数的调用功能,大模型能够根据用户的prompt确定要去调用的外部API/function-->调用外部API/function并获得结果-->大模型整合结果再输出,如下图所示:

我们可以参考某一个大模型对应的官方文档,学习如何调用其API。

智谱AI大模型API调用的过程

智谱AI是清华大学计算机系技术成果转化而来的公司,公司产品包括:中英双语千亿级超大规模预训练模型GLM-130B,并基于此推出对话模型ChatGLM;高效率代码模型CodeGeeX;多模态理解模型CogVLM和文生图模型CogView等。

获取API_KEY

调用API首先需要获取API,每个用户需要自己注册申请对应的API。进入智谱AI官网,注册后申请API,复制API_KEY以便后续调用:

现在打开API调用参考文档:通用大模型图像大模型,直接复制其代码,看看输出是什么样的!

GLM_4同步调用

同步调用就是创建一个调用任务后,一直运行这个任务直到收到大模型的响应输出;对应的是异步调用,创建调用任务后获得这个任务的request-id,之后可以去干其他事情,后续通过这个任务ID查看模型的输出。

python 复制代码
from zhipuai import ZhipuAI  # 先安装ZhipuAI的包 pip install ZhipuAI
client = ZhipuAI(api_key="xxxx") # 填写您自己的APIKey
response = client.chat.completions.create(
    model="glm-4",  # 填写需要调用的模型名称
    messages=[
    # messages是json格式的数据,大模型逐条响应
        {"role": "user", "content": "作为一名营销专家,请为我的产品创作一个吸引人的slogan"},
        {"role": "assistant", "content": "当然,为了创作一个吸引人的slogan,请告诉我一些关于您产品的信息"},
        {"role": "user", "content": "智谱AI开放平台"},
        {"role": "assistant", "content": "智启未来,谱绘无限一智谱AI,让创新触手可及!"},
        {"role": "user", "content": "创造一个更精准、吸引人的slogan"}
    ],
)
# 直接输出response,查看响应的具体内容
print(response)

输出的完整json数据:

如果只关注最终输出的message的content,可以只取response.choices[0].message.content

上面例子传入大模型的message列表里面,有user的信息,也有assistant的信息,大模型实际响应的只有user对应的content,但是assistant的内容可以为大模型提供一些上下文或者提示。

message只传入用户的输入,并多次问答的例子如下:

python 复制代码
from zhipuai import ZhipuAI
 
client = ZhipuAI(api_key="xxxxxx") # 填写您自己的APIKey
# 循环提问/对话
while True:
	# 接收用户输入作为问题
    prompt = input("\nuser:")
    response = client.chat.completions.create(
        model="glm-4",  # 填写需要调用的模型名称
        messages=[
            {"role": "user", "content": prompt}
        ],
    )
    answer = response.choices[0].message.content
    print("\nZhipuAI:",answer) # 只输出大模型响应的message.context

结果:

上述例子虽然user能够无限次的与大模型进行对话(交互),但由于message中只有用户单次的问题,没有保存上下文的信息,因此如果前后问题有衔接,这样的方式是不行的。应该使用多轮对话,将每轮对话的问答保存在message中,传给大模型为其提供对话的上下文。

GLM_4异步调用

参考官方文档,异步调用获取结果包括两步:①获取模型的响应ID,②根据ID查询响应结果;

python 复制代码
import time
from zhipuai import ZhipuAI
client = ZhipuAI(api_key="xxxxxx") # 请填写您自己的APIKey

response = client.chat.asyncCompletions.create(
    model="glm-4",  # 填写需要调用的模型名称
    messages=[
        {
            "role": "user",
            "content": "请你作为童话故事大王,写一篇短篇童话故事,故事的主题是要永远保持一颗善良的心,要能够激发儿童的学习兴趣和想象力,同时也能够帮助儿童更好地理解和接受故事中所蕴含的道理和价值观。"
        }
    ],
)
# 获取响应ID
task_id = response.id
task_status = ''
get_cnt = 0

while task_status != 'SUCCESS' and task_status != 'FAILED' and get_cnt <= 40:
    # 查询响应结果
    result_response = client.chat.asyncCompletions.retrieve_completion_result(id=task_id)
    print(result_response)
    task_status = result_response.task_status

    time.sleep(2)
    get_cnt += 1

运行输出如下:

可以看到在得到大模型的响应输出前,状态一直是processing;知道大模型响应。

文生图大模型API调用

Cogview是文生图的大模型,直接复制的官方文档的代码:

python 复制代码
from zhipuai import ZhipuAI
client = ZhipuAI(api_key="xxxxx") # 请填写您自己的APIKey

response = client.images.generations(
    model="cogview-3", #填写需要调用的模型名称
    prompt="一只可爱的小猫咪",
)
print(response.data[0].url)

结果(0.25一张照片呜呜呜呜呜呜呜)返回图片url:https://sfile.chatglm.cn/testpath/b158178b-e00c-5ea0-92bb-f2962922b877_0.png 👇:

阿里云通义千问API调用过程

通义千问是阿里云开发的基于QWen模型的聊天模型,QWen模型是开源的。调用API同样需要先获取API_KEY------首先注册阿里云的账号,然后进入灵积平台创建API-KEY:

之后查看通义千问的官方API调用文档👉快速入门,会话可以分为单轮会话和多轮会话:

单轮会话

可以通过message和prompt两种方式调用API:

python 复制代码
import random
from http import HTTPStatus # 通义千问API是通过HTTP调用的 
import dashscope

dashscope.api_key = 'xxxxx' # 设置自己申请的API_KEY; 这个方式设置有泄露api的可能

def call_with_messages():
    prompt = input("user:")
    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': prompt}]
    response = dashscope.Generation.call(
        dashscope.Generation.Models.qwen_turbo, # 选择模型
        messages=messages,
        # set the random seed, optional, default to 1234 if not set
        seed=random.randint(1, 10000),
        result_format='message',  # set the result to be "message" format.
    )
    # 之后HTTPStatus为OK时,才是调用成功
    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
        ))

# For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html

def call_with_prompt():
    prompt = input("user:")
    response = dashscope.Generation.call(
        model=dashscope.Generation.Models.qwen_turbo, # 选择模型
        prompt=prompt
    )
    # The response status_code is HTTPStatus.OK indicate success,
    # otherwise indicate request is failed, you can get error code
    # and message from code and message.
    if response.status_code == HTTPStatus.OK:
        print(response.output)  # The output text
        print(response.usage)  # The usage information
    else:
        print(response.code)  # The error code.
        print(response.message)  # The error message.

if __name__ == '__main__':

    call_with_messages()
    print("\n")
    call_with_prompt()

两种方式的单轮会话输出如下:

这里需要注意通过message和prompt调用,这两种方式对应的输出Json格式是不同的。

多轮会话

通过messages调用API,每轮会话用户的问题(the content of user)和大模型的输出(content of assistant)会存储在messages这个列表中,之后的输出会参考前面的内容,体现了大模型的记忆性!(可以在体验中心,选择多轮会话,注意观察代码中messages的变化):

观察对应的代码,每轮会话结束后,user的问题和模型的响应都会append在messages中:

可以用下面的代码调用API实现多轮会话:

python 复制代码
from http import HTTPStatus
import dashscope
from dashscope import Generation
from dashscope.api_entities.dashscope_response import Role

dashscope.api_key = 'xxxxx' # 设置API_KEY

def conversation_with_messages():
    messages = [{'role': Role.SYSTEM, 'content': 'You are a helpful assistant.'}  ]
    # 循环实现多轮会话
    while True:
        prompt = input("USER:")
        # 添加新一轮会话用户的问题
        messages.append({'role': Role.USER, 'content': prompt})
        response = Generation.call(
            Generation.Models.qwen_turbo, #选择响应的模型
            messages=messages,
            result_format='message',  # set the result to be "message" format.
        )
        if response.status_code == HTTPStatus.OK:
            print(response)
            # 把模型的输出添加到messages中
            messages.append({'role': response.output.choices[0]['message']['role'],
                             'content': response.output.choices[0]['message']['content']})
        else:
            print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
                response.request_id, response.status_code,
                response.code, response.message
            ))
            exit()

if __name__ == '__main__':
    conversation_with_messages()

结果如下:

相关推荐
DisonTangor2 分钟前
Windows 11将新增基于AI的搜索、生成式填充和其它AI功能
人工智能
soso19684 分钟前
【AI自然语言处理应用】通过API调用通义晓蜜CCAI-对话分析AIO应用
人工智能·自然语言·ccai
网安-搬运工7 分钟前
RAG再总结之如何使大模型更好使用外部数据:四个不同层级及查询-文档对齐策略
人工智能·自然语言处理·大模型·llm·大语言模型·ai大模型·rag
大模型八哥8 分钟前
大模型扫盲系列——大模型实用技术介绍(上)
人工智能·程序人生·ai·大模型·llm·llama·ai大模型
被制作时长两年半的个人练习生22 分钟前
【pytorch】权重为0的情况
人工智能·pytorch·深度学习
Elastic 中国社区官方博客37 分钟前
使用 Vertex AI Gemini 模型和 Elasticsearch Playground 快速创建 RAG 应用程序
大数据·人工智能·elasticsearch·搜索引擎·全文检索
说私域1 小时前
地理定位营销与开源AI智能名片O2O商城小程序的融合与发展
人工智能·小程序
Q_w77421 小时前
计算机视觉小目标检测模型
人工智能·目标检测·计算机视觉
创意锦囊2 小时前
ChatGPT推出Canvas功能
人工智能·chatgpt
知来者逆2 小时前
V3D——从单一图像生成 3D 物体
人工智能·计算机视觉·3d·图像生成