From a1fbd673070327778a2d7e8f02c4cf24cf2f7f28 Mon Sep 17 00:00:00 2001 From: Matt Williams Date: Fri, 2 Aug 2024 09:38:49 -0400 Subject: [PATCH] Added the code for the latest video...late as usual Signed-off-by: Matt Williams --- .../.gitignore | 175 ++++++++++++++++++ .../README.md | 6 + .../bun.lockb | Bin 0 -> 3867 bytes .../index.ts | 107 +++++++++++ .../package.json | 14 ++ .../tsconfig.json | 27 +++ 6 files changed, 329 insertions(+) create mode 100644 2024-08-01-anotherfunctioncallingvideo/.gitignore create mode 100644 2024-08-01-anotherfunctioncallingvideo/README.md create mode 100755 2024-08-01-anotherfunctioncallingvideo/bun.lockb create mode 100644 2024-08-01-anotherfunctioncallingvideo/index.ts create mode 100644 2024-08-01-anotherfunctioncallingvideo/package.json create mode 100644 2024-08-01-anotherfunctioncallingvideo/tsconfig.json diff --git a/2024-08-01-anotherfunctioncallingvideo/.gitignore b/2024-08-01-anotherfunctioncallingvideo/.gitignore new file mode 100644 index 0000000..9b1ee42 --- /dev/null +++ b/2024-08-01-anotherfunctioncallingvideo/.gitignore @@ -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 diff --git a/2024-08-01-anotherfunctioncallingvideo/README.md b/2024-08-01-anotherfunctioncallingvideo/README.md new file mode 100644 index 0000000..237d1fe --- /dev/null +++ b/2024-08-01-anotherfunctioncallingvideo/README.md @@ -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 + diff --git a/2024-08-01-anotherfunctioncallingvideo/bun.lockb b/2024-08-01-anotherfunctioncallingvideo/bun.lockb new file mode 100755 index 0000000000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xI- zm>{rbt}+w~K2YIGTrR`78kDOc;f+Fu%N4kat4g`565c3rMF8&qFPPD=LL?{Hs5qhW z2;|A6poye3g2KZx7=kit3~w_OX^amM%ew-ndLnQPt+lcoe(bq}%3te=3VQU+{n|G5 zy&);(3{4XhFvgr0FNxAcJaT{-p*nh*N_kq4;ntyIy;#Z?YLH8?Px7a26Q+E#x%#nno@lOiX4nL2D^8nHg{GO5wMKo?(*dAgPoh;z^pJ zGI3*aQUZ;esCcPuBc&3yBauPe18^ojD_yT)(&HsMg3;=`!Sl_4g+G+o_8pORL(t_V zz|rp@loVDE(7?KcZqS`JEZAyqrH}`riCOk^n(Y`(3y5y$cve7&j3u^ollvpKRO13t zOFq*nz8TQ)hZ1X!D(#C&It}P_T4Fs5a)hy^m@TckSw>e5072IPCRgItg;?|UCH(g{ Dtli=v literal 0 HcmV?d00001 diff --git a/2024-08-01-anotherfunctioncallingvideo/index.ts b/2024-08-01-anotherfunctioncallingvideo/index.ts new file mode 100644 index 0000000..0a0e8d4 --- /dev/null +++ b/2024-08-01-anotherfunctioncallingvideo/index.ts @@ -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); +} diff --git a/2024-08-01-anotherfunctioncallingvideo/package.json b/2024-08-01-anotherfunctioncallingvideo/package.json new file mode 100644 index 0000000..c945ece --- /dev/null +++ b/2024-08-01-anotherfunctioncallingvideo/package.json @@ -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" + } +} \ No newline at end of file diff --git a/2024-08-01-anotherfunctioncallingvideo/tsconfig.json b/2024-08-01-anotherfunctioncallingvideo/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/2024-08-01-anotherfunctioncallingvideo/tsconfig.json @@ -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 + } +}