Skip to content

Commit

Permalink
feat: options.messages for prompt() and prompt(options)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Sep 2, 2024
1 parent bb08b13 commit 2a3f033
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions lib/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@

/** @type {import('..').PromptInterface} */
export async function prompt(userPrompt, promptOptions) {
const promptFetch = promptOptions.request?.fetch || fetch;
const options = typeof userPrompt === "string" ? promptOptions : userPrompt;

const systemMessage = promptOptions.tools
const promptFetch = options.request?.fetch || fetch;

const systemMessage = options.tools
? "You are a helpful assistant. Use the supplied tools to assist the user."
: "You are a helpful assistant.";

const messages = [
{
role: "system",
content: systemMessage,
},
];

if (options.messages) {
messages.push(...options.messages);
}

if (typeof userPrompt === "string") {
messages.push({
role: "user",
content: userPrompt,
});
}

const response = await promptFetch(
"https://api.githubcopilot.com/chat/completions",
{
Expand All @@ -16,22 +36,13 @@ export async function prompt(userPrompt, promptOptions) {
accept: "application/json",
"content-type": "application/json; charset=UTF-8",
"user-agent": "copilot-extensions/preview-sdk.js",
authorization: `Bearer ${promptOptions.token}`,
authorization: `Bearer ${options.token}`,
},
body: JSON.stringify({
messages: [
{
role: "system",
content: systemMessage,
},
{
role: "user",
content: userPrompt,
},
],
model: promptOptions.model,
toolChoice: promptOptions.tools ? "auto" : undefined,
tools: promptOptions.tools,
messages: messages,
model: options.model,
toolChoice: options.tools ? "auto" : undefined,
tools: options.tools,
}),
}
);
Expand Down

0 comments on commit 2a3f033

Please sign in to comment.