tastypear commited on
Commit
83e5367
·
verified ·
1 Parent(s): a243baa

Update serve.py

Browse files
Files changed (1) hide show
  1. serve.py +132 -117
serve.py CHANGED
@@ -1,117 +1,132 @@
1
- # coding:utf-8
2
- import argparse
3
- import requests
4
- import json
5
- import os
6
- from cerebras import CerebrasUnofficial
7
- from flask import Flask, request, Response, stream_with_context, jsonify
8
- import sys
9
-
10
- # -- Start of Config --
11
-
12
- # Replace with your cerebras.ai session token found in the `authjs.session-token` cookie.
13
- # Or you can `set AUTHJS_SESSION_TOKEN=authjs.session-token`
14
- # This token is valid for one month.
15
- authjs_session_token = '12345678-abcd-abcd-abcd-12345678abcd'
16
-
17
- # Replace with any string you wish like `my-api-key`.
18
- # Or you can `set SERVER_API_KEY=my-api-key`
19
- # You should set it to update the session token in the future.
20
- server_api_key = 'my-api-key'
21
-
22
- # -- End of Config --
23
-
24
- sys.tracebacklimit = 0
25
-
26
- authjs_session_token = os.environ.get('AUTHJS_SESSION_TOKEN', authjs_session_token)
27
- server_api_key = os.environ.get('SERVER_API_KEY', server_api_key)
28
- print(f'Using the cookie: authjs.session-token={authjs_session_token}')
29
- print(f'Your api key: {server_api_key}')
30
-
31
- cerebras_ai = CerebrasUnofficial(authjs_session_token)
32
-
33
- app = Flask(__name__)
34
- app.json.sort_keys = False
35
- parser = argparse.ArgumentParser(description='Cerebras.AI API.')
36
- parser.add_argument('--host', type=str, help='Set the ip address.(default: 0.0.0.0)', default='0.0.0.0')
37
- parser.add_argument('--port', type=int, help='Set the port.(default: 7860)', default=7860)
38
- args = parser.parse_args()
39
-
40
- class Provider:
41
- key = ''
42
- max_tokens = None
43
- api_url = ''
44
-
45
- def __init__(self, request_key, model):
46
- self.request_key = request_key
47
- self.model = model
48
- self.init_request_info()
49
-
50
- def init_request_info(self):
51
- if self.request_key == server_api_key:
52
- self.api_url = cerebras_ai.api_url
53
- self.key = cerebras_ai.get_api_key()
54
-
55
- @app.route('/api/renew', methods=['GET', 'POST'])
56
- @app.route('/renew', methods=['GET', 'POST'])
57
- def renew_token():
58
- if server_api_key == request.args.get('key', ''):
59
- request_token = request.args.get('token', '')
60
- global cerebras_ai
61
- cerebras_ai = CerebrasUnofficial(request_token)
62
- return f'new authjs.session_token: {request_token}'
63
- else:
64
- raise Exception('invalid api key')
65
-
66
- @app.route('/api/v1/models', methods=['GET', 'POST'])
67
- @app.route('/v1/models', methods=['GET', 'POST'])
68
- def model_list():
69
- model_list = {
70
- 'object': 'list',
71
- 'data': [{
72
- 'id': 'llama3.1-8b',
73
- 'object': 'model',
74
- 'created': 1721692800,
75
- 'owned_by': 'Meta'
76
- }, {
77
- 'id': 'llama-3.3-70b',
78
- 'object': 'model',
79
- 'created': 1733443200,
80
- 'owned_by': 'Meta'
81
- }, {
82
- 'id': 'deepseek-r1-distill-llama-70b',
83
- 'object': 'model',
84
- 'created': 1733443200,
85
- 'owned_by': 'deepseek'
86
- }]
87
- }
88
- return jsonify(model_list)
89
-
90
-
91
- @app.route('/api/v1/chat/completions', methods=['POST'])
92
- @app.route('/v1/chat/completions', methods=['POST'])
93
- def proxy():
94
- request_key = request.headers['Authorization'].split(' ')[1]
95
- if server_api_key != request_key:
96
- raise Exception('invalid api key')
97
-
98
- headers = dict(request.headers)
99
- headers.pop('Host', None)
100
- headers.pop('Content-Length', None)
101
-
102
- headers['X-Use-Cache'] = 'false'
103
- model = request.get_json()['model']
104
- provider = Provider(request_key, model)
105
- headers['Authorization'] = f'Bearer {provider.key}'
106
- chat_api = f'{provider.api_url}/v1/chat/completions'
107
-
108
- def generate():
109
- with requests.post(chat_api, json=request.json, headers=headers, stream=True) as resp:
110
- for chunk in resp.iter_content(chunk_size=1024):
111
- if chunk:
112
- chunk_str = chunk.decode('utf-8')
113
- yield chunk_str
114
- return Response(stream_with_context(generate()), content_type='text/event-stream')
115
-
116
- if __name__ == '__main__':
117
- app.run(host=args.host, port=args.port, debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding:utf-8
2
+ import argparse
3
+ import requests
4
+ import json
5
+ import os
6
+ from cerebras import CerebrasUnofficial
7
+ from flask import Flask, request, Response, stream_with_context, jsonify
8
+ import sys
9
+
10
+ # -- Start of Config --
11
+
12
+ # Replace with your cerebras.ai session token found in the `authjs.session-token` cookie.
13
+ # Or you can `set AUTHJS_SESSION_TOKEN=authjs.session-token`
14
+ # This token is valid for one month.
15
+ authjs_session_token = '12345678-abcd-abcd-abcd-12345678abcd'
16
+
17
+ # Replace with any string you wish like `my-api-key`.
18
+ # Or you can `set SERVER_API_KEY=my-api-key`
19
+ # You should set it to update the session token in the future.
20
+ server_api_key = 'my-api-key'
21
+
22
+ # -- End of Config --
23
+
24
+ sys.tracebacklimit = 0
25
+
26
+ authjs_session_token = os.environ.get('AUTHJS_SESSION_TOKEN', authjs_session_token)
27
+ server_api_key = os.environ.get('SERVER_API_KEY', server_api_key)
28
+ print(f'Using the cookie: authjs.session-token={authjs_session_token}')
29
+ print(f'Your api key: {server_api_key}')
30
+
31
+ cerebras_ai = CerebrasUnofficial(authjs_session_token)
32
+
33
+ app = Flask(__name__)
34
+ app.json.sort_keys = False
35
+ parser = argparse.ArgumentParser(description='Cerebras.AI API')
36
+ parser.add_argument('--host', type=str, help='Set the ip address.(default: 0.0.0.0)', default='0.0.0.0')
37
+ parser.add_argument('--port', type=int, help='Set the port.(default: 7860)', default=7860)
38
+ args = parser.parse_args()
39
+
40
+ class Provider:
41
+ key = ''
42
+ max_tokens = None
43
+ api_url = ''
44
+
45
+ def __init__(self, request_key, model):
46
+ self.request_key = request_key
47
+ self.model = model
48
+ self.init_request_info()
49
+
50
+ def init_request_info(self):
51
+ if self.request_key == server_api_key:
52
+ self.api_url = cerebras_ai.api_url
53
+ self.key = cerebras_ai.get_api_key()
54
+
55
+ @app.route('/api', methods=['GET', 'POST'])
56
+ @app.route('/', methods=['GET', 'POST'])
57
+ def index():
58
+ return f'''
59
+ renew/change token by visiting:<br>
60
+ {request.host_url}renew?key={{your server api key}}&token={{your Cerebras authjs_session_token}}<br>
61
+ <br>
62
+ Your interface:<br>
63
+ {request.host_url}v1/chat/completions OR<br>
64
+ {request.host_url}api/v1/chat/completions<br>
65
+ <br>
66
+ For more infomation by visiting:<br>
67
+ https://github.com/tastypear/CerebrasUnofficial
68
+ '''
69
+
70
+ @app.route('/api/renew', methods=['GET', 'POST'])
71
+ @app.route('/renew', methods=['GET', 'POST'])
72
+ def renew_token():
73
+ if server_api_key == request.args.get('key', ''):
74
+ request_token = request.args.get('token', '')
75
+ global cerebras_ai
76
+ cerebras_ai = CerebrasUnofficial(request_token)
77
+ return f'new authjs.session_token: {request_token}'
78
+ else:
79
+ raise Exception('invalid api key')
80
+
81
+ @app.route('/api/v1/models', methods=['GET', 'POST'])
82
+ @app.route('/v1/models', methods=['GET', 'POST'])
83
+ def model_list():
84
+ model_list = {
85
+ 'object': 'list',
86
+ 'data': [{
87
+ 'id': 'llama3.1-8b',
88
+ 'object': 'model',
89
+ 'created': 1721692800,
90
+ 'owned_by': 'Meta'
91
+ }, {
92
+ 'id': 'llama-3.3-70b',
93
+ 'object': 'model',
94
+ 'created': 1733443200,
95
+ 'owned_by': 'Meta'
96
+ }, {
97
+ 'id': 'deepseek-r1-distill-llama-70b',
98
+ 'object': 'model',
99
+ 'created': 1733443200,
100
+ 'owned_by': 'deepseek'
101
+ }]
102
+ }
103
+ return jsonify(model_list)
104
+
105
+
106
+ @app.route('/api/v1/chat/completions', methods=['POST'])
107
+ @app.route('/v1/chat/completions', methods=['POST'])
108
+ def proxy():
109
+ request_key = request.headers['Authorization'].split(' ')[1]
110
+ if server_api_key != request_key:
111
+ raise Exception('invalid api key')
112
+
113
+ headers = dict(request.headers)
114
+ headers.pop('Host', None)
115
+ headers.pop('Content-Length', None)
116
+
117
+ headers['X-Use-Cache'] = 'false'
118
+ model = request.get_json()['model']
119
+ provider = Provider(request_key, model)
120
+ headers['Authorization'] = f'Bearer {provider.key}'
121
+ chat_api = f'{provider.api_url}/v1/chat/completions'
122
+
123
+ def generate():
124
+ with requests.post(chat_api, json=request.json, headers=headers, stream=True) as resp:
125
+ for chunk in resp.iter_content(chunk_size=1024):
126
+ if chunk:
127
+ chunk_str = chunk.decode('utf-8')
128
+ yield chunk_str
129
+ return Response(stream_with_context(generate()), content_type='text/event-stream')
130
+
131
+ if __name__ == '__main__':
132
+ app.run(host=args.host, port=args.port, debug=True)