-
Notifications
You must be signed in to change notification settings - Fork 5
/
chat.py
146 lines (115 loc) · 4.17 KB
/
chat.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import requests
import json
import time
MODEL = 'gpt-4-1106-preview'
token = None
messages = []
def setup():
resp = requests.post('https://github.com/login/device/code', headers={
'accept': 'application/json',
'editor-version': 'Neovim/0.6.1',
'editor-plugin-version': 'copilot.vim/1.16.0',
'content-type': 'application/json',
'user-agent': 'GithubCopilot/1.155.0',
'accept-encoding': 'gzip,deflate,br'
}, data='{"client_id":"Iv1.b507a08c87ecfe98","scope":"read:user"}')
# Parse the response json, isolating the device_code, user_code, and verification_uri
resp_json = resp.json()
device_code = resp_json.get('device_code')
user_code = resp_json.get('user_code')
verification_uri = resp_json.get('verification_uri')
# Print the user code and verification uri
print(f'Please visit {verification_uri} and enter code {user_code} to authenticate.')
while True:
time.sleep(5)
resp = requests.post('https://github.com/login/oauth/access_token', headers={
'accept': 'application/json',
'editor-version': 'Neovim/0.6.1',
'editor-plugin-version': 'copilot.vim/1.16.0',
'content-type': 'application/json',
'user-agent': 'GithubCopilot/1.155.0',
'accept-encoding': 'gzip,deflate,br'
}, data=f'{{"client_id":"Iv1.b507a08c87ecfe98","device_code":"{device_code}","grant_type":"urn:ietf:params:oauth:grant-type:device_code"}}')
# Parse the response json, isolating the access_token
resp_json = resp.json()
access_token = resp_json.get('access_token')
if access_token:
break
# Save the access token to a file
with open('.copilot_token', 'w') as f:
f.write(access_token)
print('Authentication success!')
def get_token():
global token
# Check if the .copilot_token file exists
while True:
try:
with open('.copilot_token', 'r') as f:
access_token = f.read()
break
except FileNotFoundError:
setup()
# Get a session with the access token
resp = requests.get('https://api.github.com/copilot_internal/v2/token', headers={
'authorization': f'token {access_token}',
'editor-version': 'Neovim/0.6.1',
'editor-plugin-version': 'copilot.vim/1.16.0',
'user-agent': 'GithubCopilot/1.155.0'
})
# Parse the response json, isolating the token
resp_json = resp.json()
token = resp_json.get('token')
def chat(message):
global token, messages
# If the token is None, get a new one
if token is None:
get_token()
messages.append({
"content": str(message),
"role": "user"
})
try:
resp = requests.post('https://api.githubcopilot.com/chat/completions', headers={
'authorization': f'Bearer {token}',
'Editor-Version': 'vscode/1.80.1',
}, json={
'intent': False,
'model': MODEL,
'temperature': 0,
'top_p': 1,
'n': 1,
'stream': True,
'messages': messages
})
except requests.exceptions.ConnectionError:
return ''
result = ''
# Parse the response text, splitting it by newlines
resp_text = resp.text.split('\n')
for line in resp_text:
# If the line contains a completion, print it
if line.startswith('data: {'):
# Parse the completion from the line as json
json_completion = json.loads(line[6:])
try:
completion = json_completion.get('choices')[0].get('delta').get('content')
if completion:
result += completion
else:
result += '\n'
except:
pass
messages.append({
"content": result,
"role": "assistant"
})
if result == '':
print(resp.status_code)
print(resp.text)
return result
def main():
get_token()
while True:
print(chat(input('>>> ')))
if __name__ == '__main__':
main()