Compare commits
4
Commits
6f7735f70f
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0cde86a0c7 | ||
|
|
527492326a | ||
|
|
49f785cf8d | ||
|
|
da48c38d51 |
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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.[/]")
|
||||||
+41
-66
@@ -1,71 +1,46 @@
|
|||||||
import json
|
import sys
|
||||||
from rich.console import Console
|
import argparse
|
||||||
from rich.spinner import Spinner
|
from agent import Agent
|
||||||
from qwen_agent.agents import Assistant
|
|
||||||
|
|
||||||
# Define LLM
|
def setup_argparse():
|
||||||
llm_cfg = {
|
parser = argparse.ArgumentParser(description='Qwen3 Agent CLI')
|
||||||
'model': 'hf.co/unsloth/Qwen3-30B-A3B-GGUF:Q5_K_M',
|
parser.add_argument('--model', default='qwen3:32b',
|
||||||
# 'model': '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)')
|
||||||
|
|
||||||
|
subparsers = parser.add_subparsers(dest='command', help='Available commands')
|
||||||
|
|
||||||
|
# Prompt command
|
||||||
|
prompt_parser = subparsers.add_parser('prompt', help='Run agent with a prompt')
|
||||||
|
prompt_parser.add_argument('text', nargs='?', default='-',
|
||||||
|
help='Prompt text or "-" for stdin (default: -)')
|
||||||
|
|
||||||
|
return parser
|
||||||
|
|
||||||
# Use a custom endpoint compatible with OpenAI API:
|
def read_prompt(text: str) -> str:
|
||||||
'model_server': 'http://localhost:11434/v1', # api_base
|
"""Read prompt from argument or stdin if text is '-'"""
|
||||||
'api_key': 'EMPTY',
|
if text == '-':
|
||||||
|
return sys.stdin.read().strip()
|
||||||
|
return text
|
||||||
|
|
||||||
# Other parameters:
|
|
||||||
# '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 main():
|
||||||
tools = [
|
parser = setup_argparse()
|
||||||
{'mcpServers': { # You can specify the MCP configuration file
|
args = parser.parse_args()
|
||||||
'time': {
|
|
||||||
'command': 'uvx',
|
if args.command == 'prompt':
|
||||||
'args': ['mcp-server-time', '--local-timezone=Europe/London']
|
prompt_text = read_prompt(args.text)
|
||||||
},
|
agent = Agent(
|
||||||
"fetch": {
|
model=args.model,
|
||||||
"command": "uvx",
|
server=args.server,
|
||||||
"args": ["mcp-server-fetch"]
|
api_key=args.api_key
|
||||||
},
|
)
|
||||||
"ddg-search": {
|
agent.run(prompt_text)
|
||||||
"command": "npx",
|
else:
|
||||||
"args": ["-y", "duckduckgo-mcp-server"]
|
parser.print_help()
|
||||||
},
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'code_interpreter', # Built-in tools
|
|
||||||
]
|
|
||||||
|
|
||||||
# Define Agent
|
if __name__ == '__main__':
|
||||||
bot = Assistant(llm=llm_cfg, function_list=tools)
|
main()
|
||||||
console = Console()
|
|
||||||
|
|
||||||
# Streaming generation
|
|
||||||
messages = [{'role': 'user',
|
|
||||||
'content':
|
|
||||||
""""
|
|
||||||
- ***Research** Parsing CLI commands and options in python code.
|
|
||||||
-- **Analyze** Clean separation of concerns between parsing commands and options and execution of the commands.
|
|
||||||
-- **Answer** What is the best way to parse CLI commands and options in python code?"""}]
|
|
||||||
|
|
||||||
final_responses = None
|
|
||||||
# Consider adding error handling around bot.run
|
|
||||||
try:
|
|
||||||
with console.status("[bold blue]Thinking...", spinner="dots") as status:
|
|
||||||
for responses in bot.run(messages=messages, enable_thinking=True, max_tokens=30000):
|
|
||||||
final_responses = responses.pop()
|
|
||||||
except Exception as e:
|
|
||||||
console.print(f"[bold red]An error occurred during agent execution:[/] {e}")
|
|
||||||
|
|
||||||
# Pretty-print the final response object
|
|
||||||
if final_responses:
|
|
||||||
console.print("\n[bold green]--- Full Response Object ---[/]")
|
|
||||||
console.print(json.dumps(final_responses, indent=2))
|
|
||||||
console.print("\n[bold green]--- Extracted Content ---[/]")
|
|
||||||
console.print(final_responses.get('content', 'No content found in response.'))
|
|
||||||
else:
|
|
||||||
console.print("[bold red]No final response received from the agent.[/]")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user