in src/app/gemini.service.ts [54:129]
async generateResponse(prompt: string) {
if (!this.model) {
this.lastResponse = {
type: "error",
response: 'You must specify a model name and valid API Key.',
};
return;
}
try {
this.lastResponse = { type: "waiting" };
this.model.tools = [{
functionDeclarations: functionDeclarations,
}];
this.model.toolConfig = {
functionCallingConfig: {
// Require function calling response.
mode: FunctionCallingMode.ANY,
allowedFunctionNames: ["createTable", "alterTable"],
}
};
if (this.systemInstruction) {
this.model.systemInstruction = {
role: "user",
parts: [{ text: this.systemInstruction }],
};
}
prompt = prompt + "\nCurrent database schema:\n" +
(this.database.tables.length ?
JSON.stringify(this.database.tables)
: "None, the database does not contain any table definitions.");
this.log.info("Sending prompt:\n-----\n" + prompt + "\n-----");
const result = await this.model.generateContent(prompt);
const calls = result.response.functionCalls();
if (calls) {
this.log.info("Received", calls.length, "function calls.");
const success = calls.every((fc, i) => {
this.log.info("Received function call response:", fc);
const err = this.database.callFunction(fc);
if (err) {
this.log.error("Error calling function: " + fc.name, err);
return false;
}
this.log.info("Successfully called function " + fc.name);
return true;
});
this.lastResponse = {
type: success ? "functionCalls" : "invalidFunctionCalls",
response: JSON.stringify(calls, null, 2),
};
} else if (result.response.text()) {
this.log.info("Received text response:", result.response.text());
this.lastResponse = {
type: "text",
response: result.response.text(),
};
} else {
this.lastResponse = {
type: "unknown",
response: JSON.stringify(result.response),
}
}
} catch (e) {
this.lastResponse = {
type: "error",
response: '' + e,
}
// Rethrow.
throw e;
}
}