| from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool |
| import datetime |
| import requests |
| import pytz |
| import yaml |
| from tools.final_answer import FinalAnswerTool |
|
|
| from Gradio_UI import GradioUI |
|
|
| from typing import List, Dict |
|
|
| def generate_test_cases(topic: str) -> List[Dict[str, str]]: |
| """ |
| Suggest simple QA test cases for a given topic. |
| Args: |
| topic: The feature, function, or topic to generate test cases for. |
| Returns: |
| A list of test cases, each with input and expected output. |
| """ |
| topic_lower = topic.lower() |
| test_cases = [] |
|
|
| if "login" in topic_lower: |
| test_cases = [ |
| {"test_case": "Valid login", "input": "Correct username/password", "expected_output": "User is logged in successfully"}, |
| {"test_case": "Invalid password", "input": "Wrong password", "expected_output": "Error message displayed"}, |
| {"test_case": "Empty username", "input": "Username left blank", "expected_output": "Error message displayed"}, |
| {"test_case": "Empty password", "input": "Password left blank", "expected_output": "Error message displayed"}, |
| ] |
| elif "calculator" in topic_lower: |
| test_cases = [ |
| {"test_case": "Addition", "input": "2 + 3", "expected_output": "5"}, |
| {"test_case": "Subtraction", "input": "5 - 2", "expected_output": "3"}, |
| {"test_case": "Multiplication", "input": "4 * 5", "expected_output": "20"}, |
| {"test_case": "Divide by zero", "input": "5 / 0", "expected_output": "Error handled gracefully"}, |
| ] |
| else: |
| |
| test_cases = [ |
| {"test_case": f"Test case 1 for {topic}", "input": "Sample input", "expected_output": "Expected behavior"}, |
| {"test_case": f"Test case 2 for {topic}", "input": "Another input", "expected_output": "Expected behavior"}, |
| {"test_case": f"Test case 3 for {topic}", "input": "Additional input", "expected_output": "Expected behavior"}, |
| ] |
|
|
| return test_cases |
| |
| |
| @tool |
| def my_custom_tool(arg1:str, arg2:int)-> str: |
| |
| """A tool that does nothing yet |
| Args: |
| arg1: the first argument |
| arg2: the second argument |
| """ |
| return "What magic will you build ?" |
|
|
| @tool |
| def suggest_test_cases(topic: str) -> list: |
| """ |
| Suggest simple QA test cases for a given topic. |
| |
| Args: |
| topic: Feature or topic to generate test cases for. |
| |
| Returns: |
| A list of test cases with input and expected output. |
| """ |
| return generate_test_cases(topic) |
|
|
| @tool |
| def time_difference(ts1: str, tz1: str, ts2: str, tz2: str, signed: bool = False) -> str: |
| """ |
| Calculate the duration between two timestamps in different timezones. |
| Args: |
| ts1: First timestamp string in 'YYYY-MM-DD HH:MM:SS' format. |
| tz1: Timezone of ts1 (e.g. 'UTC', 'America/New_York'). |
| ts2: Second timestamp string in 'YYYY-MM-DD HH:MM:SS' format. |
| tz2: Timezone of ts2. |
| signed: If True, return negative values when ts2 is earlier than ts1. |
| If False, return absolute difference. |
| Returns: |
| A formatted string showing the difference in days, hours, minutes, and seconds. |
| """ |
| try: |
| |
| fmt = "%Y-%m-%d %H:%M:%S" |
| dt1_naive = datetime.datetime.strptime(ts1, fmt) |
| dt2_naive = datetime.datetime.strptime(ts2, fmt) |
|
|
| |
| dt1 = pytz.timezone(tz1).localize(dt1_naive) |
| dt2 = pytz.timezone(tz2).localize(dt2_naive) |
|
|
| |
| dt1_utc = dt1.astimezone(pytz.UTC) |
| dt2_utc = dt2.astimezone(pytz.UTC) |
|
|
| |
| diff = dt2_utc - dt1_utc |
|
|
| if not signed: |
| diff = abs(diff) |
|
|
| |
| total_seconds = int(diff.total_seconds()) |
| days = total_seconds // 86400 |
| hours = (total_seconds % 86400) // 3600 |
| minutes = (total_seconds % 3600) // 60 |
| seconds = total_seconds % 60 |
|
|
| result = f"Difference: {days}d {hours}h {minutes}m {seconds}s (total {total_seconds} seconds)" |
| return result |
|
|
| except Exception as e: |
| return f"Error calculating time difference: {str(e)}" |
| |
| @tool |
| def get_current_time_in_timezone(timezone: str) -> str: |
| """A tool that fetches the current local time in a specified timezone. |
| Args: |
| timezone: A string representing a valid timezone (e.g., 'America/New_York'). |
| """ |
| try: |
| |
| tz = pytz.timezone(timezone) |
| |
| local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") |
| return f"The current local time in {timezone} is: {local_time}" |
| except Exception as e: |
| return f"Error fetching time for timezone '{timezone}': {str(e)}" |
|
|
|
|
| final_answer = FinalAnswerTool() |
|
|
| |
| |
|
|
| model = HfApiModel( |
| max_tokens=2096, |
| temperature=0.5, |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', |
| custom_role_conversions=None, |
| ) |
|
|
|
|
| |
| image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) |
|
|
| with open("prompts.yaml", 'r') as stream: |
| prompt_templates = yaml.safe_load(stream) |
| |
| agent = CodeAgent( |
| model=model, |
| tools=[final_answer, suggest_test_cases, time_difference], |
| max_steps=6, |
| verbosity_level=1, |
| grammar=None, |
| planning_interval=None, |
| name=None, |
| description=None, |
| prompt_templates=prompt_templates |
| ) |
|
|
|
|
| GradioUI(agent).launch() |