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

feat(prompt): prompt.stream(), options.endpoint #57

Merged
merged 4 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,22 @@ await prompt({
});
```

#### `prompt.stream(message, options)`

Works the same way as `prompt()`, but returns an async iterator instead of a promise.

```js
import { prompt } from "@copilot-extensions/preview-sdk";

const { requestId, stream } = prompt.stream("What is the capital of France?", {
token: process.env.TOKEN,
});

for await (const chunk of stream) {
console.log(new TextDecoder().decode(chunk));
}
```

### `getFunctionCalls()`

Convenience metthod if a result from a `prompt()` call includes function calls.
Expand Down
21 changes: 18 additions & 3 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ export function getUserConfirmationTest(payload: CopilotRequestPayload) {

export async function promptTest() {
const result = await prompt("What is the capital of France?", {
model: "gpt-4",
token: "secret",
});

Expand All @@ -311,7 +310,6 @@ export async function promptTest() {

// with custom fetch
await prompt("What is the capital of France?", {
model: "gpt-4",
token: "secret",
request: {
fetch: () => {},
Expand All @@ -327,7 +325,6 @@ export async function promptTest() {

export async function promptWithToolsTest() {
await prompt("What is the capital of France?", {
model: "gpt-4",
token: "secret",
tools: [
{
Expand Down Expand Up @@ -366,6 +363,24 @@ export async function promptWithoutMessageButMessages() {
});
}

export async function otherPromptOptionsTest() {
const result = await prompt("What is the capital of France?", {
token: "secret",
model: "gpt-4",
endpoint: "https://api.githubcopilot.com",
});
}

export async function promptStreamTest() {
const result = await prompt.stream("What is the capital of France?", {
model: "gpt-4",
token: "secret",
});

expectType<string>(result.requestId);
expectType<ReadableStream<Uint8Array>>(result.stream);
}

export async function getFunctionCallsTest(
promptResponsePayload: PromptResult,
) {
Expand Down
134 changes: 114 additions & 20 deletions test/prompt.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test, suite } from "node:test";
import assert from "node:assert";

import { MockAgent } from "undici";

Expand Down Expand Up @@ -84,13 +85,13 @@
method: "post",
path: `/chat/completions`,
body: JSON.stringify({
model: "<custom-model>",
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(
Expand Down Expand Up @@ -190,6 +191,67 @@
});
});

test("options.endpoint", 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://my-copilot-endpoint.test");
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?",
},
],
model: "gpt-4",
}),
})
.reply(
200,
{
choices: [
{
message: {
content: "<response text>",
},
},
],
},
{
headers: {
"content-type": "application/json",
"x-request-id": "<request-id>",
},
},
);

const result = await prompt("What is the capital of France?", {
token: "secret",
endpoint: "https://my-copilot-endpoint.test/chat/completions",
request: { fetch: fetchMock },
});

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

test("single options argument", async (t) => {
const mockAgent = new MockAgent();
function fetchMock(url, opts) {
Expand Down Expand Up @@ -266,6 +328,12 @@
method: "post",
path: `/chat/completions`,
body: JSON.stringify({
tools: [
{
type: "function",
function: { name: "the_function", description: "The function" },
},
],
messages: [
{
role: "system",
Expand All @@ -275,13 +343,7 @@
{ role: "user", content: "Call the function" },
],
model: "gpt-4",
toolChoice: "auto",
tools: [
{
type: "function",
function: { name: "the_function", description: "The function" },
},
],
toolsChoice: "auto",
}),
})
.reply(
Expand Down Expand Up @@ -360,19 +422,51 @@
},
});

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

t.assert.deepEqual(result, {
message: {
content:
"Sorry, an error occured with the chat completions API. (Status: 400, request ID: <request-id>)",
role: "Sssistant",
await assert.rejects(
async () => {
await prompt("What is the capital of France?", {
token: "secret",
request: { fetch: fetchMock },
});
},
requestId: "<request-id>",
});
{
name: "PromptError",
message:
"[@copilot-extensions/preview-sdk] An error occured with the chat completions API",
request: {
method: "POST",
url: "https://api.githubcopilot.com/chat/completions",
headers: {
"content-type": "application/json; charset=UTF-8",
"user-agent": "copilot-extensions/preview-sdk.js",
accept: "application/json",
authorization: "Bearer [REDACTED]",
},
body: {
messages: [
{
content: "You are a helpful assistant.",
role: "system",
},
{
content: "What is the capital of France?",
role: "user",
},
],
model: "gpt-4",
toolsChoice: undefined,
},
},
response: {
status: 400,
headers: [
["content-type", "text/plain"],
["x-request-id", "<request-id>"],
],
body: "Bad Request",
},
},
);
});

suite("getFunctionCalls()", () => {
Expand Down
Loading