安装
按照以下网址的指示申请api_key然后替换程序中的your_api_key
https://help.aliyun.com/zh/dashscope/developer-reference/activate-dashscope-and-create-an-api-key?spm=a2c4g.11186623.0.0.459ac11aaiehfX
程序
from http import HTTPStatus
from dashscope import Generation
import dashscope
dashscope.api_key = your_api_key
def chatglmv2_call(prompt):
rsp = Generation.call(model='chatglm-6b-v2',
prompt=prompt,
history=[])
print(rsp)
if rsp.status_code == HTTPStatus.OK:
glm_result = rsp.output['text']
else:
print('Failed, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))
return glm_result
if __name__ == '__main__':
prompt_TP = "Who are you?"
glm_TP = chatglmv2_call(prompt=prompt_TP)
print(glm_TP)
程序运行结果
{"status_code": 200, "request_id": "6faa94b1-6fb1-95cb-8fb5-4a7331bee0b2", "code": "", "message": "", "output": {"text": "I am an AI assistant named ChatGLM2-6B, which is developed based on the language model jointly trained by Tsinghua University KEG Lab and Zhipu AI Company in 2023. My job is to provide appropriate answers and support to users' questions and requests.", "finish_reason": "stop", "choices": null}, "usage": {"input_tokens": 20, "output_tokens": 63, "total_tokens": 83}}
I am an AI assistant named ChatGLM2-6B, which is developed based on the language model jointly trained by Tsinghua University KEG Lab and Zhipu AI Company in 2023. My job is to provide appropriate answers and support to users' questions and requests.