add fcn calling with tools

Signed-off-by: Matt Williams <m@technovangelist.com>
This commit is contained in:
Matt Williams 2024-07-10 16:22:48 -07:00
parent 8cd36f5769
commit 318eb54b32
7 changed files with 435 additions and 0 deletions

View 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

View file

@ -0,0 +1,17 @@
# functioncalling with Tools
This is an example of how function calling works. And we specifically look at using multiple tools from Ollama.
To install dependencies:
```bash
bun install
```
To run:
```bash
bun run index.ts
```

Binary file not shown.

View file

@ -0,0 +1,25 @@
import ollama from "ollama";
import {toolsString, executeFunction} from "./tools";
const promptandanswer = async (prompt: string) => {
const response = await ollama.generate({
model: "llama3",
system: systemPrompt,
prompt: prompt,
stream: false,
format: "json",
});
console.log(`\n${prompt}\n`);
// console.log(response.response.trim());
const responseObject = JSON.parse(response.response.trim());
executeFunction(responseObject.functionName, responseObject.parameters);
};
const systemPrompt = `You are a helpful assistant that takes a question and finds the most appropriate tool or tools to execute, along with the parameters required to run the tool. Respond as JSON using the following schema: {"functionName": "function name", "parameters": [{"parameterName": "name of parameter", "parameterValue": "value of parameter"}]}. The tools are: ${toolsString}`;
await promptandanswer("What is the weather in London?");
await promptandanswer("What is the weather at 41.881832, -87.640406?");
await promptandanswer("who is the current ceo of tesla?");
await promptandanswer("what is located at 41.881832, -87.640406?");

View file

@ -0,0 +1,14 @@
{
"name": "functioncalling",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"ollama": "^0.5.2"
}
}

View file

@ -0,0 +1,177 @@
export type Tool = {
name: string;
description: string;
parameters: ToolParameter[];
};
type ToolParameter = {
name: string;
description: string;
type: string;
required: boolean;
};
type FunctionParameter = {
parameterName: string;
parameterValue: string;
};
const cityToLatLonTool: Tool = {
name: "CityToLatLon",
description: "Get the latitude and longitude for a given city",
parameters: [
{
name: "city",
description: "The city to get the latitude and longitude for",
type: "string",
required: true,
},
],
};
const weatherFromLatLonTool: Tool = {
name: "WeatherFromLatLon",
description: "Get the weather for a location",
parameters: [
{
name: "latitude",
description: "The latitude of the location",
type: "number",
required: true,
},
{
name: "longitude",
description: "The longitude of the location",
type: "number",
required: true,
},
],
};
const latlonToCityTool: Tool = {
name: "LatLonToCity",
description: "Get the city name for a given latitude and longitude",
parameters: [
{
name: "latitude",
description: "The latitude of the location",
type: "number",
required: true,
},
{
name: "longitude",
description: "The longitude of the location",
type: "number",
required: true,
},
],
};
const webSearchTool: Tool = {
name: "WebSearch",
description: "Search the web for a query",
parameters: [
{
name: "query",
description: "The query to search for",
type: "string",
required: true,
},
],
};
const weatherFromLocationTool: Tool = {
name: "WeatherFromLocation",
description: "Get the weather for a location",
parameters: [
{
name: "location",
description: "The location to get the weather for",
type: "string",
required: true,
},
],
};
async function CityToLatLon(city: string) {
const output = await fetch(
`https://nominatim.openstreetmap.org/search?q=${city}&format=json`,
);
const json = await output.json();
return [json[0].lat, json[0].lon];
}
async function LatLonToCity(latitude: string, longitude: string) {
const output = await fetch(
`https://nominatim.openstreetmap.org/reverse?lat=${latitude}&lon=${longitude}&format=json`,
);
const json = await output.json();
console.log(json.display_name);
}
async function WeatherFromLatLon(latitude: string, longitude: string) {
const output = await fetch(
`https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&current=temperature_2m&temperature_unit=fahrenheit&wind_speed_unit=mph&forecast_days=1`,
);
const json = await output.json();
console.log(`${json.current.temperature_2m} degrees Farenheit`);
}
async function WeatherFromLocation(location: string) {
const latlon = await CityToLatLon(location);
await WeatherFromLatLon(latlon[0], latlon[1]);
}
async function WebSearch(query: string) {
const output = await fetch(
`http://localhost:3333/search?q=${query}&format=json`,
);
const json = await output.json();
console.log(`${json.results[0].title}\n${json.results[0].content}\n`);
}
export const toolsString = JSON.stringify(
{
tools: [
weatherFromLocationTool,
weatherFromLatLonTool,
webSearchTool,
latlonToCityTool,
],
},
null,
2,
);
function getValueOfParameter(
parameterName: string,
parameters: FunctionParameter[],
) {
return parameters.filter((p) => p.parameterName === parameterName)[0]
.parameterValue;
}
export async function executeFunction(
functionName: string,
parameters: FunctionParameter[],
) {
switch (functionName) {
case "WeatherFromLocation":
return await WeatherFromLocation(getValueOfParameter("location", parameters));
case "WeatherFromLatLon":
return await WeatherFromLatLon(
getValueOfParameter("latitude", parameters),
getValueOfParameter("longitude", parameters),
);
case "WebSearch":
return await WebSearch(getValueOfParameter("query", parameters));
case "LatLonToCity":
return await LatLonToCity(
getValueOfParameter("latitude", parameters),
getValueOfParameter("longitude", parameters),
);
}
}

View 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
}
}