|
| 1 | +# SPDX-FileCopyrightText: Copyright (C) ARDUINO SRL (http://www.arduino.cc) |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +# EXAMPLE_NAME = "Chat with tools and streaming" |
| 6 | +# EXAMPLE_REQUIRES = "Models must be downloaded and available locally." |
| 7 | + |
| 8 | +from arduino.app_bricks.llm import LargeLanguageModel, tool |
| 9 | +from arduino.app_utils import App |
| 10 | + |
| 11 | + |
| 12 | +# Tool definition - simulates a simple weather API - please replace with actual API calls in a real application |
| 13 | +@tool |
| 14 | +def get_current_weather(location: str) -> str: |
| 15 | + """ |
| 16 | + Get the current weather in a given location. |
| 17 | + The output is a string with a summary of the weather. |
| 18 | +
|
| 19 | + Args: |
| 20 | + location (str): The location to get the weather for. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + str: A summary of the current weather in the specified location. |
| 24 | +
|
| 25 | + """ |
| 26 | + if "boston" in location.lower(): |
| 27 | + return "The current weather in Boston is 15°C and partly cloudy." |
| 28 | + elif "paris" in location.lower(): |
| 29 | + return "The current weather in Paris is 8°C and rainy." |
| 30 | + elif "turin" in location.lower(): |
| 31 | + return "The current weather in Turin is 8°C and rainy." |
| 32 | + else: |
| 33 | + return f"Sorry, I do not have real-time weather data for {location}. Assuming it's a sunny day!" |
| 34 | + |
| 35 | + |
| 36 | +llm = LargeLanguageModel(max_tokens=512, tools=[get_current_weather]) |
| 37 | + |
| 38 | + |
| 39 | +def ask_prompt(): |
| 40 | + prompt = input("Enter your prompt (or type 'exit' to quit): ") |
| 41 | + if prompt.lower() == "exit": |
| 42 | + raise StopIteration() |
| 43 | + for chunk in llm.chat_stream(prompt): |
| 44 | + print(chunk, end="", flush=True) |
| 45 | + print() |
| 46 | + |
| 47 | + |
| 48 | +App.run(ask_prompt) |
0 commit comments