Compare commits
7
Commits
76eb0d17c1
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0cde86a0c7 | ||
|
|
527492326a | ||
|
|
49f785cf8d | ||
|
|
da48c38d51 | ||
|
|
6f7735f70f | ||
|
|
e2f6f4e15a | ||
|
|
5a9932a1e7 |
@@ -9,3 +9,4 @@ wheels/
|
|||||||
# Virtual environments
|
# Virtual environments
|
||||||
.venv
|
.venv
|
||||||
/workspace/tools/
|
/workspace/tools/
|
||||||
|
.aider*
|
||||||
|
|||||||
@@ -56,9 +56,19 @@ Event working with tools, when used with `agentic_search.py` worked, up to a poi
|
|||||||
|
|
||||||
2. **Run the agent script:**
|
2. **Run the agent script:**
|
||||||
```bash
|
```bash
|
||||||
python agentic_search.py
|
# Run with direct prompt
|
||||||
|
python agentic_search.py --model "qwen3:32b" prompt "Your prompt here"
|
||||||
|
|
||||||
|
# Run with prompt from stdin
|
||||||
|
echo "Your prompt" | python agentic_search.py prompt -
|
||||||
|
|
||||||
|
# Run with custom server and API key
|
||||||
|
python agentic_search.py \
|
||||||
|
--model "hf.co/unsloth/Qwen3-30B-A3B-GGUF:Q5_K_M" \
|
||||||
|
--server "https://api.example.com/v1" \
|
||||||
|
--api-key "your-key" \
|
||||||
|
prompt "Your prompt"
|
||||||
```
|
```
|
||||||
This will execute the predefined query in the script, run the agent, print progress dots (`.`) for each response chunk, and finally output the full structured response and the extracted content.
|
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
|
|
||||||
@@ -68,6 +78,7 @@ Key Python libraries used:
|
|||||||
* `mlx` / `mlx-lm`: Likely used for efficient model inference, especially on Apple Silicon.
|
* `mlx` / `mlx-lm`: Likely used for efficient model inference, especially on Apple Silicon.
|
||||||
* `mcp`: For integrating external tools via the Multi-Agent Collaboration Protocol.
|
* `mcp`: For integrating external tools via the Multi-Agent Collaboration Protocol.
|
||||||
* `python-dotenv`: For managing environment variables (e.g., API keys).
|
* `python-dotenv`: For managing environment variables (e.g., API keys).
|
||||||
|
* `rich`: For beautiful terminal formatting and progress indicators.
|
||||||
|
|
||||||
See `pyproject.toml` for the full list of dependencies.
|
See `pyproject.toml` for the full list of dependencies.
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
import json
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional, List, Dict, Any
|
||||||
|
from rich.console import Console
|
||||||
|
from qwen_agent.agents import Assistant
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Agent:
|
||||||
|
model: str
|
||||||
|
server: str
|
||||||
|
api_key: str
|
||||||
|
max_tokens: int = 30000
|
||||||
|
enable_thinking: bool = True
|
||||||
|
tools: Optional[List[Dict[str, Any]]] = None
|
||||||
|
console: Console = Console()
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
if self.tools is None:
|
||||||
|
self.tools = [
|
||||||
|
{'mcpServers': {
|
||||||
|
'time': {
|
||||||
|
'command': 'uvx',
|
||||||
|
'args': ['mcp-server-time', '--local-timezone=Europe/London']
|
||||||
|
},
|
||||||
|
"fetch": {
|
||||||
|
"command": "uvx",
|
||||||
|
"args": ["mcp-server-fetch"]
|
||||||
|
},
|
||||||
|
"ddg-search": {
|
||||||
|
"command": "npx",
|
||||||
|
"args": ["-y", "duckduckgo-mcp-server"]
|
||||||
|
},
|
||||||
|
}},
|
||||||
|
'code_interpreter',
|
||||||
|
]
|
||||||
|
|
||||||
|
def run(self, prompt: str) -> None:
|
||||||
|
"""Run the agent with the given prompt"""
|
||||||
|
llm_cfg = {
|
||||||
|
'model': self.model,
|
||||||
|
'model_server': self.server,
|
||||||
|
'api_key': self.api_key,
|
||||||
|
}
|
||||||
|
|
||||||
|
# Define Agent
|
||||||
|
bot = Assistant(llm=llm_cfg, function_list=self.tools)
|
||||||
|
|
||||||
|
# Streaming generation
|
||||||
|
messages = [{'role': 'user', 'content': prompt}]
|
||||||
|
|
||||||
|
final_responses = None
|
||||||
|
try:
|
||||||
|
with self.console.status("[bold blue]Thinking...", spinner="dots") as status:
|
||||||
|
for responses in bot.run(messages=messages,
|
||||||
|
enable_thinking=self.enable_thinking,
|
||||||
|
max_tokens=self.max_tokens):
|
||||||
|
final_responses = responses.pop()
|
||||||
|
except Exception as e:
|
||||||
|
self.console.print(f"[bold red]An error occurred during agent execution:[/] {e}")
|
||||||
|
|
||||||
|
# Pretty-print the final response object
|
||||||
|
if final_responses:
|
||||||
|
self.console.print("\n[bold green]--- Full Response Object ---[/]")
|
||||||
|
self.console.print(json.dumps(final_responses, indent=2))
|
||||||
|
self.console.print("\n[bold green]--- Extracted Content ---[/]")
|
||||||
|
self.console.print(final_responses.get('content', 'No content found in response.'))
|
||||||
|
else:
|
||||||
|
self.console.print("[bold red]No final response received from the agent.[/]")
|
||||||
+37
-59
@@ -1,68 +1,46 @@
|
|||||||
import json # Import the json module
|
import sys
|
||||||
|
import argparse
|
||||||
|
from agent import Agent
|
||||||
|
|
||||||
from qwen_agent.agents import Assistant
|
def setup_argparse():
|
||||||
|
parser = argparse.ArgumentParser(description='Qwen3 Agent CLI')
|
||||||
|
parser.add_argument('--model', default='qwen3:32b',
|
||||||
|
help='Model identifier (default: qwen3:32b)')
|
||||||
|
parser.add_argument('--server', default='http://localhost:11434/v1',
|
||||||
|
help='Model server URL (default: http://localhost:11434/v1)')
|
||||||
|
parser.add_argument('--api-key', default='EMPTY',
|
||||||
|
help='API key for the model server (default: EMPTY)')
|
||||||
|
|
||||||
# Define LLM
|
subparsers = parser.add_subparsers(dest='command', help='Available commands')
|
||||||
llm_cfg = {
|
|
||||||
'model': 'hf.co/unsloth/Qwen3-30B-A3B-GGUF:Q5_K_M',
|
|
||||||
# 'model': 'qwen3:32b',
|
|
||||||
|
|
||||||
# Use a custom endpoint compatible with OpenAI API:
|
# Prompt command
|
||||||
'model_server': 'http://localhost:11434/v1', # api_base
|
prompt_parser = subparsers.add_parser('prompt', help='Run agent with a prompt')
|
||||||
'api_key': 'EMPTY',
|
prompt_parser.add_argument('text', nargs='?', default='-',
|
||||||
|
help='Prompt text or "-" for stdin (default: -)')
|
||||||
|
|
||||||
# Other parameters:
|
return parser
|
||||||
# 'generate_cfg': {
|
|
||||||
# # Add: When the response content is `<think>this is the thought</think>this is the answer;
|
|
||||||
# # Do not add: When the response has been separated by reasoning_content and content.
|
|
||||||
# 'thought_in_content': True,
|
|
||||||
# },
|
|
||||||
}
|
|
||||||
|
|
||||||
# Define Tools
|
def read_prompt(text: str) -> str:
|
||||||
tools = [
|
"""Read prompt from argument or stdin if text is '-'"""
|
||||||
{'mcpServers': { # You can specify the MCP configuration file
|
if text == '-':
|
||||||
'time': {
|
return sys.stdin.read().strip()
|
||||||
'command': 'uvx',
|
return text
|
||||||
'args': ['mcp-server-time', '--local-timezone=Europe/London']
|
|
||||||
},
|
|
||||||
"fetch": {
|
|
||||||
"command": "uvx",
|
|
||||||
"args": ["mcp-server-fetch"]
|
|
||||||
},
|
|
||||||
"ddg-search": {
|
|
||||||
"command": "npx",
|
|
||||||
"args": ["-y", "duckduckgo-mcp-server"]
|
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'code_interpreter', # Built-in tools
|
|
||||||
]
|
|
||||||
|
|
||||||
# Define Agent
|
|
||||||
bot = Assistant(llm=llm_cfg, function_list=tools)
|
|
||||||
|
|
||||||
# Streaming generation
|
def main():
|
||||||
messages = [{'role': 'user',
|
parser = setup_argparse()
|
||||||
'content':
|
args = parser.parse_args()
|
||||||
""""- ***Research** What is enshittification, and who came up with it?
|
|
||||||
-- **Analyze** Developments around enshittification in the last five years, and related concepts.
|
|
||||||
-- **Answer** What is enshittification, and what does it mean for society?"""}]
|
|
||||||
|
|
||||||
final_responses = None
|
if args.command == 'prompt':
|
||||||
# Consider adding error handling around bot.run
|
prompt_text = read_prompt(args.text)
|
||||||
try:
|
agent = Agent(
|
||||||
for responses in bot.run(messages=messages, enable_thinking=True, max_tokens=30000):
|
model=args.model,
|
||||||
print(".", end="", flush=True)
|
server=args.server,
|
||||||
final_responses = responses.pop()
|
api_key=args.api_key
|
||||||
except Exception as e:
|
)
|
||||||
print(f"An error occurred during agent execution: {e}")
|
agent.run(prompt_text)
|
||||||
|
|
||||||
# Pretty-print the final response object
|
|
||||||
if final_responses:
|
|
||||||
print("--- Full Response Object ---")
|
|
||||||
print(json.dumps(final_responses, indent=2)) # Use indent=2 (or 4) for pretty printing
|
|
||||||
print("\n--- Extracted Content ---")
|
|
||||||
print(final_responses.get('content', 'No content found in response.')) # Use .get for safer access
|
|
||||||
else:
|
else:
|
||||||
print("No final response received from the agent.")
|
parser.print_help()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|||||||
@@ -11,4 +11,5 @@ dependencies = [
|
|||||||
"python-dateutil>=2.9.0.post0",
|
"python-dateutil>=2.9.0.post0",
|
||||||
"python-dotenv>=1.1.0",
|
"python-dotenv>=1.1.0",
|
||||||
"qwen-agent[code-interpreter]>=0.0.20",
|
"qwen-agent[code-interpreter]>=0.0.20",
|
||||||
|
"rich>=13.7.0",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -1016,6 +1016,18 @@ wheels = [
|
|||||||
{ url = "https://files.pythonhosted.org/packages/4c/fa/be89a49c640930180657482a74970cdcf6f7072c8d2471e1babe17a222dc/kiwisolver-1.4.8-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:be4816dc51c8a471749d664161b434912eee82f2ea66bd7628bd14583a833e85", size = 2349213 },
|
{ url = "https://files.pythonhosted.org/packages/4c/fa/be89a49c640930180657482a74970cdcf6f7072c8d2471e1babe17a222dc/kiwisolver-1.4.8-cp313-cp313t-musllinux_1_2_x86_64.whl", hash = "sha256:be4816dc51c8a471749d664161b434912eee82f2ea66bd7628bd14583a833e85", size = 2349213 },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "markdown-it-py"
|
||||||
|
version = "3.0.0"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
dependencies = [
|
||||||
|
{ name = "mdurl" },
|
||||||
|
]
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/38/71/3b932df36c1a044d397a1f92d1cf91ee0a503d91e470cbd670aa66b07ed0/markdown-it-py-3.0.0.tar.gz", hash = "sha256:e3f60a94fa066dc52ec76661e37c851cb232d92f9886b15cb560aaada2df8feb", size = 74596 }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/42/d7/1ec15b46af6af88f19b8e5ffea08fa375d433c998b8a7639e76935c14f1f/markdown_it_py-3.0.0-py3-none-any.whl", hash = "sha256:355216845c60bd96232cd8d8c40e8f9765cc86f46880e43a8fd22dc1a1a8cab1", size = 87528 },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "markupsafe"
|
name = "markupsafe"
|
||||||
version = "3.0.2"
|
version = "3.0.2"
|
||||||
@@ -1106,6 +1118,15 @@ wheels = [
|
|||||||
{ url = "https://files.pythonhosted.org/packages/10/30/20a7f33b0b884a9d14dd3aa94ff1ac9da1479fe2ad66dd9e2736075d2506/mcp-1.6.0-py3-none-any.whl", hash = "sha256:7bd24c6ea042dbec44c754f100984d186620d8b841ec30f1b19eda9b93a634d0", size = 76077 },
|
{ url = "https://files.pythonhosted.org/packages/10/30/20a7f33b0b884a9d14dd3aa94ff1ac9da1479fe2ad66dd9e2736075d2506/mcp-1.6.0-py3-none-any.whl", hash = "sha256:7bd24c6ea042dbec44c754f100984d186620d8b841ec30f1b19eda9b93a634d0", size = 76077 },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mdurl"
|
||||||
|
version = "0.1.2"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/d6/54/cfe61301667036ec958cb99bd3efefba235e65cdeb9c84d24a8293ba1d90/mdurl-0.1.2.tar.gz", hash = "sha256:bb413d29f5eea38f31dd4754dd7377d4465116fb207585f97bf925588687c1ba", size = 8729 }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/b3/38/89ba8ad64ae25be8de66a6d463314cf1eb366222074cfda9ee839c56a4b4/mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8", size = 9979 },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mistune"
|
name = "mistune"
|
||||||
version = "3.1.3"
|
version = "3.1.3"
|
||||||
@@ -1791,6 +1812,7 @@ dependencies = [
|
|||||||
{ name = "python-dateutil" },
|
{ name = "python-dateutil" },
|
||||||
{ name = "python-dotenv" },
|
{ name = "python-dotenv" },
|
||||||
{ name = "qwen-agent", extra = ["code-interpreter"] },
|
{ name = "qwen-agent", extra = ["code-interpreter"] },
|
||||||
|
{ name = "rich" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.metadata]
|
[package.metadata]
|
||||||
@@ -1801,6 +1823,7 @@ requires-dist = [
|
|||||||
{ name = "python-dateutil", specifier = ">=2.9.0.post0" },
|
{ name = "python-dateutil", specifier = ">=2.9.0.post0" },
|
||||||
{ name = "python-dotenv", specifier = ">=1.1.0" },
|
{ name = "python-dotenv", specifier = ">=1.1.0" },
|
||||||
{ name = "qwen-agent", extras = ["code-interpreter"], specifier = ">=0.0.20" },
|
{ name = "qwen-agent", extras = ["code-interpreter"], specifier = ">=0.0.20" },
|
||||||
|
{ name = "rich", specifier = ">=13.7.0" },
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1875,6 +1898,19 @@ wheels = [
|
|||||||
{ url = "https://files.pythonhosted.org/packages/9e/51/17023c0f8f1869d8806b979a2bffa3f861f26a3f1a66b094288323fba52f/rfc3986_validator-0.1.1-py2.py3-none-any.whl", hash = "sha256:2f235c432ef459970b4306369336b9d5dbdda31b510ca1e327636e01f528bfa9", size = 4242 },
|
{ url = "https://files.pythonhosted.org/packages/9e/51/17023c0f8f1869d8806b979a2bffa3f861f26a3f1a66b094288323fba52f/rfc3986_validator-0.1.1-py2.py3-none-any.whl", hash = "sha256:2f235c432ef459970b4306369336b9d5dbdda31b510ca1e327636e01f528bfa9", size = 4242 },
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rich"
|
||||||
|
version = "14.0.0"
|
||||||
|
source = { registry = "https://pypi.org/simple" }
|
||||||
|
dependencies = [
|
||||||
|
{ name = "markdown-it-py" },
|
||||||
|
{ name = "pygments" },
|
||||||
|
]
|
||||||
|
sdist = { url = "https://files.pythonhosted.org/packages/a1/53/830aa4c3066a8ab0ae9a9955976fb770fe9c6102117c8ec4ab3ea62d89e8/rich-14.0.0.tar.gz", hash = "sha256:82f1bc23a6a21ebca4ae0c45af9bdbc492ed20231dcb63f297d6d1021a9d5725", size = 224078 }
|
||||||
|
wheels = [
|
||||||
|
{ url = "https://files.pythonhosted.org/packages/0d/9b/63f4c7ebc259242c89b3acafdb37b41d1185c07ff0011164674e9076b491/rich-14.0.0-py3-none-any.whl", hash = "sha256:1c9491e1951aac09caffd42f448ee3d04e58923ffe14993f6e83068dc395d7e0", size = 243229 },
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rpds-py"
|
name = "rpds-py"
|
||||||
version = "0.24.0"
|
version = "0.24.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user