tools.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import ollama from 'ollama';
  2. // Simulates an API call to get flight times
  3. // In a real application, this would fetch data from a live database or API
  4. function getFlightTimes(departure: string, arrival: string) {
  5. const flights = {
  6. "NYC-LAX": { departure: "08:00 AM", arrival: "11:30 AM", duration: "5h 30m" },
  7. "LAX-NYC": { departure: "02:00 PM", arrival: "10:30 PM", duration: "5h 30m" },
  8. "LHR-JFK": { departure: "10:00 AM", arrival: "01:00 PM", duration: "8h 00m" },
  9. "JFK-LHR": { departure: "09:00 PM", arrival: "09:00 AM", duration: "7h 00m" },
  10. "CDG-DXB": { departure: "11:00 AM", arrival: "08:00 PM", duration: "6h 00m" },
  11. "DXB-CDG": { departure: "03:00 AM", arrival: "07:30 AM", duration: "7h 30m" }
  12. };
  13. const key = `${departure}-${arrival}`.toUpperCase();
  14. return JSON.stringify(flights[key] || { error: "Flight not found" });
  15. }
  16. async function run(model: string) {
  17. // Initialize conversation with a user query
  18. let messages = [{ role: 'user', content: 'What is the flight time from New York (NYC) to Los Angeles (LAX)?' }];
  19. // First API call: Send the query and function description to the model
  20. const response = await ollama.chat({
  21. model: model,
  22. messages: messages,
  23. tools: [
  24. {
  25. type: 'function',
  26. function: {
  27. name: 'get_flight_times',
  28. description: 'Get the flight times between two cities',
  29. parameters: {
  30. type: 'object',
  31. properties: {
  32. departure: {
  33. type: 'string',
  34. description: 'The departure city (airport code)',
  35. },
  36. arrival: {
  37. type: 'string',
  38. description: 'The arrival city (airport code)',
  39. },
  40. },
  41. required: ['departure', 'arrival'],
  42. },
  43. },
  44. },
  45. ],
  46. })
  47. // Add the model's response to the conversation history
  48. messages.push(response.message);
  49. // Check if the model decided to use the provided function
  50. if (!response.message.tool_calls || response.message.tool_calls.length === 0) {
  51. console.log("The model didn't use the function. Its response was:");
  52. console.log(response.message.content);
  53. return;
  54. }
  55. // Process function calls made by the model
  56. if (response.message.tool_calls) {
  57. const availableFunctions = {
  58. get_flight_times: getFlightTimes,
  59. };
  60. for (const tool of response.message.tool_calls) {
  61. const functionToCall = availableFunctions[tool.function.name];
  62. const functionResponse = functionToCall(
  63. tool.function.arguments.departure,
  64. tool.function.arguments.arrival
  65. );
  66. // Add function response to the conversation
  67. messages.push({
  68. role: 'tool',
  69. content: functionResponse,
  70. });
  71. }
  72. }
  73. // Second API call: Get final response from the model
  74. const finalResponse = await ollama.chat({
  75. model: model,
  76. messages: messages,
  77. });
  78. console.log(finalResponse.message.content);
  79. }
  80. run('mistral').catch(error => console.error("An error occurred:", error));