SYSTEM PROMPT
Browse files- agent/agent.py +8 -2
- agent/tools.py +9 -3
- app.py +17 -1
- requirements.txt +3 -0
agent/agent.py
CHANGED
|
@@ -33,12 +33,14 @@ class Agent:
|
|
| 33 |
api_key: str,
|
| 34 |
model: str,
|
| 35 |
max_iterations: int = 15,
|
|
|
|
| 36 |
) -> None:
|
| 37 |
self.client = OpenAI(base_url=base_url, api_key=api_key)
|
| 38 |
self.model = model
|
| 39 |
self._tools: list[Tool] = []
|
| 40 |
self._final_tool_name: str | None = None
|
| 41 |
self._max_iterations = max_iterations
|
|
|
|
| 42 |
|
| 43 |
# ------------------------------------------------------------------
|
| 44 |
# Tool registration
|
|
@@ -86,6 +88,12 @@ class Agent:
|
|
| 86 |
"""
|
| 87 |
iteration = 0
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
while True:
|
| 90 |
iteration += 1
|
| 91 |
if iteration > self._max_iterations:
|
|
@@ -123,10 +131,8 @@ class Agent:
|
|
| 123 |
continue
|
| 124 |
delta = choices[0].get("delta", {})
|
| 125 |
|
| 126 |
-
# Reasoning (e.g. DeepSeek R1)
|
| 127 |
reasoning = delta.get("reasoning_content") or delta.get("reasoning")
|
| 128 |
if reasoning:
|
| 129 |
-
print(reasoning, flush=True, end="")
|
| 130 |
yield {"type": "reasoning", "content": reasoning}
|
| 131 |
|
| 132 |
# Text content
|
|
|
|
| 33 |
api_key: str,
|
| 34 |
model: str,
|
| 35 |
max_iterations: int = 15,
|
| 36 |
+
system_prompt: str | None = None,
|
| 37 |
) -> None:
|
| 38 |
self.client = OpenAI(base_url=base_url, api_key=api_key)
|
| 39 |
self.model = model
|
| 40 |
self._tools: list[Tool] = []
|
| 41 |
self._final_tool_name: str | None = None
|
| 42 |
self._max_iterations = max_iterations
|
| 43 |
+
self.system_prompt = system_prompt
|
| 44 |
|
| 45 |
# ------------------------------------------------------------------
|
| 46 |
# Tool registration
|
|
|
|
| 88 |
"""
|
| 89 |
iteration = 0
|
| 90 |
|
| 91 |
+
# Inject system prompt once at the front if set
|
| 92 |
+
if self.system_prompt and (
|
| 93 |
+
not messages or messages[0].get("role") != "system"
|
| 94 |
+
):
|
| 95 |
+
messages.insert(0, {"role": "system", "content": self.system_prompt})
|
| 96 |
+
|
| 97 |
while True:
|
| 98 |
iteration += 1
|
| 99 |
if iteration > self._max_iterations:
|
|
|
|
| 131 |
continue
|
| 132 |
delta = choices[0].get("delta", {})
|
| 133 |
|
|
|
|
| 134 |
reasoning = delta.get("reasoning_content") or delta.get("reasoning")
|
| 135 |
if reasoning:
|
|
|
|
| 136 |
yield {"type": "reasoning", "content": reasoning}
|
| 137 |
|
| 138 |
# Text content
|
agent/tools.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
from markitdown import MarkItDown
|
| 2 |
from typing import Any, Callable
|
| 3 |
-
|
| 4 |
|
| 5 |
class Tool:
|
| 6 |
"""A callable tool the agent can invoke."""
|
|
@@ -29,8 +29,14 @@ class Tool:
|
|
| 29 |
|
| 30 |
def fetch_webpage(url: str) -> str:
|
| 31 |
"""Fetch a webpage and return its text content."""
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
FETCH_WEBPAGE_TOOL = Tool(
|
| 36 |
name="fetch_webpage",
|
|
|
|
| 1 |
from markitdown import MarkItDown
|
| 2 |
from typing import Any, Callable
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
class Tool:
|
| 6 |
"""A callable tool the agent can invoke."""
|
|
|
|
| 29 |
|
| 30 |
def fetch_webpage(url: str) -> str:
|
| 31 |
"""Fetch a webpage and return its text content."""
|
| 32 |
+
try:
|
| 33 |
+
jina_ai_url = "https://r.jina.ai/"
|
| 34 |
+
response = requests.get(jina_ai_url + url)
|
| 35 |
+
response.raise_for_status()
|
| 36 |
+
return response.text
|
| 37 |
+
except Exception as e:
|
| 38 |
+
md = MarkItDown()
|
| 39 |
+
return md.convert(url).text_content
|
| 40 |
|
| 41 |
FETCH_WEBPAGE_TOOL = Tool(
|
| 42 |
name="fetch_webpage",
|
app.py
CHANGED
|
@@ -9,10 +9,26 @@ from pathlib import Path
|
|
| 9 |
load_dotenv()
|
| 10 |
gr.set_static_paths("static/")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
agent = Agent(
|
| 13 |
base_url=os.getenv("OPENAI_BASE_URL"),
|
| 14 |
api_key=os.getenv("OPENAI_API_KEY"),
|
| 15 |
model=os.getenv("OPENAI_MODEL"),
|
|
|
|
| 16 |
)
|
| 17 |
agent.register_tool(FETCH_WEBPAGE_TOOL)
|
| 18 |
agent.register_final_message_tool()
|
|
@@ -182,7 +198,7 @@ class GradioEvents:
|
|
| 182 |
except Exception as exc:
|
| 183 |
display_messages.append({
|
| 184 |
"role": "assistant",
|
| 185 |
-
"content": f'<span style="color: var(--color-red-
|
| 186 |
"metadata": {"title": "💥 Error"},
|
| 187 |
})
|
| 188 |
yield {
|
|
|
|
| 9 |
load_dotenv()
|
| 10 |
gr.set_static_paths("static/")
|
| 11 |
|
| 12 |
+
_SYSTEM_PROMPT = """\
|
| 13 |
+
You are OpenMythos, a powerful AI agent specialized in cybersecurity-related tasks.
|
| 14 |
+
|
| 15 |
+
You have access to tools that you can use to accomplish your goals.
|
| 16 |
+
|
| 17 |
+
=== IMPORTANT: How to end the conversation ===
|
| 18 |
+
You MUST call the `final_message` tool when you have completed your response and want to end.
|
| 19 |
+
If you do NOT call `final_message`, you will be stuck in a loop:
|
| 20 |
+
- You respond → system waits for final_message → you did not call it
|
| 21 |
+
- → system sends your response back to you → you must respond again
|
| 22 |
+
- → this repeats until you call `final_message`
|
| 23 |
+
To break out of the loop, simply call `final_message` with no arguments.
|
| 24 |
+
Only call `final_message` when you are done or already responded or stuck in a loop.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
agent = Agent(
|
| 28 |
base_url=os.getenv("OPENAI_BASE_URL"),
|
| 29 |
api_key=os.getenv("OPENAI_API_KEY"),
|
| 30 |
model=os.getenv("OPENAI_MODEL"),
|
| 31 |
+
system_prompt=_SYSTEM_PROMPT,
|
| 32 |
)
|
| 33 |
agent.register_tool(FETCH_WEBPAGE_TOOL)
|
| 34 |
agent.register_final_message_tool()
|
|
|
|
| 198 |
except Exception as exc:
|
| 199 |
display_messages.append({
|
| 200 |
"role": "assistant",
|
| 201 |
+
"content": f'<span style="color: var(--color-red-400)">{exc}</span>',
|
| 202 |
"metadata": {"title": "💥 Error"},
|
| 203 |
})
|
| 204 |
yield {
|
requirements.txt
CHANGED
|
@@ -1,3 +1,6 @@
|
|
| 1 |
gradio==6.8.0
|
| 2 |
openai==2.41.0
|
| 3 |
python-dotenv==1.2.2
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
gradio==6.8.0
|
| 2 |
openai==2.41.0
|
| 3 |
python-dotenv==1.2.2
|
| 4 |
+
cloudscraper>=1.2,<2
|
| 5 |
+
trafilatura>=2.0,<3
|
| 6 |
+
markitdown>=0.1,<1
|