-
Notifications
You must be signed in to change notification settings - Fork 264
adding lesson 6 #104
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
Merged
Merged
adding lesson 6 #104
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3160f45
adding lesson 6
2fe667a
adding working example for tool calling
softchris 4b54ab3
adding character section
3445ae8
adding solution and app artefacts
179a27a
fixing solution
softchris 45d0437
fixing punctuation
softchris c7a98f4
Fix list formatting in README.md
softchris a40611b
fixing bullet list
54addae
Apply suggestions from code review
softchris 29c2d80
addressing comments
softchris File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Large diffs are not rendered by default.
Oops, something went wrong.
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor detail, but let's remove unncessasry commas just to clean it up a bit.