Created
June 8, 2025 22:45
-
-
Save al-swaiti/c44c64c607f4a988b68fa37152068f98 to your computer and use it in GitHub Desktop.
forloop.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# --- الخطوة 1: تعريف الأدوات الوهمية (Dummy Tools) --- | |
# في تطبيق حقيقي، قد تتصل هذه الدوال بواجهات برمجية (APIs) حقيقية | |
def get_weather(text_input): | |
"""أداة وهمية لمعرفة الطقس في مدينة ما""" | |
# منطق بسيط جدًا لاستخراج اسم المدينة (للتوضيح فقط) | |
if "in" in text_input: | |
city = text_input.split("in")[-1].strip() | |
return f"The weather in {city} is sunny and pleasant." | |
return "Please specify a city, for example: 'weather in Riyadh'." | |
def analyze_sentiment(text_input): | |
"""أداة وهمية لتحليل المشاعر""" | |
if "happy" in text_input or "love" in text_input: | |
return "The sentiment appears to be POSITIVE." | |
elif "sad" in text_input or "hate" in text_input: | |
return "The sentiment appears to be NEGATIVE." | |
return "Could not determine the sentiment." | |
def tell_joke(text_input): | |
"""أداة وهمية لإلقاء نكتة""" | |
return "Why don't scientists trust atoms? Because they make up everything!" | |
# --- الخطوة 2: تسجيل الأدوات والكلمات المفتاحية الخاصة بها --- | |
# هذا هو "صندوق أدوات" العميل | |
tools = { | |
"weather_tool": { | |
"function": get_weather, | |
"keywords": ["weather", "temperature", "forecast"], | |
"description": "Use to get the weather forecast for a city." | |
}, | |
"sentiment_tool": { | |
"function": analyze_sentiment, | |
"keywords": ["sentiment", "feeling", "feel", "opinion"], | |
"description": "Use to analyze the sentiment of a text." | |
}, | |
"joke_tool": { | |
"function": tell_joke, | |
"keywords": ["joke", "funny", "laugh"], | |
"description": "Use to tell a joke." | |
} | |
} | |
# --- الخطوة 3: بناء العميل الصغير (Tiny Agent) --- | |
class TinyAgent: | |
def __init__(self, tool_dict): | |
self.tools = tool_dict | |
def run(self): | |
"""هذه هي دالة التشغيل الرئيسية التي تحتوي على حلقة التفكير""" | |
print("Hello! I am a Tiny Agent. How can I help you? (Type 'exit' to quit)") | |
while True: | |
user_prompt = input("You: ") | |
if user_prompt.lower() in ["exit", "quit"]: | |
print("Agent: Goodbye!") | |
break | |
tool_found = False | |
# <<< هنا تكمن الفكرة الأساسية للميم: "مجرد حلقة for" >>> | |
# العميل يدور على كل أداة متاحة لديه ويقرر هل يستخدمها أم لا | |
for tool_name, tool_info in self.tools.items(): | |
# تحويل الطلب إلى حروف صغيرة لتسهيل المقارنة | |
prompt_lower = user_prompt.lower() | |
# التحقق إذا كانت إحدى الكلمات المفتاحية موجودة في طلب المستخدم | |
if any(keyword in prompt_lower for keyword in tool_info['keywords']): | |
print(f"Agent: Found a suitable tool: '{tool_name}'") | |
# استدعاء الدالة الخاصة بالأداة | |
result = tool_info['function'](prompt_lower) | |
print(f"Agent: {result}") | |
tool_found = True | |
break # الخروج من الحلقة لأنه وجد أداة مناسبة | |
if not tool_found: | |
print("Agent: I'm sorry, I don't have a tool for that.") | |
print("-" * 20) | |
# --- الخطوة 4: تشغيل العميل --- | |
if __name__ == "__main__": | |
agent = TinyAgent(tools) | |
agent.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment