Skip to content

Commit

Permalink
chore: small improvement to tool example (#143)
Browse files Browse the repository at this point in the history
The tool example was quite useful, but my first extension was to
add a second function which leads to this suggested change.

I think it is better to handle the mapping of the args object returned
to the specific argments needed in the function itself. This allows
additional functions to be added by simply adding to the json
desribing the available tools and the list of tools that can be called.

The example was already half way there by pulling the function from
an array based on the name, but missed handling the parameters in
a generic way to complete making invocation of the function generic.

Signed-off-by: Michael Dawson <[email protected]>
  • Loading branch information
mhdawson authored Sep 18, 2024
1 parent 3a9d651 commit 6b6221d
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions examples/tools/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import ollama from 'ollama';

// Simulates an API call to get flight times
// In a real application, this would fetch data from a live database or API
function getFlightTimes(departure: string, arrival: string) {
function getFlightTimes(args: { [key: string]: any }) {
// this is where you would validate the arguments you received
const departure = args.departure;
const arrival = args.arrival;

const flights = {
"NYC-LAX": { departure: "08:00 AM", arrival: "11:30 AM", duration: "5h 30m" },
"LAX-NYC": { departure: "02:00 PM", arrival: "10:30 PM", duration: "5h 30m" },
Expand Down Expand Up @@ -65,10 +69,7 @@ async function run(model: string) {
};
for (const tool of response.message.tool_calls) {
const functionToCall = availableFunctions[tool.function.name];
const functionResponse = functionToCall(
tool.function.arguments.departure,
tool.function.arguments.arrival
);
const functionResponse = functionToCall(tool.function.arguments);
// Add function response to the conversation
messages.push({
role: 'tool',
Expand All @@ -85,4 +86,4 @@ async function run(model: string) {
console.log(finalResponse.message.content);
}

run('mistral').catch(error => console.error("An error occurred:", error));
run('mistral').catch(error => console.error("An error occurred:", error));

0 comments on commit 6b6221d

Please sign in to comment.