-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.ts
256 lines (224 loc) · 8.16 KB
/
index.ts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { createServer, type IncomingMessage } from "node:http";
import { verifyAndParseRequest, transformPayloadForOpenAICompatibility, getFunctionCalls, createDoneEvent, createReferencesEvent } from "@copilot-extensions/preview-sdk";
import OpenAI from "openai";
import { describeModel } from "./functions/describe-model.js";
import { executeModel } from "./functions/execute-model.js";
import { listModels } from "./functions/list-models.js";
import { RunnerResponse } from "./functions.js";
import { recommendModel } from "./functions/recommend-model.js";
import { ModelsAPI } from "./models-api.js";
const server = createServer(async (request, response) => {
if (request.method === "GET") {
response.statusCode = 200;
response.end(`OK`);
return;
}
const body = await getBody(request);
let verifyAndParseRequestResult: Awaited<ReturnType<typeof verifyAndParseRequest>>;
const apiKey = request.headers["x-github-token"] as string;
try {
const signature = request.headers["x-github-public-key-signature"] as string;
const keyID = request.headers["x-github-public-key-identifier"] as string;
verifyAndParseRequestResult = await verifyAndParseRequest(body, signature, keyID, {
token: apiKey,
});
} catch (err) {
console.error(err);
response.statusCode = 401
response.end("Unauthorized");
return
}
const { isValidRequest, payload } = verifyAndParseRequestResult
if (!isValidRequest) {
console.log("Signature verification failed");
response.statusCode = 401
response.end("Unauthorized");
}
console.log("Signature verified");
const compatibilityPayload = transformPayloadForOpenAICompatibility(payload);
// Use the GitHub API token sent in the request
if (!apiKey) {
response.statusCode = 400
response.end()
return;
}
// List of functions that are available to be called
const modelsAPI = new ModelsAPI(apiKey);
const functions = [listModels, describeModel, executeModel, recommendModel];
// Use the Copilot API to determine which function to execute
const capiClient = new OpenAI({
baseURL: "https://api.githubcopilot.com",
apiKey,
});
// Prepend a system message that includes the list of models, so that
// tool calls can better select the right model to use.
const models = await modelsAPI.listModels();
const toolCallMessages = [
{
role: "system" as const,
content: [
"You are an extension of GitHub Copilot, built to interact with GitHub Models.",
"GitHub Models is a language model playground, where you can experiment with different models and see how they respond to your prompts.",
"Here is a list of some of the models available to the user:",
"<-- LIST OF MODELS -->",
JSON.stringify(
[...models.map((model) => ({
friendly_name: model.displayName,
name: model.name,
publisher: model.publisher,
registry: model.registryName,
description: model.summary,
})),
{
friendly_name: "OpenAI o1-mini",
name: "o1-mini",
publisher: "openai",
model_registry: "azure-openai",
description: "Smaller, faster, and 80% cheaper than o1-preview, performs well at code generation and small context operations."
},
{
friendly_name: "OpenAI o1-preview",
name: "o1-preview",
publisher: "openai",
model_registry: "azure-openai",
description: "Focused on advanced reasoning and solving complex problems, including math and science tasks. Ideal for applications that require deep contextual understanding and agentic workflows."
},
]
),
"<-- END OF LIST OF MODELS -->",
].join("\n"),
},
...compatibilityPayload.messages,
];
console.time("tool-call");
const toolCaller = await capiClient.chat.completions.create({
stream: false,
model: "gpt-4o",
messages: toolCallMessages,
tools: functions.map((f) => f.tool),
});
console.timeEnd("tool-call");
const [functionToCall] = getFunctionCalls(
// @ts-expect-error - type error due to Copilot/OpenAI SDKs interop, I'll look into it ~@gr2m
toolCaller.choices[0]
)
if (
!functionToCall
) {
console.log("No tool call found");
// No tool to call, so just call the model with the original messages
const stream = await capiClient.chat.completions.create({
stream: true,
model: "gpt-4o",
messages: compatibilityPayload.messages,
})
for await (const chunk of stream) {
const chunkStr = "data: " + JSON.stringify(chunk) + "\n\n";
response.write(chunkStr);
}
response.end(createDoneEvent());
return;
}
const args = JSON.parse(functionToCall.function.arguments);
console.time("function-exec");
let functionCallRes: RunnerResponse;
try {
console.log("Executing function", functionToCall.function.name);
const funcClass = functions.find(
(f) => f.definition.name === functionToCall.function.name
);
if (!funcClass) {
throw new Error("Unknown function");
}
console.log("\t with args", args);
const func = new funcClass(modelsAPI);
functionCallRes = await func.execute(
compatibilityPayload.messages,
args
);
} catch (err) {
console.error(err);
response.statusCode = 500
response.end();
return;
}
console.timeEnd("function-exec");
// Now that we have a tool result, let's use it to call the model.
try {
let stream: AsyncIterable<any>;
if (functionToCall.function.name === executeModel.definition.name) {
// First, let's write a reference with the model we're executing.
// Fetch the model data from the index (already in-memory) so we have all the information we need
// to build out the reference URLs
const modelData = await modelsAPI.getModelFromIndex(functionCallRes.model);
const sseData = {
type: "models.reference",
id: `models.reference.${modelData.name}`,
data: {
model: functionCallRes.model
},
is_implicit: false,
metadata: {
display_name: `Model: ${modelData.name}`,
display_icon: "icon",
display_url: `https://github.com/marketplace/models/${modelData.registryName}/${modelData.name}`,
}
};
const event = createReferencesEvent([sseData]);
response.write(event);
if (["o1-mini", "o1-preview"].includes(args.model)) {
// for non-streaming models, we need to still stream the response back, so we build the stream ourselves
stream = (async function*() {
const result = await modelsAPI.inference.chat.completions.create({
model: functionCallRes.model,
messages: functionCallRes.messages
});
yield result;
})();
} else {
stream = await modelsAPI.inference.chat.completions.create({
model: functionCallRes.model,
messages: functionCallRes.messages,
stream: true
});
}
} else {
stream = await capiClient.chat.completions.create({
stream: true,
model: "gpt-4o",
messages: functionCallRes.messages,
});
}
console.time("streaming");
for await (const chunk of stream) {
const chunkStr = "data: " + JSON.stringify(chunk) + "\n\n";
response.write(chunkStr);
}
response.end(createDoneEvent());
console.timeEnd("streaming");
} catch (err) {
console.error(err);
if ((err as any).response && (err as any).response.status === 400) {
console.error('Error 400:', (err as any).response.data);
}
response.statusCode = 500
response.write("data: Something went wrong\n\n")
response.end()
}
});
const port = process.env.PORT || "3000"
server.listen(port);
console.log(`Server running at http://localhost:${port}`);
function getBody(request: IncomingMessage): Promise<string> {
return new Promise((resolve) => {
const bodyParts: Buffer[] = [];
let body;
request
.on("data", (chunk: Buffer) => {
bodyParts.push(chunk);
})
.on("end", () => {
resolve(Buffer.concat(bodyParts).toString());
});
});
}