| # # from langchain.agents import create_tool_calling_agent, AgentExecutor | |
| # # from langchain.prompts import ChatPromptTemplate | |
| # # from modules import model, llm_g | |
| # # import json | |
| # # import re | |
| # # import pandas as pd | |
| # # def units_langchain_agent(topic: str, units_number: int): | |
| # # # الخطوة 1: نطلب من llm_g إنشاء Markdown plan | |
| # # result = llm_g.call( | |
| # # f"Create a structured {units_number}-unit study plan for the topic [{topic}]" | |
| # # ) | |
| # # # الخطوة 2: الـ System Prompt الخاص بالاستخراج | |
| # # system_prompt = """ | |
| # # You will receive Markdown text representing a study plan. | |
| # # Extract structured data and return it strictly in this JSON format: | |
| # # {{ | |
| # # "topic": "<topic name>", | |
| # # "General_objectives_of_the_plan": ["string", "string", ...], | |
| # # "total_units": <total_units_number>, | |
| # # "units": [ | |
| # # {{ | |
| # # "unit_number": 1, | |
| # # "unit_title": "string", | |
| # # "description": "string", | |
| # # "unit_objectives": ["string", "string", ...], | |
| # # "unit_main_topics": ["string", "string", ...] | |
| # # }}, | |
| # # ... | |
| # # ] | |
| # # }} | |
| # # Output **only valid JSON**, no explanations, markdown, or extra text. | |
| # # """ | |
| # # # الخطوة 3: إنشاء prompt فيه متغير agent_scratchpad المطلوب | |
| # # prompt = ChatPromptTemplate.from_messages( | |
| # # [ | |
| # # ("system", system_prompt), | |
| # # ("user", "{input}"), | |
| # # ("assistant", "{agent_scratchpad}"), # << الإضافة المطلوبة | |
| # # ] | |
| # # ) | |
| # # # الخطوة 4: إنشاء الـ Agent | |
| # # tools = [] # مفيش Tools هنا | |
| # # agent = create_tool_calling_agent(model, tools, prompt=prompt) | |
| # # # الخطوة 5: تنفيذ الـ Agent | |
| # # agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True) | |
| # # response = agent_executor.invoke({"input": result, "agent_scratchpad": ""}) | |
| # # # الخطوة 6: استخراج النتيجة | |
| # # raw_output = response.get("output", "").strip() | |
| # # # الخطوة 7: تنظيف وتحويل JSON | |
| # # try: | |
| # # structured_data = json.loads(raw_output) | |
| # # except json.JSONDecodeError: | |
| # # cleaned = re.sub(r"```json|```", "", raw_output).strip() | |
| # # structured_data = json.loads(cleaned) | |
| # # return structured_data | |
| # from modules import model, llm_g | |
| # import json | |
| # import re | |
| # from langchain.agents import initialize_agent, AgentType | |
| # # from langchain.agents import create_agent | |
| # def units_langchain1_agent(topic: str, units_number: int): | |
| # llm = llm_g() | |
| # # الخطوة 1: نطلب من llm_g إنشاء Markdown plan | |
| # result = llm.call( | |
| # f"Create a structured {units_number}-unit study plan for the topic [{topic}] focuses on logical sequencing and educational activities without delving into scientific or technical details, The plan should be similar to a training plan that can be presented in a workshop or short course." | |
| # ) | |
| # # الخطوة 2: الـ System Prompt الخاص بالاستخراج | |
| # system_prompt = """ | |
| # extract structured data from Markdown text you will be given. | |
| # The JSON should have this exact structure: | |
| # { | |
| # "topic": "<topic name>", | |
| # "General_objectives_of_the_plan": ["string", "string", ...] | |
| # "total_units": <total_units_number>, | |
| # "units": [ | |
| # { | |
| # "unit_number": 1, | |
| # "unit_title": "string", | |
| # "description": "string", | |
| # "unit_objectives": ["string", "string", ...], | |
| # "unit_main_topics": ["string", "string", ...] | |
| # }, | |
| # ... | |
| # ] | |
| # } | |
| # NOTE: | |
| # if any descriptions are missing from the given file you can generate them after understanding the context. | |
| # Output **only valid JSON**, no explanations, markdown, or extra text. | |
| # """ | |
| # # agent = create_agent( | |
| # # model=model, | |
| # # system_prompt=system_prompt, | |
| # # ) | |
| # model_open = model() | |
| # agent = initialize_agent( | |
| # system_prompt=system_prompt, | |
| # llm=model_open, | |
| # agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, | |
| # verbose=True, | |
| # ) | |
| # response = agent.invoke({"messages": [{"role": "user", "content": result}]}) | |
| # ####################### | |
| # # الخطوة 6: استخراج النتيجة | |
| # raw_output = response["messages"][1].content.strip() | |
| # # الخطوة 7: تنظيف وتحويل JSON | |
| # try: | |
| # structured_data = json.loads(raw_output) | |
| # except json.JSONDecodeError: | |
| # cleaned = re.sub(r"```json|```", "", raw_output).strip() | |
| # structured_data = json.loads(cleaned) | |
| # return structured_data | |