Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding lesson 6 #104

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/public/characters.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,14 @@
"image": "ada.jpeg",
"avatar": "ada-avatar.jpeg",
"voice": 2
},
{
"title": "Amelia Earhart",
"name": "ada",
"description": "You are Amelia Earhart a pioneer in aviation, skilled pilot and mechanic. Limit your responses to only the time you live in, you don't know anything else. When asked about Ada Lovelace, you say you know her but you can't talk about it or it would jeopardize the future",
"page": "amelia.html",
"image": "amelia-front.jpeg",
"avatar": "amelia-avatar.jpeg",
"voice": 2
}
]
Binary file added app/public/images/amelia-avatar.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/public/images/amelia-front.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
426 changes: 426 additions & 0 deletions lessons/06-tool-calling/README.md

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/06-tool-calling/assets/amelia-front.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lessons/06-tool-calling/assets/amelia.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 108 additions & 0 deletions lessons/06-tool-calling/code/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { OpenAI } from 'openai';

function findLandingSpot(lat, long) {
console.log("[Function] Finding landing spot with coordinates: ", lat, long);
// Perform the task of finding a suitable landing spot
// Return the coordinates of the landing spot
return { lat: 7.5, long: 134.5 };
}

function getBackgroundOnCharacter(character= "unknown") {
console.log("[Function] Getting background on character: ", character);
// Perform the task of getting background information on a character
// Return the background information
return `Background information on ${character}`;
}

const getBackgroundOnCharacterJson = {
name: "get-background-on-character",
description: "Get background information on a character",
parameters: {
type: "object",
properties: {
name: {
type: "string",
description: "The name of the character",
}
},
required: ["lat", "long"],
},
output: { type: "string" }
};

const findLandingSpotJson = {
name: "find-landing-spot",
description: "Finds a suitable landing spot",
parameters: {
type: "object",
properties: {
lat: {
type: "number",
description: "The latitude of the location",
},
long: {
type: "number",
description: "The longitude of the location",
},
},
required: ["lat", "long"],
},
output: { type: "object", properties: { lat: "number", long: "number" } }
};

const tools = {
[findLandingSpotJson.name]: findLandingSpot,
[getBackgroundOnCharacterJson.name]: getBackgroundOnCharacter
};


const openai = new OpenAI({
baseURL: "https://models.inference.ai.azure.com", // might need to change to this url in the future: https://models.github.ai/inference
apiKey: process.env.GITHUB_TOKEN,
});

/*
// {
// role: "user",
// content: `We need to know where to land, here's the coordinates: 7.5, 134.5. `,
// },
*/

const messages = [
{
role: "system",
content: `You are a helpful assistant. You can call functions to perform tasks. Make sure to parse the function call and arguments correctly.`
}, {
role: "user",
content: "Can you give me some background on the character named Amelia Earhart?"
}
];

async function main(){
console.log("Making LLM call")

const result = await openai.chat.completions.create({
model: 'gpt-4o',
messages: messages,
functions: [getBackgroundOnCharacterJson, findLandingSpotJson]
});

for (const choice of result.choices) {
// console.log("Result", choice.message);

let functionCall = choice.message?.function_call;
let functionName = functionCall?.name;
let args = JSON.parse(functionCall?.arguments);
// console.log("Wants to call: ", choice.message?.function_call);
// console.log("With args: ", args);
if (functionName && functionName in tools) {
console.log(`Calling [${functionName}]`);
const toolFunction = tools[functionName];
const toolResponse = toolFunction(...Object.values(args)); // Extract values from args and spread them
console.log("Result from [tool] calling: ", toolResponse);
}
}

}

main();
Loading