Finalize GenAI exam: simplify qabilah+q2, add q5 LangChain agent
هذا الالتزام موجود في:
47
q5-llm-agent/agent.py
Normal file
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"])
|
||||
50
q5-llm-agent/llm-agent.md
Normal file
50
q5-llm-agent/llm-agent.md
Normal file
@@ -0,0 +1,50 @@
|
||||
# Q5 — Build a simple LLM agent with LangChain (on Ghaymah's GenAI services)
|
||||
|
||||
⏱ **Budget: ~9 minutes** · Weight: 20%
|
||||
|
||||
## The task
|
||||
|
||||
Build a small **LLM agent** using **LangChain**, and point it at **Ghaymah's GenAI services** (the ready-to-use LLM models you saw in the dashboard).
|
||||
|
||||
Requirements:
|
||||
|
||||
1. Use LangChain (Python) with an **OpenAI-compatible** client pointed at Ghaymah's inference endpoint. Get the **base URL**, **API key**, and a **model ID** from https://deploy.ghaymah.systems (see the AI / models section).
|
||||
2. Give the agent **at least one tool** — e.g. a calculator, a "save note" memory tool, or a tiny retrieval tool.
|
||||
3. (Optional, +bonus) Wire it to the static site from **Q4** — the page calls your agent's endpoint and shows the reply.
|
||||
|
||||
There's a starter `agent.py` + `requirements.txt` in this folder you may extend or replace.
|
||||
|
||||
## Deliverable
|
||||
|
||||
Fill in the template below and commit your code in this folder.
|
||||
|
||||
---
|
||||
|
||||
## ANSWER — LangChain agent
|
||||
|
||||
### 1. What my agent does
|
||||
|
||||
> _one or two lines_
|
||||
|
||||
### 2. Architecture (LangChain components)
|
||||
|
||||
| Component | What I used |
|
||||
|---|---|
|
||||
| Model (LLM) | |
|
||||
| Tool(s) | |
|
||||
| Prompt | |
|
||||
| Executor | |
|
||||
|
||||
### 3. How it uses Ghaymah's GenAI services
|
||||
|
||||
> _base URL pattern, model ID, how you got the key (do not paste the key)_
|
||||
|
||||
### 4. Code
|
||||
|
||||
> _commit your code in this folder (agent.py etc.) and reference it here_
|
||||
|
||||
### 5. How to run / deploy it
|
||||
|
||||
```bash
|
||||
# commands
|
||||
```
|
||||
2
q5-llm-agent/requirements.txt
Normal file
2
q5-llm-agent/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
langchain
|
||||
langchain-openai
|
||||
المرجع في مشكلة جديدة
حظر مستخدم