-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
146 lines (136 loc) · 4.99 KB
/
Copy pathtest.js
File metadata and controls
146 lines (136 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const toolCallMessages = [
{
role: "system" as const,
content: [
"You are an extension of GitHub Copilot, built to interact with GitHub Models.",
"GitHub Models is a language model playground, where you can experiment with different models and see how they respond to your prompts.",
"Here is a list of some of the models available to the user:",
"<-- LIST OF MODELS -->",
JSON.stringify(
[...models.map((model) => ({
friendly_name: model.displayName,
name: model.name,
publisher: model.publisher,
registry: model.registryName,
description: model.summary,
})),
{
friendly_name: "OpenAI o1-mini",
name: "o1-mini",
publisher: "openai",
model_registry: "azure-openai",
description: "Smaller, faster, and 80% cheaper than o1-preview, performs well at code generation and small context operations."
},
{
friendly_name: "OpenAI o1-preview",
name: "o1-preview",
publisher: "openai",
model_registry: "azure-openai",
description: "Focused on advanced reasoning and solving complex problems, including math and science tasks. Ideal for applications that require deep contextual understanding and agentic workflows."
},
]
),
"<-- END OF LIST OF MODELS -->",
].join("\n"),
},
...compatibilityPayload.messages,
];
console.time("tool-call");
const toolCaller = await capiClient.chat.completions.create({
stream: false,
model: "gpt-4o",
messages: toolCallMessages,
tool_choice: "auto",
tools: functions.map((f) => f.tool),
});
console.timeEnd("tool-call");
if (
!toolCaller.choices[0] ||
!toolCaller.choices[0].message ||
!toolCaller.choices[0].message.tool_calls ||
!toolCaller.choices[0].message.tool_calls[0].function
) {
console.log("No tool call found");
// No tool to call, so just call the model with the original messages
const stream = await capiClient.chat.completions.create({
stream: true,
model: "gpt-4o",
messages: payload.messages,
});
for await (const chunk of stream) {
const chunkStr = "data: " + JSON.stringify(chunk) + "\n\n";
response.write(chunkStr);
}
response.write("data: [DONE]\n\n");
response.end();
return;
}
// A tool has been called, so we need to execute the tool's function
const functionToCall = toolCaller.choices[0].message.tool_calls[0].function;
const args = JSON.parse(functionToCall.arguments);
console.time("function-exec");
let functionCallRes: RunnerResponse;
try {
console.log("Executing function", functionToCall.name);
const funcClass = functions.find(
(f) => f.definition.name === functionToCall.name
);
if (!funcClass) {
throw new Error("Unknown function");
}
console.log("\t with args", args);
const func = new funcClass(modelsAPI);
functionCallRes = await func.execute(payload.messages, args);
} catch (err) {
console.error(err);
response.statusCode = 500
response.end();
return;
}
console.timeEnd("function-exec");
// Now that we have a tool result, let's use it to call the model.
try {
let stream: AsyncIterable<any>;
if (functionToCall.name === executeModel.definition.name) {
// First, let's write a reference with the model we're executing.
// Fetch the model data from the index (already in-memory) so we have all the information we need
// to build out the reference URLs
const modelData = await modelsAPI.getModelFromIndex(functionCallRes.model);
const sseData = {
type: "models.reference",
id: `models.reference.${modelData.name}`,
data: {
model: functionCallRes.model
},
is_implicit: false,
metadata: {
display_name: `Model: ${modelData.name}`,
display_icon: "icon",
display_url: `https://github.com/marketplace/models/${modelData.registryName}/${modelData.name}`,
}
};
const event = createReferencesEvent([sseData]);
response.write(event);
if (["o1-mini", "o1-preview"].includes(args.model)) {
// for non-streaming models, we need to still stream the response back, so we build the stream ourselves
stream = (async function*() {
const result = await modelsAPI.inference.chat.completions.create({
model: functionCallRes.model,
messages: functionCallRes.messages
});
yield result;
})();
} else {
stream = await modelsAPI.inference.chat.completions.create({
model: functionCallRes.model,
messages: functionCallRes.messages,
stream: true
});
}
} else {
stream = await capiClient.chat.completions.create({
stream: true,
model: "gpt-4o",
messages: functionCallRes.messages,
});
}