```javascript
import Groq from "groq-sdk";

// Initialize Groq client
const groq = new Groq();
const model = "openai/gpt-oss-20b";

type ToolFunction = (location: string) => string;

// Define weather tools
const getTemperature: ToolFunction = (location: string) => {
  // This is a mock tool/function. In a real scenario, you would call a weather API.
  const temperatures: Record<string, string> = {
    "New York": "22°C",
    "London": "18°C",
    "Tokyo": "26°C",
    "Sydney": "20°C",
  };
  return temperatures[location] || "Temperature data not available";
};

const getWeatherCondition: ToolFunction = (location: string) => {
  // This is a mock tool/function. In a real scenario, you would call a weather API.
  const conditions: Record<string, string> = {
    "New York": "Sunny",
    "London": "Rainy",
    "Tokyo": "Cloudy",
    "Sydney": "Clear",
  };
  return conditions[location] || "Weather condition data not available";
};

// Define system messages and tools
const messages: Groq.Chat.Completions.ChatCompletionMessageParam[] = [
  { role: "system", content: "You are a helpful weather assistant." },
  {
    role: "user",
    content:
      "What's the weather and temperature like in New York and London? Respond with one sentence for each city. Use tools to get the current weather and temperature.",
  },
];

const tools: Groq.Chat.Completions.ChatCompletionTool[] = [
  {
    type: "function",
    function: {
      name: "getTemperature",
      description: "Get the temperature for a given location",
      parameters: {
        type: "object",
        properties: {
          location: {
            type: "string",
            description: "The name of the city",
          },
        },
        required: ["location"],
      },
    },
  },
  {
    type: "function",
    function: {
      name: "getWeatherCondition",
      description: "Get the weather condition for a given location",
      parameters: {
        type: "object",
        properties: {
          location: {
            type: "string",
            description: "The name of the city",
          },
        },
        required: ["location"],
      },
    },
  },
];

// Make the initial request
export async function runWeatherAssistant() {
  try {
    const response = await groq.chat.completions.create({
      model,
      messages,
      tools,
      temperature: 0.5, // Keep temperature between 0.0 - 0.5 for best tool calling results
      tool_choice: "auto",
      max_completion_tokens: 4096,
    });

    const responseMessage = response.choices[0].message;
    console.log("Response message:", JSON.stringify(responseMessage, null, 2));

    const toolCalls = responseMessage.tool_calls || [];

    // Process tool calls
    messages.push(responseMessage);

    const availableFunctions: Record<string, ToolFunction> = {
      getTemperature,
      getWeatherCondition,
    };

    for (const toolCall of toolCalls) {
      const functionName = toolCall.function.name;
      const functionToCall = availableFunctions[functionName];
      const functionArgs = JSON.parse(toolCall.function.arguments);
      // Call corresponding tool function if it exists
      const functionResponse = functionToCall?.(functionArgs.location);

      if (functionResponse) {
        messages.push({
          role: "tool",
          content: functionResponse,
          tool_calls_id: toolCall.id,
        });
      }
    }

    // Make the final request with tool call results
    const finalResponse = await groq.chat.completions.create({
      model,
      messages,
      tools,
      temperature: 0.5,
      tool_choice: "auto",
      max_completion_tokens: 4096,
    });

    return finalResponse.choices[0].message.content;
  } catch (error) {
    console.error("An error occurred:", error);
    throw error; // Re-throw the error so it can be caught by the caller
  }
}

runWeatherAssistant()
  .then((result) => {
    console.log("Final result:", result);
  })
  .catch((error) => {
    console.error("Error in main execution:", error);
  });
```

---

## Tool Use: Routing (js)