用python写AI

fanghuangxu7月前 ⋅ 1348 阅读

1.在↓注册一个账号

 https://open.bigmodel.cn/ 

2.获取APIkey

3.输入以下代码:

import zhipuai

import threading
import time
def main():
    zhipuai.api_key = "你的api key"

    try:
        response = zhipuai.model_api.sse_invoke(
            model="chatglm_6b",
            prompt=[{"role": "user", "content": input('你:')}],
            temperature=0.9,
            top_p=0.7,
            incremental=True
        )
    except Exception as e:
        print(f"发生错误: {e}")
        return

    print('AI:')
    data = ''
    for event in response.events():
        if event.event == "add":
            data += event.data
            print(event.data, end='')
            time.sleep(0.1)  # 避免输出过快导致混乱
    print('')

if __name__ == "__main__":
    main()


全部评论: 0

    相关推荐