Download 02_tools_agent.py from smlflg/AgentSDK-HelloWorld: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/smlflg/AgentSDK-HelloWorld/resolve/main/02_tools_agent.py
- Command line
-
hf download hf://smlflg/AgentSDK-HelloWorld/02_tools_agent.py
-
curl -L -o 02_tools_agent.py https://huggingface.co/smlflg/AgentSDK-HelloWorld/resolve/main/02_tools_agent.py
1.38 kB
| """ | |
| Agent mit Custom Tools — OpenAI Agents SDK mit MiniMax | |
| Zeigt Function Calling: Agent kann Funktionen aufrufen. | |
| """ | |
| import os | |
| import json | |
| from datetime import datetime | |
| from dotenv import load_dotenv | |
| from openai import AsyncOpenAI | |
| from agents import Agent, Runner, OpenAIChatCompletionsModel, set_tracing_disabled, function_tool | |
| load_dotenv() | |
| set_tracing_disabled(True) | |
| client = AsyncOpenAI( | |
| base_url="http://localhost:11434/v1", | |
| api_key="ollama", | |
| ) | |
| model = OpenAIChatCompletionsModel(model="mistral-nemo", openai_client=client) | |
| def get_current_time() -> str: | |
| """Gibt die aktuelle Uhrzeit und das Datum zurueck.""" | |
| return datetime.now().strftime("%d.%m.%Y, %H:%M Uhr") | |
| def list_files(directory: str) -> str: | |
| """Listet Dateien in einem Verzeichnis auf.""" | |
| try: | |
| files = os.listdir(os.path.expanduser(directory)) | |
| return json.dumps(files[:20], ensure_ascii=False) | |
| except OSError as e: | |
| return f"Fehler: {e}" | |
| agent = Agent( | |
| name="ToolBot", | |
| instructions="Du bist ein hilfreicher Assistent mit Zugriff auf Tools. Nutze sie wenn noetig.", | |
| model=model, | |
| tools=[get_current_time, list_files], | |
| ) | |
| if __name__ == "__main__": | |
| result = Runner.run_sync(agent, "Wie spaet ist es, und welche Dateien liegen in ~/Projekte?") | |
| print(f"\n--- Ergebnis ---\n{result.final_output}") | |