1
0

Finalize GenAI exam: simplify qabilah+q2, add q5 LangChain agent

هذا الالتزام موجود في:
2026-08-31 06:52:52 +00:00
الأصل 08cb1ef544
التزام 52ac62ab40
7 ملفات معدلة مع 172 إضافات و100 حذوفات

47
q5-llm-agent/agent.py Normal file
عرض الملف

@@ -0,0 +1,47 @@
"""
Q5 starter — a minimal LangChain agent on Ghaymah's GenAI services.
1. Get base_url + API key + model ID from https://deploy.ghaymah.systems
2. pip install -r requirements.txt
3. python agent.py "what is 12 * 7 plus 4?"
"""
import os
import sys
from langchain_openai import ChatOpenAI
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain.tools import tool
from langchain_core.prompts import ChatPromptTemplate
# --- Ghaymah GenAI config (fill these in / use env vars) ---
BASE_URL = os.getenv("GHAYMAH_BASE_URL", "https://<ghaymah-llm-endpoint>/v1")
API_KEY = os.getenv("GHAYMAH_API_KEY", "<redacted>")
MODEL = os.getenv("GHAYMAH_MODEL", "<model-id-from-dashboard>")
llm = ChatOpenAI(base_url=BASE_URL, api_key=API_KEY, model=MODEL, temperature=0)
@tool
def calculator(expression: str) -> str:
"""Evaluate a simple arithmetic expression and return the result."""
try:
return str(eval(expression, {"__builtins__": {}}, {}))
except Exception as e: # noqa: BLE001
return f"error: {e}"
tools = [calculator]
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful Arabic/English assistant. Use the calculator tool when you need arithmetic."),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
]
)
agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
if __name__ == "__main__":
q = sys.argv[1] if len(sys.argv) > 1 else "ما حاصل 12 × 7 + 4؟"
print(executor.invoke({"input": q})["output"])