Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(prompt): parameters.model is now a string and is optional. It defaults to gpt-4 #53

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import { prompt } from "@copilot-extensions/preview-sdk";

try {
const { message } = await prompt("What is the capital of France?", {
model: "gpt-4",
token: process.env.TOKEN,
});

Expand Down
21 changes: 6 additions & 15 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,12 @@ export interface GetUserConfirmationInterface {

/**
* model names supported by Copilot API
*
* Based on https://api.githubcopilot.com/models from 2024-09-02
* A list of currently supported models can be retrieved at
* https://api.githubcopilot.com/models. We set `ModelName` to `string`
* instead of a union of the supported models as we cannot give
* guarantees about the supported models in the future.
*/
export type ModelName =
| "gpt-3.5-turbo"
| "gpt-3.5-turbo-0613"
| "gpt-4"
| "gpt-4-0613"
| "gpt-4-o-preview"
| "gpt-4o"
| "gpt-4o-2024-05-13"
| "text-embedding-3-small"
| "text-embedding-3-small-inference"
| "text-embedding-ada-002"
| "text-embedding-ada-002-index"
export type ModelName = string

export interface PromptFunction {
type: "function"
Expand All @@ -284,7 +275,7 @@ export interface PromptFunction {
}

export type PromptOptions = {
model: ModelName
model?: ModelName
token: string
tools?: PromptFunction[]
messages?: InteropMessage[]
Expand Down
3 changes: 0 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,6 @@ export async function promptTest() {
// @ts-expect-error - 2nd argument is required
prompt("What is the capital of France?")

// @ts-expect-error - model argument is required
prompt("What is the capital of France?", { token: "" })

// @ts-expect-error - token argument is required
prompt("What is the capital of France?", { model: "" })
}
Expand Down
3 changes: 2 additions & 1 deletion lib/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export async function prompt(userPrompt, promptOptions) {
const options = typeof userPrompt === "string" ? promptOptions : userPrompt;

const promptFetch = options.request?.fetch || fetch;
const modelName = options.model || "gpt-4";

const systemMessage = options.tools
? "You are a helpful assistant. Use the supplied tools to assist the user."
Expand Down Expand Up @@ -40,7 +41,7 @@ export async function prompt(userPrompt, promptOptions) {
},
body: JSON.stringify({
messages: messages,
model: options.model,
model: modelName,
toolChoice: options.tools ? "auto" : undefined,
tools: options.tools,
}),
Expand Down
66 changes: 61 additions & 5 deletions test/prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,67 @@ suite("prompt", () => {

const result = await prompt("What is the capital of France?", {
token: "secret",
model: "gpt-4",
request: { fetch: fetchMock },
});

t.assert.deepEqual(result, {
requestId: "<request-id>",
message: {
content: "<response text>",
},
});
});

test("options.prompt", async (t) => {
const mockAgent = new MockAgent();
function fetchMock(url, opts) {
opts ||= {};
opts.dispatcher = mockAgent;
return fetch(url, opts);
}

mockAgent.disableNetConnect();
const mockPool = mockAgent.get("https://api.githubcopilot.com");
mockPool
.intercept({
method: "post",
path: `/chat/completions`,
body: JSON.stringify({
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What is the capital of France?" },
{ role: "assistant", content: "The capital of France is Paris." },
{ role: "user", content: "What about Spain?" },
],
model: "<custom-model>",
}),
})
.reply(
200,
{
choices: [
{
message: {
content: "<response text>",
},
},
],
},
{
headers: {
"content-type": "application/json",
"x-request-id": "<request-id>",
},
}
);

const result = await prompt("What about Spain?", {
model: "<custom-model>",
token: "secret",
messages: [
{ role: "user", content: "What is the capital of France?" },
{ role: "assistant", content: "The capital of France is Paris." },
],
request: { fetch: fetchMock },
});

Expand Down Expand Up @@ -114,7 +174,6 @@ suite("prompt", () => {
);

const result = await prompt("What about Spain?", {
model: "gpt-4",
token: "secret",
messages: [
{ role: "user", content: "What is the capital of France?" },
Expand Down Expand Up @@ -175,7 +234,6 @@ suite("prompt", () => {
);

const result = await prompt({
model: "gpt-4",
token: "secret",
messages: [
{ role: "user", content: "What is the capital of France?" },
Expand Down Expand Up @@ -247,7 +305,6 @@ suite("prompt", () => {

const result = await prompt("Call the function", {
token: "secret",
model: "gpt-4",
tools: [
{
type: "function",
Expand Down Expand Up @@ -305,7 +362,6 @@ suite("prompt", () => {

const result = await prompt("What is the capital of France?", {
token: "secret",
model: "gpt-4",
request: { fetch: fetchMock },
});

Expand Down