Skip to content

Commit

Permalink
refector functions to a provider that can inject the functions supported
Browse files Browse the repository at this point in the history
  • Loading branch information
crpietschmann committed Nov 8, 2024
1 parent 1778c72 commit d9c4f87
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 59 deletions.
2 changes: 1 addition & 1 deletion app/nodejs/simple/api/functions/downloadHtmlToMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function downloadHtmlToMarkdown(url) {
// Convert HTML to Markdown
const markdown = turndownService.turndown($.html());

console.log('downloadHtmlToMarkdown Results:', markdown);
// console.log('downloadHtmlToMarkdown Results:', markdown);

return markdown;
} catch (error) {
Expand Down
70 changes: 70 additions & 0 deletions app/nodejs/simple/api/functions/functionProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const downloadHtmlToMarkdown = require('./downloadHtmlToMarkdown');
const searchGoogle = require('./searchGoogle');

//async function functionProvider() {
// return {
const functionProvider = {
getFunctionDefinition: async () => {
return [
{
type: "function",
function: {
name: "downloadHtmlToMarkdown",
description: "Download the contents of a web page URL",
parameters: {
type: "object",
properties: {
url: {
type: "string",
description: "The web page URL of the page to download, e.g. https://build5nines.com/category/page"
}
}
},
required: ["url"]
}
},
{
type: "function",
function: {
name: "searchGoogle",
description: "Search Google for information",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "The search query to use, e.g. Azure Functions"
}
}
}
}
}
];
},
executeFunction: async (functionName, arguments) => {
let functionResult = null;

switch (functionName) {
case "downloadHtmlToMarkdown":
const { url } = JSON.parse(arguments);

// Call the downloadHtmlToMarkdown function and get the result
functionResult = await downloadHtmlToMarkdown(url);
break;

case "searchGoogle":
const { query } = JSON.parse(arguments);

// Call the searchGoogle function and get the result
functionResult = await searchGoogle(query);
break;

default:
console.error("Unsupported Function Call:", functionName);
}

return functionResult;
}
};

module.exports = functionProvider;
4 changes: 3 additions & 1 deletion app/nodejs/simple/api/functions/searchGoogle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ async function searchGoogle(query) {
try {
const url = `https://www.google.com/search?q=${query}`;

console.log('searchGoogle:', query);

let markdown = await downloadHtmlToMarkdown(url);

var maxLength = 15 * 1024;
if (markdown.length > maxLength) {
markdown = markdown.substring(0, maxLength);
}

console.log('searchGoogle Results:', markdown);
// console.log('searchGoogle Results:', markdown);

return markdown;
} catch (error) {
Expand Down
98 changes: 41 additions & 57 deletions app/nodejs/simple/api/v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const azure_search_index_name = process.env["AZURE_SEARCH_INDEX_NAME"];


// Import Functions Used
const downloadHtmlToMarkdown = require('./functions/downloadHtmlToMarkdown');
const searchGoogle = require('./functions/searchGoogle');
const functionProvider = require('./functions/functionProvider');

// Store conversation history in memory for demonstration purposes.
// In a production environment, consider storing this data in a persistent store.
Expand Down Expand Up @@ -92,41 +91,42 @@ module.exports = (app) => {
// https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions
// https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/function-calling?tabs=python
// ERROR: 'Functions are not supported for this API version or this model version. To learn how to user use function calling with Azure OpenAI Service. Please refer to this wiki https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/function-calling?tabs=python'
tools: [
{
type: "function",
function: {
name: "downloadHtmlToMarkdown",
description: "Download the contents of a web page URL",
parameters: {
type: "object",
properties: {
url: {
type: "string",
description: "The web page URL of the page to download, e.g. https://build5nines.com/category/page"
}
}
},
required: ["url"]
}
},
{
type: "function",
function: {
name: "searchGoogle",
description: "Search Google for information",
parameters: {
type: "object",
properties: {
query: {
type: "string",
description: "The search query to use, e.g. Azure Functions"
}
}
}
}
}
],
tools: await functionProvider.getFunctionDefinition(),
// [
// {
// type: "function",
// function: {
// name: "downloadHtmlToMarkdown",
// description: "Download the contents of a web page URL",
// parameters: {
// type: "object",
// properties: {
// url: {
// type: "string",
// description: "The web page URL of the page to download, e.g. https://build5nines.com/category/page"
// }
// }
// },
// required: ["url"]
// }
// },
// {
// type: "function",
// function: {
// name: "searchGoogle",
// description: "Search Google for information",
// parameters: {
// type: "object",
// properties: {
// query: {
// type: "string",
// description: "The search query to use, e.g. Azure Functions"
// }
// }
// }
// }
// }
// ],
tool_choice: "auto", // this is the default behavior when tools are specified anyway

/*
Expand Down Expand Up @@ -211,26 +211,10 @@ module.exports = (app) => {
// Check if the tool call is for the downloadHtmlToMarkdown function
console.log('toolCall:', toolCall);

let functionResult = null;

switch (toolCall.function.name) {
case "downloadHtmlToMarkdown":
const { url } = JSON.parse(toolCall.function.arguments);

// Call the downloadHtmlToMarkdown function and get the result
functionResult = await downloadHtmlToMarkdown(url);
break;

case "searchGoogle":
const { query } = JSON.parse(toolCall.function.arguments);

// Call the searchGoogle function and get the result
functionResult = await searchGoogle(query);
break;

default:
console.error("Unsupported Function Call:", JSON.stringify(toolCall));
}
let functionResult = await functionProvider.executeFunction(
toolCall.function.name,
toolCall.function.arguments
);

// Add the function response to the conversation history
conversationHistoryWithFunctionResults.push(
Expand Down

0 comments on commit d9c4f87

Please sign in to comment.