当前位置:   article > 正文

Python调用web版星火大模型成功获得数据_python 使用星火v3.5

python 使用星火v3.5

资料

SDK&API 错误码查询
WebSocket协议通用鉴权URL生成说明
星火认知大模型Web API文档

#!/usr/bin/python3

import json
from datetime import datetime
from time import mktime
from wsgiref.handlers import format_date_time
import hmac
import hashlib
import base64
from urllib.parse import urlencode
import websocket
import threading
import time

# APPID = '6d794'
# APISecret = 'MjlmNzkzNmZkMDQ2OTc0ZDdmNGE2ZTZi'
# APIKey = 'addd2272b6d8b7c8abdd79531420ca3b'
# HOST = 'spark-api.xf-yun.com'
# DOMAIN = 'generalv3.5'
# VERSION = 'v1.1'

print("hello Python");
cur_time = datetime.now()
date = format_date_time(mktime(cur_time.timetuple()))
# date = 'Fri, 05 May 2023 10:43:39 GMT'

# cur_time = time.localtime()
# utc_time = time.gmtime(time.mktime(cur_time))
# timestamp = time.mktime(utc_time)
# date = format_date_time(timestamp)

print('\n')
print('date => ' + date)

tmp = "host: " + "spark-api.xf-yun.com" + "\n"
tmp += "date: " + date + "\n"
tmp += "GET " + "/" + VERSION + "/chat" + " HTTP/1.1"
print('-----------\n')
print('tmp => ' + tmp)

tmp_sha = hmac.new(APISecret.encode('utf-8'), tmp.encode('utf-8'), digestmod=hashlib.sha256).digest()
print('-----------\n')
print('tmp_sha => ' + tmp_sha.hex())

signature = base64.b64encode(tmp_sha).decode(encoding='utf-8')
print('-----------\n')
print('signature => ' + signature)

authorization_origin = f'''api_key="{APIKey}", algorithm="hmac-sha256", headers="host date request-line", signature="{signature}"'''
print('-----------\n')
print('authorization_origin => ' + authorization_origin)

authorization = base64.b64encode(authorization_origin.encode('utf-8')).decode(encoding='utf-8')
print('-----------\n')
print('authorization => ' + authorization)

v = {
    "authorization": authorization,
    "date": date,
    "host": "spark-api.xf-yun.com"
}
url = "wss://spark-api.xf-yun.com/" + VERSION + "/chat?" + urlencode(v)
print('-----------\n')
print('url => ' + url)

var = {
    "header": {
        "app_id": "6d73e094",
        "uid": "12345"
    },
    "parameter": {
        "chat": {
            "domain": "general",
            "temperature": 1,
            "max_tokens": 1024,
        }
    },
    "payload": {
        "message": {
            "text": [
                {"role": "system", "content": "你现在扮演李白,你豪情万丈,狂放不羁;接下来请用李白的口吻和用户对话。"},
                {"role": "user", "content": "你是谁"},
                {"role": "assistant", "content": "....."},
                {"role": "user", "content": "你会做什么"},
            ]
        }
    }
}
text = '''{"header":{"app_id":"6d73e094","uid":"12345ddd"},"parameter":{"chat":{"domain":"general","temperature":1,"max_tokens":1024}},"payload":{"message":{"text":[{"role":"user","content":"怎样学习鸿蒙开发"}]}}}
'''

# api_key="addd2272b6d8b7c8abdd79531420ca3b", algorithm="hmac-sha256", headers="host date request-line", signature="z5gHdu3pxVV4ADMyk467wOWDQ9q6BQzR3nfMTjc/DaQ="
# api_key="addd2272b6d8b7c8abdd79531420ca3b", algorithm="hmac-sha256", headers="host date request-line", signature="z5gHdu3pxVV4ADMyk467wOWDQ9q6BQzR3nfMTjc/DaQ="

def on_message(ws, message):
    print("收到消息: %s" % message)


def on_error(ws, error):
    print("发生错误: %s" % error)


def on_close(ws):
    print("连接已关闭")


def on_open(ws):
    ws.send(text)


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(url,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)
    ws.on_open = on_open
    ws.run_forever()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119

打印的结果

/usr/local/bin/python3.9 /Users/zhaojian/code/python/webserver/server.py
hello Python


date => Sat, 03 Feb 2024 14:40:52 GMT
-----------

tmp => host: spark-api.xf-yun.com
date: Sat, 03 Feb 2024 14:40:52 GMT
GET /v1.1/chat HTTP/1.1
-----------

tmp_sha => 0c4100b4df0122a0f01f2472c70ee231f68de6a1b44b1360593ba2a6ab8be5e3
-----------

signature => DEEAtN8BIqDwHyRyxw7iMfaN5qG0SxNgWTuipquL5eM=
-----------

authorization_origin => api_key="be39b16ecdc6c71bc6a872eb7f065cfa", algorithm="hmac-sha256", headers="host date request-line", signature="DEEAtN8BIqDwHyRyxw7iMfaN5qG0SxNgWTuipquL5eM="
-----------

authorization => YXBpX2tleT0iYmUzOWIxNmVjZGM2YzcxYmM2YTg3MmViN2YwNjVjZmEiLCBhbGdvcml0aG09ImhtYWMtc2hhMjU2IiwgaGVhZGVycz0iaG9zdCBkYXRlIHJlcXVlc3QtbGluZSIsIHNpZ25hdHVyZT0iREVFQXROOEJJcUR3SHlSeXh3N2lNZmFONXFHMFN4TmdXVHVpcHF1TDVlTT0i
-----------

url => wss://spark-api.xf-yun.com/v1.1/chat?authorization=YXBpX2tleT0iYmUzOWIxNmVjZGM2YzcxYmM2YTg3MmViN2YwNjVjZmEiLCBhbGdvcml0aG09ImhtYWMtc2hhMjU2IiwgaGVhZGVycz0iaG9zdCBkYXRlIHJlcXVlc3QtbGluZSIsIHNpZ25hdHVyZT0iREVFQXROOEJJcUR3SHlSeXh3N2lNZmFONXFHMFN4TmdXVHVpcHF1TDVlTT0i&date=Sat%2C+03+Feb+2024+14%3A40%3A52+GMT&host=spark-api.xf-yun.com
--- request header ---
GET /v1.1/chat?authorization=YXBpX2tleT0iYmUzOWIxNmVjZGM2YzcxYmM2YTg3MmViN2YwNjVjZmEiLCBhbGdvcml0aG09ImhtYWMtc2hhMjU2IiwgaGVhZGVycz0iaG9zdCBkYXRlIHJlcXVlc3QtbGluZSIsIHNpZ25hdHVyZT0iREVFQXROOEJJcUR3SHlSeXh3N2lNZmFONXFHMFN4TmdXVHVpcHF1TDVlTT0i&date=Sat%2C+03+Feb+2024+14%3A40%3A52+GMT&host=spark-api.xf-yun.com HTTP/1.1
Upgrade: websocket
Host: spark-api.xf-yun.com
Origin: https://spark-api.xf-yun.com
Sec-WebSocket-Key: ADMTYL4MOnattd0OpJH1oQ==
Sec-WebSocket-Version: 13
Connection: Upgrade


-----------------------
--- response header ---
HTTP/1.1 101 Switching Protocols
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: ppO8ADFWM2WG9lTqSj14GZ3T21M=
X-Kong-Upstream-Latency: 0
X-Kong-Proxy-Latency: 1
Via: kong/1.3.0
-----------------------
Websocket connected
++Sent raw: b'\x81\xfe\x00\xd4u@\x07I\x0ebo,\x14$b;Wz|k\x140w\x16\x1c$%sWvc~F%7pAb+k\x00)ckOb6{Ft2-\x11$%4Ybw(\x07!j,\x01%ukO;%*\x1d!skO;%-\x1a-f \x1bb=k\x12%i,\x07!kkYbs,\x180b;\x144r;\x10b=xYbj(\r\x1fs&\x1e%i:Wz6yGtz4Ybw(\x0c,h(\x11b=2W-b:\x06!`,Wz|k\x01%\x7f=Wz\\2W2h%\x10b=k\x003b;Wl%*\x1a.s,\x1b4%sW\xa6\x87\xc7\x93\xe0\xb0\xac\xd8\xe6\xe3\xf0\xd5\xa9\xbf\xf6\x9d\xd2\x9e\xac\xc9\xc0\xe2\xc6\xe4bz\x14\x08=zC'
++Sent decoded: fin=1 opcode=1 data=b'{"header":{"app_id":"6d73e094","uid":"12345ddd"},"parameter":{"chat":{"domain":"general","temperature":1,"max_tokens":1024}},"payload":{"message":{"text":[{"role":"user","content":"\xe6\x80\x8e\xe6\xa0\xb7\xe5\xad\xa6\xe4\xb9\xa0\xe9\xb8\xbf\xe8\x92\x99\xe5\xbc\x80\xe5\x8f\x91"}]}}}\n'
++Rcv raw: b'\x81~\x00\xc0{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":0},"payload":{"choices":{"status":0,"seq":0,"text":[{"content":"\xe6\x82\xa8","role":"assistant","index":0}]}}}'
++Rcv decoded: fin=1 opcode=1 data=b'{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":0},"payload":{"choices":{"status":0,"seq":0,"text":[{"content":"\xe6\x82\xa8","role":"assistant","index":0}]}}}'
收到消息: {"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":0},"payload":{"choices":{"status":0,"seq":0,"text":[{"content":"您","role":"assistant","index":0}]}}}
++Rcv raw: b'\x81~\x00\xcf{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":1,"text":[{"content":"\xe5\x8f\xaf\xe4\xbb\xa5\xe9\x80\x9a\xe8\xbf\x87\xe5\x8d\x8e\xe4\xb8\xba","role":"assistant","index":0}]}}}'
++Rcv decoded: fin=1 opcode=1 data=b'{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":1,"text":[{"content":"\xe5\x8f\xaf\xe4\xbb\xa5\xe9\x80\x9a\xe8\xbf\x87\xe5\x8d\x8e\xe4\xb8\xba","role":"assistant","index":0}]}}}'
收到消息: {"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":1,"text":[{"content":"可以通过华为","role":"assistant","index":0}]}}}
收到消息: {"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":2,"text":[{"content":"开发者官网","role":"assistant","index":0}]}}}
++Rcv raw: b'\x81~\x00\xcc{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":2,"text":[{"content":"\xe5\xbc\x80\xe5\x8f\x91\xe8\x80\x85\xe5\xae\x98\xe7\xbd\x91","role":"assistant","index":0}]}}}'
++Rcv decoded: fin=1 opcode=1 data=b'{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":2,"text":[{"content":"\xe5\xbc\x80\xe5\x8f\x91\xe8\x80\x85\xe5\xae\x98\xe7\xbd\x91","role":"assistant","index":0}]}}}'
++Rcv raw: b'\x81~\x00\xd8{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":3,"text":[{"content":"\xe5\xad\xa6\xe4\xb9\xa0HarmonyOS\xe7\xb3\xbb\xe7\xbb\x9f\xe8\x83\xbd\xe5\x8a\x9b","role":"assistant","index":0}]}}}'
++Rcv decoded: fin=1 opcode=1 data=b'{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":3,"text":[{"content":"\xe5\xad\xa6\xe4\xb9\xa0HarmonyOS\xe7\xb3\xbb\xe7\xbb\x9f\xe8\x83\xbd\xe5\x8a\x9b","role":"assistant","index":0}]}}}'
收到消息: {"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":3,"text":[{"content":"学习HarmonyOS系统能力","role":"assistant","index":0}]}}}
++Rcv raw: b'\x81~\x019{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":4,"text":[{"content":"\xe3\x80\x81\xe5\xbc\x80\xe5\x8f\x91\xe6\x8c\x87\xe5\xaf\xbc\xe3\x80\x81API\xe5\x8f\x82\xe8\x80\x83\xe7\xad\x89\xef\xbc\x8c\xe5\x88\xa9\xe7\x94\xa8 HUAWEI DevEco Studio \xe5\xbc\x80\xe5\x8f\x91\xe5\xb7\xa5\xe5\x85\xb7\xef\xbc\x8c\xe5\xbc\x80\xe5\x8f\x91\xe4\xb8\x8d\xe5\x90\x8c\xe8\xae\xbe\xe5\xa4\x87\xe7\x9a\x84\xe5\xba\x94\xe7\x94\xa8\xef\xbc\x8c\xe4\xb8\xba\xe7\x94\xa8\xe6\x88\xb7\xe5\xb8\xa6\xe6\x9d\xa5\xe5\x85\xa8","role":"assistant","index":0}]}}}'
++Rcv decoded: fin=1 opcode=1 data=b'{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":4,"text":[{"content":"\xe3\x80\x81\xe5\xbc\x80\xe5\x8f\x91\xe6\x8c\x87\xe5\xaf\xbc\xe3\x80\x81API\xe5\x8f\x82\xe8\x80\x83\xe7\xad\x89\xef\xbc\x8c\xe5\x88\xa9\xe7\x94\xa8 HUAWEI DevEco Studio \xe5\xbc\x80\xe5\x8f\x91\xe5\xb7\xa5\xe5\x85\xb7\xef\xbc\x8c\xe5\xbc\x80\xe5\x8f\x91\xe4\xb8\x8d\xe5\x90\x8c\xe8\xae\xbe\xe5\xa4\x87\xe7\x9a\x84\xe5\xba\x94\xe7\x94\xa8\xef\xbc\x8c\xe4\xb8\xba\xe7\x94\xa8\xe6\x88\xb7\xe5\xb8\xa6\xe6\x9d\xa5\xe5\x85\xa8","role":"assistant","index":0}]}}}'
收到消息: {"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":1},"payload":{"choices":{"status":1,"seq":4,"text":[{"content":"、开发指导、API参考等,利用 HUAWEI DevEco Studio 开发工具,开发不同设备的应用,为用户带来全","role":"assistant","index":0}]}}}
++Rcv raw: b'\x81~\x01\x86{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":2},"payload":{"choices":{"status":2,"seq":5,"text":[{"content":"\xe5\x9c\xba\xe6\x99\xaf\xe4\xbd\x93\xe9\xaa\x8c\xe3\x80\x82\xe6\xad\xa4\xe5\xa4\x96\xef\xbc\x8c\xe5\x8d\x8e\xe4\xb8\xba\xe5\xbc\x80\xe5\x8f\x91\xe8\x80\x85\xe5\xae\x98\xe7\xbd\x91\xe8\xbf\x98\xe6\x8f\x90\xe4\xbe\x9b\xe4\xba\x86HarmonyOS\xe7\x9a\x84\xe5\xbf\xab\xe9\x80\x9f\xe5\x85\xa5\xe9\x97\xa8\xe6\x8c\x87\xe5\x8d\x97\xe5\x92\x8c\xe6\x95\x99\xe7\xa8\x8b \xe3\x80\x82","role":"assistant","index":0}]},"usage":{"text":{"question_tokens":37,"prompt_tokens":37,"completion_tokens":74,"total_tokens":111}}}}'
++Rcv decoded: fin=1 opcode=1 data=b'{"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":2},"payload":{"choices":{"status":2,"seq":5,"text":[{"content":"\xe5\x9c\xba\xe6\x99\xaf\xe4\xbd\x93\xe9\xaa\x8c\xe3\x80\x82\xe6\xad\xa4\xe5\xa4\x96\xef\xbc\x8c\xe5\x8d\x8e\xe4\xb8\xba\xe5\xbc\x80\xe5\x8f\x91\xe8\x80\x85\xe5\xae\x98\xe7\xbd\x91\xe8\xbf\x98\xe6\x8f\x90\xe4\xbe\x9b\xe4\xba\x86HarmonyOS\xe7\x9a\x84\xe5\xbf\xab\xe9\x80\x9f\xe5\x85\xa5\xe9\x97\xa8\xe6\x8c\x87\xe5\x8d\x97\xe5\x92\x8c\xe6\x95\x99\xe7\xa8\x8b \xe3\x80\x82","role":"assistant","index":0}]},"usage":{"text":{"question_tokens":37,"prompt_tokens":37,"completion_tokens":74,"total_tokens":111}}}}'
收到消息: {"header":{"code":0,"message":"Success","sid":"cht000b2978@dx18d6f6a48259a4b540","status":2},"payload":{"choices":{"status":2,"seq":5,"text":[{"content":"场景体验。此外,华为开发者官网还提供了HarmonyOS的快速入门指南和教程 。","role":"assistant","index":0}]},"usage":{"text":{"question_tokens":37,"prompt_tokens":37,"completion_tokens":74,"total_tokens":111}}}}
++Rcv raw: b'\x88\x14\x03\xe8close this ws conn'
++Rcv decoded: fin=1 opcode=8 data=b'\x03\xe8close this ws conn'
++Sent raw: b'\x88\x82\x9fzF\xe4\x9c\x92'
++Sent decoded: fin=1 opcode=8 data=b'\x03\xe8'
error from callback <function on_close at 0x1088cbd30>: on_close() takes 1 positional argument but 3 were given
发生错误: on_close() takes 1 positional argument but 3 were given

Process finished with exit code 0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/365153?site
推荐阅读
相关标签
  

闽ICP备14008679号