L♾️pGPT
إطار عمل GPT معياري
L♾️pGPT هو إعادة تنفيذ لمشروع Auto-GPT الشهير كحزمة بايثون مناسبة، تمت كتابته مع وضع النمطية وقابلية التوسعة في الاعتبار.
؟ يؤدي هذا إلى تثبيت أحدث إصدار ثابت من L♾️pGPT. يوصى بهذا لمعظم المستخدمين:
pip install loopgpt
الطريقتان أدناه لتثبيت أحدث إصدار تطوير من L♾️pGPT. لاحظ أن هذا الإصدار ربما غير مستقر:
pip install git+https://www.github.com/farizrahman4u/loopgpt.git@main
git clone https://www.github.com/farizrahman4u/loopgpt.git
cd loopgpt
pip install -e .
git clone https://www.github.com/farizrahman4u/loopgpt.git
cd loopgpt
docker build -t loopgpt:local-dev .
.env
قم بإنشاء ملف .env
في دليل العمل الحالي لديك (من المكان الذي ستقوم بتشغيل L♾️pGPT منه) وأضف السطر التالي إليه:
OPENAI_API_KEY= " <your-openai-api-key> "
؟ مهم ؟
بالنسبة لمستخدمي Windows، يرجى التأكد من تمكين "إظهار امتدادات الملفات" في مستكشف الملفات لديك. بخلاف ذلك، سيتم تسمية ملفك بـ .env.txt
بدلاً من .env
.
قم بتعيين متغير بيئة يسمى OPENAI_API_KEY
إلى مفتاح OpenAI API الخاص بك.
كيفية ضبط متغيرات البيئة:
لنقم بإنشاء وكيل في برنامج Python النصي الجديد.
from loopgpt . agent import Agent
agent = Agent ()
يستخدم L♾️pGPT gpt-3.5-turbo
افتراضيًا وجميع المخرجات الموضحة هنا مصنوعة باستخدامه. يمكن لمستخدمي GPT-4 تعيين model="gpt-4"
بدلاً من ذلك:
agent = Agent ( model = "gpt-4" )
agent . name = "ResearchGPT"
agent . description = "an AI assistant that researches and finds the best tech products"
agent . goals = [
"Search for the best headphones on Google" ,
"Analyze specs, prices and reviews to find the top 5 best headphones" ,
"Write the list of the top 5 best headphones and their prices to a file" ,
"Summarize the pros and cons of each headphone and write it to a different file called 'summary.txt'" ,
]
ونحن خارج! لنقم بتشغيل واجهة سطر الأوامر الخاصة بالوكيل:
agent . cli ()
احفظ ملف Python الخاص بك باسم research_gpt.py
وقم بتشغيله:
python research_gpt.py
يمكنك الخروج من واجهة سطر الأوامر (CLI) عن طريق كتابة "exit".
إذا تم تعيين continuous
على True
، فلن يطلب الوكيل إذن المستخدم لتنفيذ الأوامر. قد يدخل في حلقات لا نهائية، لذا استخدمه على مسؤوليتك الخاصة!
agent . cli ( continuous = True )
يمكنك تشغيل L♾️pGPT مباشرة من سطر الأوامر دون الحاجة إلى كتابة أي كود بايثون أيضًا:
loopgpt run
قم بتشغيل loopgpt --help
لرؤية جميع الخيارات المتاحة.
يمكنك تشغيل L♾️pGPT في الأوضاع المذكورة سابقًا باستخدام Docker:
# CLI mode
docker run -i --rm loopgpt:local-dev loopgpt run
# Script mode example
docker run -i --rm -v " $( pwd ) /scripts:/scripts " loopgpt:local-dev python /scripts/myscript.py
يأتي وكلاء L♾️pGPT مع مجموعة من الأدوات المضمنة التي تتيح لهم أداء العديد من المهام الأساسية مثل البحث في الويب وعمليات نظام الملفات وما إلى ذلك. يمكنك عرض هذه الأدوات باستخدام print(agent.tools)
.
بالإضافة إلى هذه الأدوات المدمجة، يمكنك أيضًا إضافة أدواتك الخاصة إلى صندوق أدوات الوكيل.
لنقم بإنشاء WeatherGPT، وهو مساعد الذكاء الاصطناعي لكل ما يتعلق بالطقس.
ترث الأداة من BaseTool
وتحتاج فقط إلى كتابة سلسلة مستندية لتشغيل الأداة الخاصة بك!
from loopgpt . tools import BaseTool
class GetWeather ( BaseTool ):
"""Quickly get the weather for a given city
Args:
city (str): name of the city
Returns:
dict: The weather report for the city
"""
def run ( self , city ):
...
يوفر L♾️pGPT معرفًا افتراضيًا لأداتك ولكن يمكنك تجاوزه إذا كنت ترغب في ذلك:
class GetWeather ( BaseTool ):
"""Quickly get the weather for a given city
Args:
city (str): name of the city
Returns:
dict: The weather report for the city
"""
@ property
def id ( self ):
return "get_weather_command"
الآن دعونا نحدد ما ستفعله أداتنا في طريقة run
الخاصة بها:
import requests
# Define your custom tool
class GetWeather ( BaseTool ):
"""Quickly get the weather for a given city
Args:
city (str): name of the city
Returns:
dict: The weather report for the city
"""
def run ( self , city ):
try :
url = "https://wttr.in/{}?format=%l+%C+%h+%t+%w+%p+%P" . format ( city )
data = requests . get ( url ). text . split ( " " )
keys = ( "location" , "condition" , "humidity" , "temperature" , "wind" , "precipitation" , "pressure" )
data = dict ( zip ( keys , data ))
return data
except Exception as e :
return f"An error occurred while getting the weather: { e } ."
هذا كل شيء! لقد قمت ببناء أول أداة مخصصة لك. دعنا نسجله مع وكيل جديد ونقوم بتشغيله:
from loopgpt . tools import WriteToFile
import loopgpt
# Register custom tool type
# This is actually not required here, but is required when you load a saved agent with custom tools.
loopgpt . tools . register_tool_type ( GetWeather )
# Create Agent
agent = loopgpt . Agent ( tools = [ GetWeather , WriteToFile ])
agent . name = "WeatherGPT"
agent . description = "an AI assistant that tells you the weather"
agent . goals = [
"Get the weather for NewYork and Beijing" ,
"Give the user tips on how to dress for the weather in NewYork and Beijing" ,
"Write the tips to a file called 'dressing_tips.txt'"
]
# Run the agent's CLI
agent . cli ()
دعونا نلقي نظرة على ملف dressing_tips.txt
الذي كتبه لنا WeatherGPT:
Dressing_tips.txt
- It's Clear outside with a temperature of +10°C in Beijing. Wearing a light jacket and pants is recommended.
- It's Overcast outside with a temperature of +11°C in New York. Wearing a light jacket, pants, and an umbrella is recommended.
على عكس Auto-GPT، لا يتم إنهاء الوكيل عندما يرفض المستخدم تنفيذ الأمر. وبدلاً من ذلك يطلب من المستخدم تقديم تعليقات لتصحيح مساره.
لتصحيح مسار الوكيل، ما عليك سوى رفض التنفيذ وتقديم الملاحظات:
قام الوكيل بتحديث مسار عمله:
يمكنك حفظ حالة الوكيل في ملف json باستخدام:
agent . save ( "ResearchGPT.json" )
يؤدي هذا إلى حفظ تكوين الوكيل (النموذج والاسم والوصف وما إلى ذلك) بالإضافة إلى حالته الداخلية (حالة المحادثة والذاكرة وحالات الأداة وما إلى ذلك). يمكنك أيضًا حفظ التكوين فقط عن طريق تمرير include_state=False
إلى agent.save()
:
agent . save ( "ResearchGPT.json" , include_state = False )
ثم تابع من حيث توقفت بـ:
import loopgpt
agent = loopgpt . Agent . load ( "ResearchGPT.json" )
agent . cli ()
أو عن طريق تشغيل الوكيل المحفوظ من سطر الأوامر:
loopgpt run ResearchGPT.json
يمكنك تحويل حالة الوكيل إلى قاموس python متوافق مع json بدلاً من الكتابة إلى ملف:
agent_config = agent . config ()
للحصول على التكوين فقط بدون الحالة الداخلية:
agent_config = agent . config ( include_state = False )
لإعادة تحميل الوكيل من التكوين، استخدم:
import loopgpt
agent = loopgpt . Agent . from_config ( agent_config )
للحصول على الدعم الرسمي لبحث Google، ستحتاج إلى إعداد مفتاحين متغيرين للبيئة GOOGLE_API_KEY
و CUSTOM_SEARCH_ENGINE_ID
، وإليك كيفية الحصول عليهما:
CUSTOM_SEARCH_ENGINE_ID
.GOOGLE_API_KEY
.في حالة غياب هذه العناصر، سيعود L♾️pGPT إلى استخدام بحث DuckDuckGo.
نحن بحاجة إلى الكثير من المساعدة! يرجى فتح قضية أو العلاقات العامة إذا كنت ترغب في المساهمة.
بحاجة الى مساعدة؟ انضم إلى خلافنا.