Skip to content

Commit

Permalink
feat: prompt()
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Sep 2, 2024
1 parent 3882b61 commit 42533d0
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
48 changes: 47 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ export interface Message {
content: string
copilot_references: MessageCopilotReference[]
copilot_confirmations?: MessageCopilotConfirmation[]
tool_calls?: {
"function": {
"arguments": string,
"name": string
},
"id": string,
"type": "function"
}[]
name?: string
}

Expand Down Expand Up @@ -244,6 +252,42 @@ export interface GetUserConfirmationInterface {
(payload: CopilotRequestPayload): UserConfirmation | undefined;
}

// prompt

/** model names supported by Copilot API */
export type ModelName =
| "gpt-4"
| "gpt-3.5-turbo"

export interface PromptFunction {
type: "function"
function: {
name: string;
description?: string;
/** @see https://platform.openai.com/docs/guides/structured-outputs/supported-schemas */
parameters?: Record<string, unknown>;
strict?: boolean | null;
}
}

export type PromptOptions = {
model: ModelName
token: string
tools?: PromptFunction[]
request?: {
fetch?: Function
}
}

export type PromptResult = {
requestId: string
message: Message
}

interface PromptInterface {
(userPrompt: string, options: PromptOptions): Promise<PromptResult>;
}

// exported methods

export declare const verifyRequest: VerifyRequestInterface;
Expand All @@ -261,4 +305,6 @@ export declare const parseRequestBody: ParseRequestBodyInterface;
export declare const transformPayloadForOpenAICompatibility: TransformPayloadForOpenAICompatibilityInterface;
export declare const verifyAndParseRequest: VerifyAndParseRequestInterface;
export declare const getUserMessage: GetUserMessageInterface;
export declare const getUserConfirmation: GetUserConfirmationInterface;
export declare const getUserConfirmation: GetUserConfirmationInterface;

export declare const prompt: PromptInterface;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
export * from "./lib/parse.js";
export * from "./lib/response.js";
export * from "./lib/verification.js";
export * from "./lib/prompt.js";
45 changes: 45 additions & 0 deletions lib/prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// @ts-check

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

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

const response = await promptFetch(
"https://api.githubcopilot.com/chat/completions",
{
method: "POST",
headers: {
accept: "application/json",
"content-type": "application/json; charset=UTF-8",
"user-agent": "copilot-extensions/preview-sdk.js",
authorization: `Bearer ${promptOptions.token}`,
},
body: JSON.stringify({
messages: [
{
role: "system",
content: systemMessage,
},
{
role: "user",
content: userPrompt,
},
],
model: promptOptions.model,
toolChoice: promptOptions.tools ? "auto" : undefined,
tools: promptOptions.tools,
}),
}
);

const data = await response.json();

return {
requestId: response.headers.get("x-request-id"),
message: data.choices[0].message,
};
}

0 comments on commit 42533d0

Please sign in to comment.