mirror of
https://github.com/technovangelist/videoprojects.git
synced 2026-09-10 07:16:19 -04:00
Added the code for the latest video...late as usual
Signed-off-by: Matt Williams <m@technovangelist.com>
This commit is contained in:
parent
cdb372c298
commit
a1fbd67307
175
2024-08-01-anotherfunctioncallingvideo/.gitignore
vendored
Normal file
175
2024-08-01-anotherfunctioncallingvideo/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
|
||||
|
||||
# Logs
|
||||
|
||||
logs
|
||||
_.log
|
||||
npm-debug.log_
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# Caches
|
||||
|
||||
.cache
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
|
||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# Runtime data
|
||||
|
||||
pids
|
||||
_.pid
|
||||
_.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Snowpack dependency directory (https://snowpack.dev/)
|
||||
|
||||
web_modules/
|
||||
|
||||
# TypeScript cache
|
||||
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
|
||||
.eslintcache
|
||||
|
||||
# Optional stylelint cache
|
||||
|
||||
.stylelintcache
|
||||
|
||||
# Microbundle cache
|
||||
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variable files
|
||||
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
|
||||
.parcel-cache
|
||||
|
||||
# Next.js build output
|
||||
|
||||
.next
|
||||
out
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
|
||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
|
||||
.vuepress/dist
|
||||
|
||||
# vuepress v2.x temp and cache directory
|
||||
|
||||
.temp
|
||||
|
||||
# Docusaurus cache and generated files
|
||||
|
||||
.docusaurus
|
||||
|
||||
# Serverless directories
|
||||
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
|
||||
.tern-port
|
||||
|
||||
# Stores VSCode versions used for testing VSCode extensions
|
||||
|
||||
.vscode-test
|
||||
|
||||
# yarn v2
|
||||
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.*
|
||||
|
||||
# IntelliJ based IDEs
|
||||
.idea
|
||||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
6
2024-08-01-anotherfunctioncallingvideo/README.md
Normal file
6
2024-08-01-anotherfunctioncallingvideo/README.md
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# 2024-08-01-anotherfunctioncallingvideo
|
||||
|
||||
This code was used in a video on 2024-08-01 to show how to use function calling with Ollama. Yes, there are two other videos on this topic, but this one covers a newer technique. Unfortunately the newer technique isn't as reliable as the older one, at least not yet. Hopefully it will be soon.
|
||||
|
||||
I use Bun to run this. You can find the video here: https://youtu.be/hb5iwDpsPwc
|
||||
|
||||
BIN
2024-08-01-anotherfunctioncallingvideo/bun.lockb
Executable file
BIN
2024-08-01-anotherfunctioncallingvideo/bun.lockb
Executable file
Binary file not shown.
107
2024-08-01-anotherfunctioncallingvideo/index.ts
Normal file
107
2024-08-01-anotherfunctioncallingvideo/index.ts
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
import ollama from "ollama";
|
||||
|
||||
async function WeatherFromCity(city: string) {
|
||||
const output = await fetch(
|
||||
`https://nominatim.openstreetmap.org/search?q=${city}&format=json`,
|
||||
);
|
||||
const json = await output.json();
|
||||
const lat = json[0].lat;
|
||||
const lon = json[0].lon;
|
||||
return await WeatherFromLatLon(lat, lon);
|
||||
}
|
||||
|
||||
async function WeatherFromLatLon(latitude: string, longitude: string) {
|
||||
const output = await fetch(
|
||||
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t=temperature_2m&temperature_unit=fahrenheit&wind_speed_unit=mph&forecast_days=1`,
|
||||
);
|
||||
|
||||
const json = await output.json();
|
||||
return `${json.current.temperature_2m} degrees Farenheit`;
|
||||
}
|
||||
|
||||
const model = "firefunction-v2";
|
||||
const prompt = "What is the weather in Paris?";
|
||||
const messages = [{ role: "user", content: prompt }];
|
||||
|
||||
const response = await ollama.chat({
|
||||
model: model,
|
||||
messages: messages,
|
||||
stream: false,
|
||||
tools: [
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
name: "WeatherFromCity",
|
||||
description: "Get the weather for a given city",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
city: {
|
||||
type: "string",
|
||||
description: "The city to get the weather for",
|
||||
},
|
||||
},
|
||||
required: ["city"],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
name: "WeatherFromLatLon",
|
||||
description: "Get the weather for a specific latitude and longitude",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: {
|
||||
latitude: {
|
||||
type: "string",
|
||||
description: "latitude for the location",
|
||||
},
|
||||
longitude: {
|
||||
type: "string",
|
||||
description: "longitude for the location",
|
||||
},
|
||||
},
|
||||
required: ["latitude", "longitude"],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
messages.push(response.message);
|
||||
|
||||
if (!response.message.tool_calls || response.message.tool_calls.length === 0) {
|
||||
// console.log("The model didn't use the function. Its response was:");
|
||||
// console.log(response.message.content);
|
||||
} else {
|
||||
for (const t of response.message.tool_calls) {
|
||||
// console.log(
|
||||
// `The model used the function ${t.function.name} with parameters ${JSON.stringify(t.function.arguments)}`,
|
||||
// );
|
||||
let functionoutput = "";
|
||||
switch (t.function.name) {
|
||||
case "WeatherFromCity":
|
||||
functionoutput = await WeatherFromCity(t.function.arguments.city);
|
||||
break;
|
||||
case "WeatherFromLatLon":
|
||||
functionoutput = await WeatherFromLatLon(
|
||||
t.function.arguments.latitude,
|
||||
t.function.arguments.longitude,
|
||||
);
|
||||
break;
|
||||
}
|
||||
messages.push({ role: "tool", content: functionoutput });
|
||||
const response2 = await ollama.chat({
|
||||
model: model,
|
||||
messages: messages,
|
||||
stream: false,
|
||||
});
|
||||
|
||||
console.log(JSON.stringify(messages, null, 2));
|
||||
console.log(response2.message.content);
|
||||
console.log(`This used the model: ${model}`);
|
||||
}
|
||||
|
||||
// console.log(response.message.content);
|
||||
}
|
||||
14
2024-08-01-anotherfunctioncallingvideo/package.json
Normal file
14
2024-08-01-anotherfunctioncallingvideo/package.json
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"name": "2024-08-01-anotherfunctioncallingvideo",
|
||||
"module": "index.ts",
|
||||
"type": "module",
|
||||
"devDependencies": {
|
||||
"@types/bun": "latest"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"ollama": "^0.5.6"
|
||||
}
|
||||
}
|
||||
27
2024-08-01-anotherfunctioncallingvideo/tsconfig.json
Normal file
27
2024-08-01-anotherfunctioncallingvideo/tsconfig.json
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
// Enable latest features
|
||||
"lib": ["ESNext", "DOM"],
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleDetection": "force",
|
||||
"jsx": "react-jsx",
|
||||
"allowJs": true,
|
||||
|
||||
// Bundler mode
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"noEmit": true,
|
||||
|
||||
// Best practices
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
|
||||
// Some stricter flags (disabled by default)
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"noPropertyAccessFromIndexSignature": false
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue