-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
95 lines (81 loc) · 3.07 KB
/
index.ts
File metadata and controls
95 lines (81 loc) · 3.07 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
import { z } from "zod";
import fs from 'fs';
import { parse } from 'csv-parse';
const Message = z.object({
role: z.string(),
name: z.string().optional(),
content: z.string(),
});
const Input = z.object({
messages: z.array(Message),
});
interface CapiJson {
choices: Array<{
content_filter_results: Record<string, unknown>;
finish_reason: string;
index: number;
message: {
function_call: {
arguments: string;
name: string;
};
role: string;
};
}>;
}
Bun.serve({
port: Bun.env.PORT ?? "3000",
async fetch(request) {
console.debug("received request", request.url);
// Do nothing with the OAuth callback, for now. Just return a 200.
if (new URL(request.url).pathname === "/oauth/callback") {
console.debug("received oauth callback");
return Response.json({ ok: true }, { status: 200 });
}
// Parsing with Zod strips unknown Copilot-specific fields in the request
// body, which cause OpenAI errors if they're included.
const json = await request.json();
const input = Input.safeParse(json);
if (!input.success) {
return Response.json({ error: "Bad request" }, { status: 400 });
}
const messages = input.data.messages;
console.debug("received input", JSON.stringify(json, null, 4));
messages.splice(-1, 0, {
role: "system",
content: "Analyze the user prompt and provide feedback as a prompt engineering coach. Answer these questions: Is the intent clear? Are the sentences concise? Does it include examples or details on the expected outcome? If there are areas for improvement, please rewrite the prompt according to prompt engineering best practices.",
},{
role: "system",
content: "Responses should follow the following structure: A. Analysis B. Enhanced prompt. If the prompt provided was good, skip section B. If the prompt provided was bad, provide a new prompt following prompt engineering best practices.",
});
console.debug("received messages", JSON.stringify(messages, null, 4));
const capiUserResponse = await fetch(
"https://api.githubcopilot.com/chat/completions",
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${request.headers.get('X-GitHub-Token')}`,
},
body: JSON.stringify({
stream: true,
messages: messages,
model: "gpt-3.5-turbo",
}),
}
);
console.debug("received capi response", JSON.stringify(capiUserResponse, null, 4));
return new Response(capiUserResponse.body, { status: 200 });
},
});
// Function to read and parse CSV file
async function readCSVFile(filePath: string): Promise<any[]> {
return new Promise((resolve, reject) => {
const results: any[] = [];
fs.createReadStream(filePath)
.pipe(parse({ columns: true }))
.on('data', (data) => results.push(data))
.on('end', () => resolve(results))
.on('error', (error) => reject(error));
});
}