lancerrrrrrr commited on
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
- answer = ""
 
 
 
 
 
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
- # 收到websocket错误的处理
61
- def on_error(ws, error):
62
- print("### error:", error)
 
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
- # 收到websocket消息的处理
81
- def on_message(ws, message):
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
- def gen_params(appid, domain,question):
101
- """
102
- 通过appid和用户的提问来生成请参数
103
- """
104
- data = {
105
- "header": {
106
- "app_id": appid,
107
- "uid": "1234"
108
- },
109
- "parameter": {
110
- "chat": {
111
- "domain": domain,
112
- "temperature": 0.5,
113
- "max_tokens": 2048
114
- }
115
- },
116
- "payload": {
117
- "message": {
118
- "text": question
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  }
120
  }
121
- }
122
- return data
123
-
124
-
125
- def main(appid, api_key, api_secret, Spark_url,domain, question):
126
- # print("星火:")
127
- wsParam = Ws_Param(appid, api_key, api_secret, Spark_url)
128
- websocket.enableTrace(False)
129
- wsUrl = wsParam.create_url()
130
- ws = websocket.WebSocketApp(wsUrl, on_message=on_message, on_error=on_error, on_close=on_close, on_open=on_open)
131
- ws.appid = appid
132
- ws.question = question
133
- ws.domain = domain
134
- ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- text.clear()
45
- while(1):
46
- Input = input("\n" +"我:")
47
- question = checklen(getText("user",Input))
48
- SparkApi.answer =""
49
- print("星火:",end = "")
50
- SparkApi.main(appid,api_key,api_secret,Spark_url,domain,question)
51
- getText("assistant",SparkApi.answer)
52
- # print(str(text))
 
 
 
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