@@ -7,10 +7,6 @@ import { listModels } from "./functions/list-models";
7
7
import { RunnerResponse } from "./functions" ;
8
8
import { recommendModel } from "./functions/recommend-model" ;
9
9
import { ModelsAPI } from "./models-api" ;
10
-
11
- // List of functions that are available to be called
12
- const functions = [ listModels , describeModel , executeModel , recommendModel ] ;
13
-
14
10
const app = express ( ) ;
15
11
16
12
app . post ( "/" , verifySignatureMiddleware , express . json ( ) , async ( req , res ) => {
@@ -21,17 +17,44 @@ app.post("/", verifySignatureMiddleware, express.json(), async (req, res) => {
21
17
return ;
22
18
}
23
19
20
+ // List of functions that are available to be called
21
+ const modelsAPI = new ModelsAPI ( apiKey ) ;
22
+ const functions = [ listModels , describeModel , executeModel , recommendModel ] ;
23
+
24
24
// Use the Copilot API to determine which function to execute
25
25
const capiClient = new OpenAI ( {
26
26
baseURL : "https://api.githubcopilot.com" ,
27
27
apiKey,
28
28
} ) ;
29
29
30
+ // Prepend a system message that includes the list of models, so that
31
+ // tool calls can better select the right model to use.
32
+ const models = await modelsAPI . listModels ( ) ;
33
+ const toolCallMessages = [
34
+ {
35
+ role : "system" ,
36
+ content : [
37
+ "You are an extension of GitHub Copilot, built to interact with GitHub Models." ,
38
+ "GitHub Models is a language model playground, where you can experiment with different models and see how they respond to your prompts." ,
39
+ "Here is a list of some of the models available to the user:" ,
40
+ JSON . stringify (
41
+ models . map ( ( model ) => ( {
42
+ name : model . name ,
43
+ publisher : model . publisher ,
44
+ registry : model . model_registry ,
45
+ description : model . summary ,
46
+ } ) )
47
+ ) ,
48
+ ] . join ( "\n" ) ,
49
+ } ,
50
+ ...req . body . messages ,
51
+ ] . concat ( req . body . messages ) ;
52
+
30
53
console . time ( "tool-call" ) ;
31
54
const toolCaller = await capiClient . chat . completions . create ( {
32
55
stream : false ,
33
56
model : "gpt-4" ,
34
- messages : req . body . messages ,
57
+ messages : toolCallMessages ,
35
58
tool_choice : "auto" ,
36
59
tools : functions . map ( ( f ) => f . tool ) ,
37
60
} ) ;
@@ -63,20 +86,19 @@ app.post("/", verifySignatureMiddleware, express.json(), async (req, res) => {
63
86
const args = JSON . parse ( functionToCall . arguments ) ;
64
87
65
88
console . time ( "function-exec" ) ;
66
- const modelsAPI = new ModelsAPI ( apiKey ) ;
67
89
let functionCallRes : RunnerResponse ;
68
90
try {
69
91
console . log ( "Executing function" , functionToCall . name ) ;
70
- const klass = functions . find (
92
+ const funcClass = functions . find (
71
93
( f ) => f . definition . name === functionToCall . name
72
94
) ;
73
- if ( ! klass ) {
95
+ if ( ! funcClass ) {
74
96
throw new Error ( "Unknown function" ) ;
75
97
}
76
98
77
99
console . log ( "\t with args" , args ) ;
78
- const inst = new klass ( modelsAPI ) ;
79
- functionCallRes = await inst . execute ( req . body . messages , args ) ;
100
+ const func = new funcClass ( modelsAPI ) ;
101
+ functionCallRes = await func . execute ( req . body . messages , args ) ;
80
102
} catch ( err ) {
81
103
console . error ( err ) ;
82
104
res . status ( 500 ) . end ( ) ;
0 commit comments