flight-tracker.ts 3.5 KB

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