-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (83 loc) · 3.62 KB
/
Copy pathmain.py
File metadata and controls
101 lines (83 loc) · 3.62 KB
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
import requests
from flask import Flask, request, Response, jsonify
import uuid
import datetime
import hashlib
app = Flask(__name__)
machine_id = hashlib.sha256(str(uuid.uuid4()).encode()).hexdigest()
def forward_request(GHO_TOKEN: str, stream: bool, json_data):
headers = {
'Host': 'api.github.com',
'authorization': f'token {GHO_TOKEN}',
"Editor-Version": "vscode/1.84.2",
"Editor-Plugin-Version": "copilot/1.138.0",
"User-Agent": "GithubCopilot/1.138.0",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "close"
}
response = requests.get(
'https://api.github.com/copilot_internal/v2/token', headers=headers)
print("Auth:",response.text)
if response.status_code == 200 and response.json():
access_token = response.json()['token']
acc_headers = {
'Authorization': f'Bearer {access_token}',
'X-Request-Id': str(uuid.uuid4()),
'Vscode-Sessionid': str(uuid.uuid4()) + str(int(datetime.datetime.utcnow().timestamp() * 1000)),
'vscode-machineid': machine_id,
'Editor-Version': 'vscode/1.84.2',
'Editor-Plugin-Version': 'copilot-chat/0.10.2',
'Openai-Organization': 'github-copilot',
'Openai-Intent': 'conversation-panel',
'Content-Type': 'application/json',
'User-Agent': 'GitHubCopilotChat/0.10.2',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
}
resp = requests.post('https://api.githubcopilot.com/chat/completions', headers=acc_headers, json=json_data, stream=stream)
return resp.iter_content(chunk_size=8192) if stream else resp.json()
else:
# print(response.text)
return response
@app.route('/v1/chat/completions', methods=['POST'])
def proxy():
# 从请求中获取json数据
json_data = request.get_json()
if json_data is None:
return "Request body is missing or not in JSON format", 400
# 获取Authorization头部信息
GHO_TOKEN = request.headers.get('Authorization')
GHO_TOKEN = GHO_TOKEN.split(' ')[1]
print("Secret:", GHO_TOKEN)
print("Message:", json_data)
if GHO_TOKEN is None:
return "Authorization header is missing", 401
# Check if stream option is set in the request data
stream = json_data.get('stream', False)
# 转发请求并获取响应
resp = forward_request(GHO_TOKEN, stream, json_data)
# 处理流式输出
return Response(resp, mimetype='application/json') if stream else resp
@app.route('/v1/models', methods=['GET'])
def models():
data = {
"object": "list",
"data": [
{"id": "gpt-4-0314", "object": "model", "created": 1687882410,
"owned_by": "openai", "root": "gpt-4-0314", "parent": None},
{"id": "gpt-4-0613", "object": "model", "created": 1686588896,
"owned_by": "openai", "root": "gpt-4-0613", "parent": None},
{"id": "gpt-4", "object": "model", "created": 1687882411,
"owned_by": "openai", "root": "gpt-4", "parent": None},
{"id": "gpt-3.5-turbo", "object": "model", "created": 1677610602,
"owned_by": "openai", "root": "gpt-3.5-turbo", "parent": None},
{"id": "gpt-3.5-turbo-0301", "object": "model", "created": 1677649963,
"owned_by": "openai", "root": "gpt-3.5-turbo-0301", "parent": None},
]
}
return jsonify(data)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=False)
# GHO_TOKEN = "gho_xx"
# set_access_token(get_token(GHO_TOKEN)['token'])