Skip to content

Commit 4fd4539

Browse files
authoredSep 17, 2024
Merge pull request #13 from copilot-extensions/sgoedecke/add-model-ref-and-update-base-url
Add model copilot_reference and update base catalog url
2 parents 41dcf34 + f9535ee commit 4fd4539

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed
 

‎src/functions/execute-model.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ Example Queries (IMPORTANT: Phrasing doesn't have to match):
5656
);
5757

5858
const content = [
59-
`The user has chosen to use the model named ${args.model}. Begin your response with the following phrase: "The model you've selected is ${args.model}".`,
60-
"Do not include any additional information about the selected model in this first sentence - ONLY the name.",
59+
`The user has chosen to use the model named ${args.model}.`,
6160
];
6261

6362
if (importantRefs.length > 0) {

‎src/index.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createServer, IncomingMessage } from "node:http";
22

3-
import { verifyAndParseRequest, transformPayloadForOpenAICompatibility } from "@copilot-extensions/preview-sdk";
3+
import { verifyAndParseRequest, transformPayloadForOpenAICompatibility, createReferencesEvent } from "@copilot-extensions/preview-sdk";
44
import OpenAI from "openai";
55

66
import { describeModel } from "./functions/describe-model.js";
@@ -139,6 +139,7 @@ const server = createServer(async (request, response) => {
139139
return;
140140
}
141141

142+
// A tool has been called, so we need to execute the tool's function
142143
const functionToCall = toolCaller.choices[0].message.tool_calls[0].function;
143144
const args = JSON.parse(functionToCall.arguments);
144145

@@ -164,8 +165,34 @@ const server = createServer(async (request, response) => {
164165
}
165166
console.timeEnd("function-exec");
166167

168+
// Now that we have a tool result, let's use it to call the model. Note that we're calling the model
169+
// via the Models API, instead of the Copilot Chat API, so that if we're in the execute-model tool we
170+
// can switch out the default model name for the requested model. We could change this in the future
171+
// if we want to handle rate-limited users more gracefully or the model difference becomes a problem.
167172
try {
168-
// We should keep all optional parameters out of this call, so it can work for any model.
173+
if (functionToCall.name === executeModel.definition.name) {
174+
// fetch the model data from the index (already in-memory) so we have all the information we need
175+
// to build out the reference URLs
176+
const modelData = await modelsAPI.getModelFromIndex(functionCallRes.model);
177+
const sseData = {
178+
type: "models.reference",
179+
id: `models.reference.${modelData.name}`,
180+
data: {
181+
model: functionCallRes.model
182+
},
183+
is_implicit: false,
184+
metadata: {
185+
display_name: `Model: ${modelData.name}`,
186+
display_icon: "icon",
187+
display_url: `https://github.com/marketplace/models/${modelData.registryName}/${modelData.name}`,
188+
}
189+
};
190+
const event = createReferencesEvent([sseData]);
191+
response.write(event);
192+
}
193+
194+
// We should keep all optional parameters out of this call, so it can work for any model (in case we've
195+
// just run the execute-model tool).
169196
const stream = await modelsAPI.inference.chat.completions.create({
170197
model: functionCallRes.model,
171198
messages: functionCallRes.messages,

‎src/models-api.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class ModelsAPI {
4545
const modelFromIndex = await this.getModelFromIndex(modelName);
4646

4747
const modelRes = await fetch(
48-
`https://eastus.api.azureml.ms/asset-gallery/v1.0/${modelFromIndex.registryName}/models/${modelFromIndex.name}/version/${modelFromIndex.version}`,
48+
`https://api.catalog.azureml.ms/asset-gallery/v1.0/${modelFromIndex.registryName}/models/${modelFromIndex.name}/version/${modelFromIndex.version}`,
4949
);
5050
if (!modelRes.ok) {
5151
throw new Error(`Failed to fetch ${modelName} details from the model catalog.`);
@@ -75,7 +75,7 @@ export class ModelsAPI {
7575
}
7676

7777
const modelsRes = await fetch(
78-
"https://eastus.api.azureml.ms/asset-gallery/v1.0/models",
78+
"https://api.catalog.azureml.ms/asset-gallery/v1.0/models",
7979
{
8080
method: "POST",
8181
headers: {

0 commit comments

Comments
 (0)
Failed to load comments.