Spaces:
Sleeping
Sleeping
Add ChatOpenRouter class and integrate system prompt loading in BasicAgent
Browse files
agent.py
CHANGED
|
@@ -3,7 +3,8 @@ from typing import Optional
|
|
| 3 |
|
| 4 |
from langgraph.graph import StateGraph, START, END, MessagesState
|
| 5 |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 6 |
-
from langgraph.prebuilt import ToolNode
|
|
|
|
| 7 |
|
| 8 |
# Try to import tools from tools.py
|
| 9 |
try:
|
|
@@ -21,6 +22,29 @@ except Exception: # pragma: no cover - optional dependency resolution
|
|
| 21 |
ChatOpenAI = None # type: ignore
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
class _EchoModel:
|
| 25 |
"""Simple stub model used when no API key / model is configured."""
|
| 26 |
|
|
@@ -56,14 +80,15 @@ class LangGraphAgent:
|
|
| 56 |
|
| 57 |
# Choose an LLM if not provided
|
| 58 |
if model is None:
|
| 59 |
-
|
|
|
|
| 60 |
model = ChatOpenAI(
|
| 61 |
api_key=os.getenv("OPENROUTER_API_KEY"),
|
| 62 |
base_url=os.getenv("OPENROUTER_BASE_URL"),
|
| 63 |
model="openai/gpt-oss-20b:free",
|
| 64 |
)
|
| 65 |
-
|
| 66 |
-
|
| 67 |
self.model = model
|
| 68 |
|
| 69 |
# Load tools and bind to the model if supported
|
|
|
|
| 3 |
|
| 4 |
from langgraph.graph import StateGraph, START, END, MessagesState
|
| 5 |
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
|
| 6 |
+
from langgraph.prebuilt import ToolNode
|
| 7 |
+
from pydantic import Field, SecretStr # add
|
| 8 |
|
| 9 |
# Try to import tools from tools.py
|
| 10 |
try:
|
|
|
|
| 22 |
ChatOpenAI = None # type: ignore
|
| 23 |
|
| 24 |
|
| 25 |
+
class ChatOpenRouter(ChatOpenAI):
|
| 26 |
+
openai_api_key: Optional[SecretStr] = Field(
|
| 27 |
+
alias="api_key",
|
| 28 |
+
default_factory=os.getenv("OPENROUTER_API_KEY", None),
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
@property
|
| 32 |
+
def lc_secrets(self) -> dict[str, str]:
|
| 33 |
+
return {"openai_api_key": "OPENROUTER_API_KEY"}
|
| 34 |
+
|
| 35 |
+
def __init__(self,
|
| 36 |
+
openai_api_key: Optional[str] = None,
|
| 37 |
+
**kwargs):
|
| 38 |
+
openai_api_key = (
|
| 39 |
+
openai_api_key or os.getenv("OPENROUTER_API_KEY")
|
| 40 |
+
)
|
| 41 |
+
super().__init__(
|
| 42 |
+
base_url="https://openrouter.ai/api/v1",
|
| 43 |
+
openai_api_key=openai_api_key,
|
| 44 |
+
**kwargs
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
class _EchoModel:
|
| 49 |
"""Simple stub model used when no API key / model is configured."""
|
| 50 |
|
|
|
|
| 80 |
|
| 81 |
# Choose an LLM if not provided
|
| 82 |
if model is None:
|
| 83 |
+
model = ChatOpenRouter(model="openai/gpt-oss-20b:free")
|
| 84 |
+
if model is None and ChatOpenAI is not None:
|
| 85 |
model = ChatOpenAI(
|
| 86 |
api_key=os.getenv("OPENROUTER_API_KEY"),
|
| 87 |
base_url=os.getenv("OPENROUTER_BASE_URL"),
|
| 88 |
model="openai/gpt-oss-20b:free",
|
| 89 |
)
|
| 90 |
+
if model is None:
|
| 91 |
+
model = _EchoModel()
|
| 92 |
self.model = model
|
| 93 |
|
| 94 |
# Load tools and bind to the model if supported
|
app.py
CHANGED
|
@@ -4,6 +4,32 @@ import requests
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
| 9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -16,8 +42,9 @@ class BasicAgent:
|
|
| 16 |
def __init__(self):
|
| 17 |
from agent import LangGraphAgent
|
| 18 |
print("BasicAgent initialized (LangGraph).")
|
| 19 |
-
# Create a
|
| 20 |
-
|
|
|
|
| 21 |
|
| 22 |
def __call__(self, question: str) -> str:
|
| 23 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
|
|
|
| 4 |
import inspect
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
# --- System Prompt Integration ---
|
| 8 |
+
try:
|
| 9 |
+
from prompt import get_system_prompt as _get_system_prompt # preferred
|
| 10 |
+
except Exception:
|
| 11 |
+
_get_system_prompt = None # type: ignore
|
| 12 |
+
try:
|
| 13 |
+
from prompt import SYSTEM_PROMPT as _SYSTEM_PROMPT # fallback constant
|
| 14 |
+
except Exception:
|
| 15 |
+
_SYSTEM_PROMPT = None # type: ignore
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def _load_system_prompt() -> str:
|
| 19 |
+
"""
|
| 20 |
+
Resolve system prompt from prompt.py:
|
| 21 |
+
- Prefer get_system_prompt() if available, else SYSTEM_PROMPT, else default.
|
| 22 |
+
"""
|
| 23 |
+
if callable(_get_system_prompt):
|
| 24 |
+
try:
|
| 25 |
+
return _get_system_prompt() # type: ignore
|
| 26 |
+
except Exception:
|
| 27 |
+
pass
|
| 28 |
+
if isinstance(_SYSTEM_PROMPT, str) and _SYSTEM_PROMPT.strip():
|
| 29 |
+
return _SYSTEM_PROMPT # type: ignore
|
| 30 |
+
return "You are a helpful assistant. Keep answers concise."
|
| 31 |
+
|
| 32 |
+
|
| 33 |
# (Keep Constants as is)
|
| 34 |
# --- Constants ---
|
| 35 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 42 |
def __init__(self):
|
| 43 |
from agent import LangGraphAgent
|
| 44 |
print("BasicAgent initialized (LangGraph).")
|
| 45 |
+
# Create a LangGraph agent with system prompt from prompt.py
|
| 46 |
+
system_prompt = _load_system_prompt()
|
| 47 |
+
self._agent = LangGraphAgent(system_prompt=system_prompt)
|
| 48 |
|
| 49 |
def __call__(self, question: str) -> str:
|
| 50 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|