forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
111 lines (98 loc) · 2.9 KB
/
utilities.py
File metadata and controls
111 lines (98 loc) · 2.9 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
102
103
104
105
106
107
108
109
110
111
import json
import os
import random
import CopilotChat.prompts as prompts
import CopilotChat.typings as typings
def random_hex(length: int = 65):
return "".join([random.choice("0123456789abcdef") for _ in range(length)])
def generate_request(
chat_history: list[typings.Message],
code_excerpt: str,
language: str = "",
system_prompt=prompts.COPILOT_INSTRUCTIONS,
model="gpt-4",
):
messages = [
{
"content": system_prompt,
"role": "system",
}
]
for message in chat_history:
messages.append(
{
"content": message.content,
"role": message.role,
}
)
if code_excerpt != "":
messages.insert(
-1,
{
"content": f"\nActive selection:\n```{language}\n{code_excerpt}\n```",
"role": "system",
},
)
return {
"intent": True,
"model": model,
"n": 1,
"stream": True,
"temperature": 0.1,
"top_p": 1,
"messages": messages,
}
def generate_embedding_request(inputs: list[typings.FileExtract]):
return {
"input": [
f"File: `{i.filepath}`\n```{i.filepath.split('.')[-1]}\n{i.code}```"
for i in inputs
],
"model": "copilot-text-embedding-ada-002",
}
def cache_token(user: str, token: str):
# ~/.config/github-copilot/hosts.json
home = os.path.expanduser("~")
config_dir = os.path.join(home, ".config", "github-copilot")
if not os.path.exists(config_dir):
os.makedirs(config_dir)
with open(os.path.join(config_dir, "hosts.json"), "w") as f:
f.write(
json.dumps(
{
"github.com": {
"user": user,
"oauth_token": token,
}
}
)
)
def get_cached_token():
home = os.path.expanduser("~")
config_dir = os.path.join(home, ".config", "github-copilot")
hosts_file = os.path.join(config_dir, "hosts.json")
if not os.path.exists(hosts_file):
return None
with open(hosts_file, "r") as f:
hosts = json.loads(f.read())
if "github.com" in hosts:
return hosts["github.com"]["oauth_token"]
else:
return None
if __name__ == "__main__":
print(
json.dumps(
generate_request(
[
typings.Message("Hello, Copilot!", "user"),
typings.Message("Hello, World!", "system"),
typings.Message("How are you?", "user"),
typings.Message("I am fine, thanks.", "system"),
typings.Message("What does this code do?", "user"),
],
"print('Hello, World!')",
"python",
),
indent=2,
)
)