智谱ai python sdk 调用学习

第一步注册 智谱

智谱AI大模型

第二步获取 api key

智谱ai  python sdk 调用学习

第三步安装 sdk

pip install zhipuai

第四步 例子

import zhipuai
from zhipuai.model_api import ChatGLM6bParams

zhipuai.api_key = "******"


def invoke_example():
    params = ChatGLM6bParams(
        prompt=[{"role": "user", "content": "人工智能"}], top_p=0.7, temperature=0.9
    )
    response = zhipuai.model_api.invoke(**params.asdict())
    print(response)


def async_invoke_example():
    params = ChatGLM6bParams(
        prompt=[{"role": "user", "content": "人工智能"}], top_p=0.7, temperature=0.9
    )
    response = zhipuai.model_api.async_invoke(**params.asdict())
    print(response)


"""
    说明:
    add: 事件流开启
    error: 平台服务或者模型异常,响应的异常事件
    interrupted: 中断事件,例如:触发敏感词
    finish: 数据接收完毕,关闭事件流
"""


def sse_invoke_example():
    params = ChatGLM6bParams(
        prompt=[{"role": "user", "content": "写一个黄山7天旅游计划"}], top_p=0.7, temperature=0.9
    )
    response = zhipuai.model_api.sse_invoke(**params.asdict())
    for event in response.events():
        if event.event == "add":
            print(event.data)
        elif event.event == "error" or event.event == "interrupted":
            print(event.data)
        elif event.event == "finish":
            print(event.data)
            print(event.meta)
        else:
            print(event.data)


def query_async_invoke_result_example():
    response = zhipuai.model_api.query_async_invoke_result("your task_id")
    print(response)


if __name__ == '__main__':
    sse_invoke_example()
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 1

ai写的用py调用ai的代码,可以,我仿佛看到了ai自己鞭策自己~

7个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
78
粉丝
8
喜欢
43
收藏
49
排名:83
访问:10.5 万
私信
所有博文
社区赞助商