From c8b6a7d76d95cc29e3ed7677549eb7b0737d49d3 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Tue, 3 Sep 2024 20:55:31 -0700 Subject: [PATCH] fix(prompt): `parameters.model` is now a string and is optional. It defaults to `gpt-4` (#53) - **fix(types): `ModelName` is a string now** - **docs(README): `model` parameter is no longer required for `prompt()`. It defaults to `gpt-4`** - **fix: `model` parameter is now optional for `prompt()`. It defaults to `gpt-4`** --- README.md | 1 - index.d.ts | 21 +++++---------- index.test-d.ts | 3 --- lib/prompt.js | 3 ++- test/prompt.test.js | 66 +++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 69 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index c53b721..99d64a7 100644 --- a/README.md +++ b/README.md @@ -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, }); diff --git a/index.d.ts b/index.d.ts index 7f5a605..533dbe0 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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" @@ -284,7 +275,7 @@ export interface PromptFunction { } export type PromptOptions = { - model: ModelName + model?: ModelName token: string tools?: PromptFunction[] messages?: InteropMessage[] diff --git a/index.test-d.ts b/index.test-d.ts index 0c0ad09..59af857 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -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: "" }) } diff --git a/lib/prompt.js b/lib/prompt.js index 8fe4901..7d90ed7 100644 --- a/lib/prompt.js +++ b/lib/prompt.js @@ -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." @@ -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, }), diff --git a/test/prompt.test.js b/test/prompt.test.js index 26c8fbf..f6a5946 100644 --- a/test/prompt.test.js +++ b/test/prompt.test.js @@ -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: "", + message: { + content: "", + }, + }); + }); + + 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: "", + }), + }) + .reply( + 200, + { + choices: [ + { + message: { + content: "", + }, + }, + ], + }, + { + headers: { + "content-type": "application/json", + "x-request-id": "", + }, + } + ); + + const result = await prompt("What about Spain?", { + 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 }, }); @@ -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?" }, @@ -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?" }, @@ -247,7 +305,6 @@ suite("prompt", () => { const result = await prompt("Call the function", { token: "secret", - model: "gpt-4", tools: [ { type: "function", @@ -305,7 +362,6 @@ suite("prompt", () => { const result = await prompt("What is the capital of France?", { token: "secret", - model: "gpt-4", request: { fetch: fetchMock }, });