-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathprompt.js
133 lines (113 loc) · 3.18 KB
/
prompt.js
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
// @ts-check
const DEFAULT_ENDPOINT = "https://api.githubcopilot.com/chat/completions";
const DEFAULT_MODEL = "gpt-4o";
/** @type {import('..').PromptInterface} */
export function parsePromptArguments(userPrompt, promptOptions) {
const { request: requestOptions, ...options } =
typeof userPrompt === "string" ? promptOptions : userPrompt;
const promptFetch = requestOptions?.fetch || fetch;
const model = options.model || DEFAULT_MODEL;
const endpoint = options.endpoint || DEFAULT_ENDPOINT;
const systemMessage = options.tools
? "You are a helpful assistant. Use the supplied tools to assist the user."
: "You are a helpful assistant.";
const toolsChoice = options.tools ? "auto" : undefined;
const messages = [
{
role: "system",
content: systemMessage,
},
];
if (options.messages) {
messages.push(...options.messages);
}
if (typeof userPrompt === "string") {
messages.push({
role: "user",
content: userPrompt,
});
}
return [promptFetch, { ...options, messages, model, endpoint, toolsChoice }];
}
async function sendPromptRequest(promptFetch, options) {
const { endpoint, token, ...payload } = options;
const method = "POST";
const headers = {
accept: "application/json",
"content-type": "application/json; charset=UTF-8",
"user-agent": "copilot-extensions/preview-sdk.js",
authorization: `Bearer ${token}`,
};
const response = await promptFetch(endpoint, {
method,
headers,
body: JSON.stringify(payload),
});
if (response.ok) {
return response;
}
const body = await response.text();
throw Object.assign(
new Error(
`[@copilot-extensions/preview-sdk] An error occured with the chat completions API`,
),
{
name: "PromptError",
request: {
method: "POST",
url: endpoint,
headers: {
...headers,
authorization: `Bearer [REDACTED]`,
},
body: payload,
},
response: {
status: response.status,
headers: [...response.headers],
body: body,
},
},
);
}
export async function prompt(userPrompt, promptOptions) {
const [promptFetch, options] = parsePromptArguments(
userPrompt,
promptOptions,
);
const response = await sendPromptRequest(promptFetch, options);
const requestId = response.headers.get("x-request-id");
const data = await response.json();
return {
requestId,
message: data.choices[0].message,
};
}
prompt.stream = async function promptStream(userPrompt, promptOptions) {
const [promptFetch, options] = parsePromptArguments(
userPrompt,
promptOptions,
);
const response = await sendPromptRequest(promptFetch, {
...options,
stream: true,
});
return {
requestId: response.headers.get("x-request-id"),
stream: response.body,
};
};
/** @type {import('..').GetFunctionCallsInterface} */
export function getFunctionCalls(payload) {
const functionCalls = payload.message.tool_calls;
if (!functionCalls) return [];
return functionCalls.map((call) => {
return {
id: call.id,
function: {
name: call.function.name,
arguments: call.function.arguments,
},
};
});
}