Spaces:
Runtime error
Runtime error
Commit ·
7532194
1
Parent(s): 306961b
Upload 16 files
Browse files
chat/openai/__pycache__/open_ai_utils.cpython-310.pyc
CHANGED
|
Binary files a/chat/openai/__pycache__/open_ai_utils.cpython-310.pyc and b/chat/openai/__pycache__/open_ai_utils.cpython-310.pyc differ
|
|
|
chat/openai/open_ai_utils.py
CHANGED
|
@@ -45,7 +45,6 @@ class OpenAiApi:
|
|
| 45 |
temperature=0,
|
| 46 |
)
|
| 47 |
|
| 48 |
-
print(response)
|
| 49 |
story = response['choices'][0]['message']['content']
|
| 50 |
t = time.time() - startTime
|
| 51 |
print("------------")
|
|
|
|
| 45 |
temperature=0,
|
| 46 |
)
|
| 47 |
|
|
|
|
| 48 |
story = response['choices'][0]['message']['content']
|
| 49 |
t = time.time() - startTime
|
| 50 |
print("------------")
|
chat/spark/SparkApi.py
CHANGED
|
@@ -10,9 +10,14 @@ from datetime import datetime
|
|
| 10 |
from time import mktime
|
| 11 |
from urllib.parse import urlencode
|
| 12 |
from wsgiref.handlers import format_date_time
|
| 13 |
-
|
| 14 |
import websocket # 使用websocket_client
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
class Ws_Param(object):
|
| 18 |
# 初始化
|
|
@@ -23,6 +28,7 @@ class Ws_Param(object):
|
|
| 23 |
self.host = urlparse(Spark_url).netloc
|
| 24 |
self.path = urlparse(Spark_url).path
|
| 25 |
self.Spark_url = Spark_url
|
|
|
|
| 26 |
|
| 27 |
# 生成url
|
| 28 |
def create_url(self):
|
|
@@ -57,80 +63,111 @@ class Ws_Param(object):
|
|
| 57 |
return url
|
| 58 |
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
# 收到websocket关闭的处理
|
| 66 |
-
def on_close(ws,one,two):
|
| 67 |
-
print(" ")
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
# 收到websocket连接建立的处理
|
| 71 |
-
def on_open(ws):
|
| 72 |
-
thread.start_new_thread(run, (ws,))
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
def run(ws, *args):
|
| 76 |
-
data = json.dumps(gen_params(appid=ws.appid, domain= ws.domain,question=ws.question))
|
| 77 |
-
ws.send(data)
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# print(message)
|
| 83 |
-
data = json.loads(message)
|
| 84 |
-
code = data['header']['code']
|
| 85 |
-
if code != 0:
|
| 86 |
-
print(f'请求错误: {code}, {data}')
|
| 87 |
-
ws.close()
|
| 88 |
-
else:
|
| 89 |
-
choices = data["payload"]["choices"]
|
| 90 |
-
status = choices["status"]
|
| 91 |
-
content = choices["text"][0]["content"]
|
| 92 |
-
print(content,end ="")
|
| 93 |
-
global answer
|
| 94 |
-
answer += content
|
| 95 |
-
# print(1)
|
| 96 |
-
if status == 2:
|
| 97 |
-
ws.close()
|
| 98 |
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
"
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
}
|
| 120 |
}
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
|
|
|
|
| 10 |
from time import mktime
|
| 11 |
from urllib.parse import urlencode
|
| 12 |
from wsgiref.handlers import format_date_time
|
| 13 |
+
import time
|
| 14 |
import websocket # 使用websocket_client
|
| 15 |
+
|
| 16 |
+
models = {
|
| 17 |
+
"Spark1":{"Spark_url":"ws://spark-api.xf-yun.com/v1.1/chat","domain":"general" },
|
| 18 |
+
"Spark2":{"Spark_url":"ws://spark-api.xf-yun.com/v2.1/chat","domain":"generalv2" },
|
| 19 |
+
"Spark3":{"Spark_url":"ws://spark-api.xf-yun.com/v3.1/chat","domain":"generalv3" }
|
| 20 |
+
}
|
| 21 |
|
| 22 |
class Ws_Param(object):
|
| 23 |
# 初始化
|
|
|
|
| 28 |
self.host = urlparse(Spark_url).netloc
|
| 29 |
self.path = urlparse(Spark_url).path
|
| 30 |
self.Spark_url = Spark_url
|
| 31 |
+
self.answer = ""
|
| 32 |
|
| 33 |
# 生成url
|
| 34 |
def create_url(self):
|
|
|
|
| 63 |
return url
|
| 64 |
|
| 65 |
|
| 66 |
+
class SparkApi(object):
|
| 67 |
+
# 收到websocket错误的处理
|
| 68 |
+
def __init__(self):
|
| 69 |
+
self.ws_close = True
|
| 70 |
|
| 71 |
+
def on_error(self,ws, error):
|
| 72 |
+
print("### error:", error)
|
| 73 |
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
# 收到websocket关闭的处理
|
| 76 |
+
def on_close(self,ws,one,two):
|
| 77 |
+
print(" ")
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
# 收到websocket连接建立的处理
|
| 81 |
+
def on_open(self,ws):
|
| 82 |
+
thread.start_new_thread(self.run, (ws,))
|
| 83 |
+
self.ws_close = False
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
def run(self,ws, *args):
|
| 87 |
+
data = json.dumps(self.gen_params(appid=ws.appid, domain= ws.domain,question=ws.question))
|
| 88 |
+
ws.send(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
+
# 收到websocket消息的处理
|
| 92 |
+
def on_message(self,ws, message):
|
| 93 |
+
# print(message)
|
| 94 |
+
data = json.loads(message)
|
| 95 |
+
code = data['header']['code']
|
| 96 |
+
if code != 0:
|
| 97 |
+
print(f'请求错误: {code}, {data}')
|
| 98 |
+
ws.close()
|
| 99 |
+
else:
|
| 100 |
+
choices = data["payload"]["choices"]
|
| 101 |
+
status = choices["status"]
|
| 102 |
+
content = choices["text"][0]["content"]
|
| 103 |
+
# print(content,end ="")
|
| 104 |
+
|
| 105 |
+
self.answer += content
|
| 106 |
+
# print(1)
|
| 107 |
+
if status == 2:
|
| 108 |
+
ws.close()
|
| 109 |
+
self.ws_close = True
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def gen_params(self,appid, domain,question):
|
| 113 |
+
"""
|
| 114 |
+
通过appid和用户的提问来生成请参数
|
| 115 |
+
"""
|
| 116 |
+
data = {
|
| 117 |
+
"header": {
|
| 118 |
+
"app_id": appid,
|
| 119 |
+
"uid": "1234"
|
| 120 |
+
},
|
| 121 |
+
"parameter": {
|
| 122 |
+
"chat": {
|
| 123 |
+
"domain": domain,
|
| 124 |
+
"temperature": 0.5,
|
| 125 |
+
"max_tokens": 2048
|
| 126 |
+
}
|
| 127 |
+
},
|
| 128 |
+
"payload": {
|
| 129 |
+
"message": {
|
| 130 |
+
"text": question
|
| 131 |
+
}
|
| 132 |
}
|
| 133 |
}
|
| 134 |
+
return data
|
| 135 |
+
|
| 136 |
+
def getText(self,role, content,text=[]):
|
| 137 |
+
|
| 138 |
+
jsoncon = {}
|
| 139 |
+
jsoncon["role"] = role
|
| 140 |
+
jsoncon["content"] = content
|
| 141 |
+
text.append(jsoncon)
|
| 142 |
+
return text
|
| 143 |
+
|
| 144 |
+
def main(self,appid="761ef9bd",
|
| 145 |
+
api_key="YmMzYThlNmU1MTA0OTAyNjQ4ZjU0NmM5",
|
| 146 |
+
api_secret="fce82992c44dd00fcb968a658f39187e",
|
| 147 |
+
model="Spark1",
|
| 148 |
+
question=None):
|
| 149 |
+
# print("星火:")
|
| 150 |
+
m = models[model]
|
| 151 |
+
wsParam = Ws_Param(appid, api_key, api_secret, m.get("Spark_url"))
|
| 152 |
+
websocket.enableTrace(False)
|
| 153 |
+
wsUrl = wsParam.create_url()
|
| 154 |
+
startTime = time.time()
|
| 155 |
+
ws = websocket.WebSocketApp(wsUrl, on_message=self.on_message, on_error=self.on_error, on_close=self.on_close, on_open=self.on_open)
|
| 156 |
+
ws.appid = appid
|
| 157 |
+
ws.question = question
|
| 158 |
+
ws.domain = m.get("domain")
|
| 159 |
+
self.answer = ""
|
| 160 |
+
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
|
| 161 |
+
while True:
|
| 162 |
+
if self.ws_close:
|
| 163 |
+
break
|
| 164 |
+
time.sleep(1)
|
| 165 |
+
t = time.time() - startTime
|
| 166 |
+
print("------------")
|
| 167 |
+
print("%s 生成完成\n使用时间:%s" % (model, t))
|
| 168 |
+
print("------------")
|
| 169 |
+
return {"content": self.answer, "model_name": model, "executeTime": t}
|
| 170 |
+
|
| 171 |
+
|
| 172 |
|
| 173 |
|
chat/spark/__pycache__/SparkApi.cpython-310.pyc
CHANGED
|
Binary files a/chat/spark/__pycache__/SparkApi.cpython-310.pyc and b/chat/spark/__pycache__/SparkApi.cpython-310.pyc differ
|
|
|
chat/spark/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (148 Bytes). View file
|
|
|
chat/spark/test.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import SparkApi
|
|
|
|
| 2 |
#以下密钥信息从控制台获取
|
| 3 |
appid = "761ef9bd" #填写控制台中获取的 APPID 信息
|
| 4 |
api_secret = "YmMzYThlNmU1MTA0OTAyNjQ4ZjU0NmM5" #填写控制台中获取的 APISecret 信息
|
|
@@ -41,13 +42,16 @@ def checklen(text):
|
|
| 41 |
|
| 42 |
|
| 43 |
if __name__ == '__main__':
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
|
|
|
|
| 1 |
import SparkApi
|
| 2 |
+
|
| 3 |
#以下密钥信息从控制台获取
|
| 4 |
appid = "761ef9bd" #填写控制台中获取的 APPID 信息
|
| 5 |
api_secret = "YmMzYThlNmU1MTA0OTAyNjQ4ZjU0NmM5" #填写控制台中获取的 APISecret 信息
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
if __name__ == '__main__':
|
| 45 |
+
api = SparkApi.SparkApi()
|
| 46 |
+
print(api.main(appid, api_key, api_secret, "Spark2", getText("user", "今天上海的天气是什么?")))
|
| 47 |
+
|
| 48 |
+
# text.clear()
|
| 49 |
+
# while(1):
|
| 50 |
+
# Input = input("\n" +"我:")
|
| 51 |
+
# question = checklen(getText("user",Input))
|
| 52 |
+
# SparkApi.answer =""
|
| 53 |
+
# print("星火:",end = "")
|
| 54 |
+
# SparkApi.main(appid,api_key,api_secret,Spark_url,domain,question)
|
| 55 |
+
# getText("assistant",SparkApi.answer)
|
| 56 |
+
# # print(str(text))
|
| 57 |
|