| from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, VisitWebpageTool, PythonInterpreterTool, LiteLLMModel, tool |
| from tools.wiki_tool import WikiTool |
| from tools.excel_analysis_tool import ExcelAnalysisTool |
| import os |
|
|
| @tool |
| def check_answer(answer: str) -> str: |
| """ |
| Reviews the answer to check that it meets the requirements specified by the user and modifies it if necessary. |
| Args: |
| answer (str): The answer fo the Agent. |
| Returns: |
| str: The final answer. |
| """ |
| return answer |
|
|
| @tool |
| def reverse_sentence_tool(reverse_sentence: str) -> str: |
| """ |
| This tool receives a sentence where both the word order and the characters in each word are reversed. |
| It returns the sentence with words and order corrected. |
| Args: |
| reverse_sentence: A sentence with reversed words and reversed word order. |
| Returns: |
| A sentence in natural reading order. |
| """ |
| inverted_words = reverse_sentence.split(" ")[::-1] |
| correct_words = [word[::-1] for word in inverted_words] |
|
|
| return " ".join(correct_words) |
|
|
| def create_agent(): |
| """ |
| Creates and configures the CodeAgent with the necessary model and tools. |
| |
| Returns: |
| CodeAgent: The configured agent instance. |
| """ |
| |
| token = os.getenv("OPENAI_API_KEY") |
| if not token: |
| raise RuntimeError("Missing Hugging Face API key. Set HF_API_KEY environment variable.") |
|
|
| |
| |
| |
| model = LiteLLMModel( |
| model_id="gemini/gemini-2.0-flash", |
| api_base="https://generativelanguage.googleapis.com/v1beta/models", |
| api_key=os.getenv("GEMINI_API_KEY") |
| ) |
|
|
| |
| wiki_tool = WikiTool() |
| web_search_tool = DuckDuckGoSearchTool() |
| excel_analysis_tool = ExcelAnalysisTool() |
| visitWebpageTool = VisitWebpageTool() |
| python_interpreter_tool = PythonInterpreterTool() |
|
|
| |
| agent = CodeAgent( |
| model=model, |
| tools=[wiki_tool, web_search_tool, excel_analysis_tool, visitWebpageTool, python_interpreter_tool, check_answer, reverse_sentence_tool], |
| max_steps=8, |
| verbosity_level=2 |
| ) |
| return agent |
|
|
| |
| if __name__ == "__main__": |
| agent = create_agent() |
| print("Agent created with tools:", [tool.name for tool in agent.tools]) |
|
|