calculator.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import ollama from 'ollama';
  2. // Add two numbers function
  3. function addTwoNumbers(args: { a: number, b: number }): number {
  4. return args.a + args.b;
  5. }
  6. // Subtract two numbers function
  7. function subtractTwoNumbers(args: { a: number, b: number }): number {
  8. return args.a - args.b;
  9. }
  10. // Tool definition for add function
  11. const addTwoNumbersTool = {
  12. type: 'function',
  13. function: {
  14. name: 'addTwoNumbers',
  15. description: 'Add two numbers together',
  16. parameters: {
  17. type: 'object',
  18. required: ['a', 'b'],
  19. properties: {
  20. a: { type: 'number', description: 'The first number' },
  21. b: { type: 'number', description: 'The second number' }
  22. }
  23. }
  24. }
  25. };
  26. // Tool definition for subtract function
  27. const subtractTwoNumbersTool = {
  28. type: 'function',
  29. function: {
  30. name: 'subtractTwoNumbers',
  31. description: 'Subtract two numbers',
  32. parameters: {
  33. type: 'object',
  34. required: ['a', 'b'],
  35. properties: {
  36. a: { type: 'number', description: 'The first number' },
  37. b: { type: 'number', description: 'The second number' }
  38. }
  39. }
  40. }
  41. };
  42. async function run(model: string) {
  43. const messages = [{ role: 'user', content: 'What is three minus one?' }];
  44. console.log('Prompt:', messages[0].content);
  45. const availableFunctions = {
  46. addTwoNumbers: addTwoNumbers,
  47. subtractTwoNumbers: subtractTwoNumbers
  48. };
  49. const response = await ollama.chat({
  50. model: model,
  51. messages: messages,
  52. tools: [addTwoNumbersTool, subtractTwoNumbersTool]
  53. });
  54. let output: number;
  55. if (response.message.tool_calls) {
  56. // Process tool calls from the response
  57. for (const tool of response.message.tool_calls) {
  58. const functionToCall = availableFunctions[tool.function.name];
  59. if (functionToCall) {
  60. console.log('Calling function:', tool.function.name);
  61. console.log('Arguments:', tool.function.arguments);
  62. output = functionToCall(tool.function.arguments);
  63. console.log('Function output:', output);
  64. // Add the function response to messages for the model to use
  65. messages.push(response.message);
  66. messages.push({
  67. role: 'tool',
  68. content: output.toString(),
  69. });
  70. } else {
  71. console.log('Function', tool.function.name, 'not found');
  72. }
  73. }
  74. // Get final response from model with function outputs
  75. const finalResponse = await ollama.chat({
  76. model: model,
  77. messages: messages
  78. });
  79. console.log('Final response:', finalResponse.message.content);
  80. } else {
  81. console.log('No tool calls returned from model');
  82. }
  83. }
  84. run('llama3.1:8b').catch(error => console.error("An error occurred:", error));