Skip to content

Commit 2453903

Browse files
committed
fixed some npc serve --port bug. also fixed openaii dalle processing that had goteen removed when including gpt-image-1. added a test_bash_cli file with commands for testing various npc cli functionalities.
1 parent 3d87586 commit 2453903

File tree

5 files changed

+126
-15
lines changed

5 files changed

+126
-15
lines changed

npcpy/gen/image_gen.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def openai_image_gen(
5151
if attachments is not None:
5252
# Process the attachments into the format OpenAI expects
5353
processed_images = []
54+
5455
for attachment in attachments:
5556
if isinstance(attachment, str):
5657
# Assume it's a file path
@@ -78,11 +79,18 @@ def openai_image_gen(
7879
n=n_images,
7980
size=f"{height}x{width}",
8081
)
81-
82-
image_base64 = result.data[0].b64_json
83-
image_bytes = base64.b64decode(image_base64)
84-
image = Image.open(io.BytesIO(image_bytes))
85-
image.save('generated_image.png')
82+
if model =='gpt-image-1':
83+
image_base64 = result.data[0].b64_json
84+
image_bytes = base64.b64decode(image_base64)
85+
image = Image.open(io.BytesIO(image_bytes))
86+
image.save('generated_image.png')
87+
elif model == 'dall-e-2' or model == 'dall-e-3':
88+
image_base64 = result.data[0].url
89+
import requests
90+
response = requests.get(image_base64)
91+
image = Image.open(io.BytesIO(response.content))
92+
image.save('generated_image.png')
93+
8694
return image
8795

8896

npcpy/modes/npc.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,52 @@ def main():
7676

7777
effective_model = args.model or NPCSH_CHAT_MODEL
7878
effective_provider = args.provider or NPCSH_CHAT_PROVIDER
79+
#print(args)
80+
#print("Debug - args dict:", vars(args))
81+
#print("Debug - unknown_args:", unknown_args)
82+
#print("Debug - command:", args.command)
83+
#print("Debug - command_args:", args.command_args)
84+
85+
extras = {}
86+
87+
# After parsing arguments, add this code to handle the case where flags are split
88+
if unknown_args and args.command_args:
89+
i = 0
90+
while i < len(unknown_args):
91+
# Check if the unknown arg is a flag (--something)
92+
if unknown_args[i].startswith('--'):
93+
# Extract the parameter name without the -- prefix
94+
param = unknown_args[i][2:]
95+
96+
# Add it to extras with the corresponding value from command_args
97+
if args.command_args:
98+
extras[param] = args.command_args[0]
99+
args.command_args.pop(0) # Remove the used value
100+
else:
101+
extras[param] = True
102+
i += 1
79103

80104
if args.command:
81105
handler = router.get_route(args.command)
82106
if not handler:
83-
print(f"Error: Command '{args.command}' recognized but no handler found.", file=sys.stderr)
107+
#print(f"Error: Command '{args.command}' recognized but no handler found.", file=sys.stderr)
84108
sys.exit(1)
85109

86110
full_command_str = args.command
87111
command_args = []
88-
extras = {}
89112

90113
# Parse command args properly
91-
print(args)
92114
if args.command_args:
93115
i = 0
94116
while i < len(args.command_args):
95-
96117
arg = args.command_args[i]
97118
if arg.startswith("--"):
98119
param = arg[2:] # Remove --
99-
if i + 1 < len(args.command_args) and not args.command_args[i+1].startswith("--"):
120+
if "=" in param:
121+
param_name, param_value = param.split("=", 1)
122+
extras[param_name] = param_value
123+
i += 1
124+
elif i + 1 < len(args.command_args) and not args.command_args[i+1].startswith("--"):
100125
extras[param] = args.command_args[i+1]
101126
i += 2
102127
else:
@@ -110,7 +135,6 @@ def main():
110135
if prompt_parts:
111136
full_command_str += " " + " ".join(prompt_parts)
112137

113-
print(full_command_str)
114138
handler_kwargs = {
115139
"model": effective_model,
116140
"provider": effective_provider,

npcpy/modes/serve.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,8 +1512,8 @@ def start_flask_server(
15121512
)
15131513

15141514
# Run the Flask app on all interfaces
1515-
print("Starting Flask server on http://0.0.0.0:5337")
1516-
app.run(host="0.0.0.0", port=5337, debug=True)
1515+
print(f"Starting Flask server on http://0.0.0.0:{port}")
1516+
app.run(host="0.0.0.0", port=port, debug=True)
15171517
except Exception as e:
15181518
print(f"Error starting server: {str(e)}")
15191519

npcpy/routes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,11 @@ def search_handler(command: str, **kwargs):
552552

553553
@router.route("serve", "Set configuration values")
554554
def serve_handler(command: str, **kwargs):
555-
555+
#print('calling serve handler')
556+
#print(kwargs)
556557

557558
port = safe_get(kwargs, "port", 5337)
559+
#print(port, type(port))
558560
messages = safe_get(kwargs, "messages", [])
559561
cors = safe_get(kwargs, "cors", None)
560562
if cors:
@@ -719,7 +721,6 @@ def vixynt_handler(command: str, **kwargs):
719721
prompt=user_prompt,
720722
model=model,
721723
provider=provider,
722-
filename=filename,
723724
npc=npc,
724725
height=height,
725726
width=width,

tests/test_bash_cli.sh

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Basic tests and help commands
2+
npc help
3+
npc --help
4+
5+
# Test serve command with different port specification formats
6+
npc serve --port=5340
7+
npc serve --port 5341
8+
npc serve -port=5342
9+
npc serve -port 5343
10+
npc serve --cors="http://localhost:3000,http://localhost:8080"
11+
12+
# Test search commands with different providers
13+
npc search "python asyncio tutorial"
14+
npc search -p google "python asyncio tutorial"
15+
npc search --provider perplexity "machine learning basics"
16+
17+
# Test sample/LLM commands
18+
npc sample "Write a hello world program in Python"
19+
npc sample "Compare Python and JavaScript" --model gpt-4 --provider openai
20+
21+
# Test file and image processing
22+
npc rag "litellm" -f ../setup.py
23+
npc rag --file /path/to/document.txt "What is this document about?"
24+
npc ots /path/to/screenshot.png "What's happening in this image?"
25+
npc vixynt "A beautiful sunset over mountains" filename=sunset.png height=512 width=768
26+
27+
# Test with different NPC selection
28+
npc -n custom_npc "Tell me about yourself"
29+
npc --npc alternative_assistant "How can you help me?"
30+
31+
# Test other route commands
32+
npc sleep 3
33+
npc jinxs
34+
npc init /tmp/new_npc_project
35+
npc wander "How to implement a cache system"
36+
npc plan "Create a new Python project with virtual environment"
37+
npc trigger "Update all npm packages in the current directory"
38+
39+
# Testing command with multiple arguments and options
40+
npc serve --port 5338 --cors "http://localhost:3000"
41+
npc vixynt "A city made by pepsi" height=1024 width=1024 filename=pepsi_city.png
42+
npc rag -f /path/to/file1.txt -f /path/to/file2.txt "Compare these two documents"
43+
44+
45+
46+
# Pipe file content to NPC sample command
47+
cat data.json | npc sample "Summarize this JSON data"
48+
49+
# Pipe command output to NPC
50+
ls -la | npc sample "Explain what these files are"
51+
52+
# Use grep to filter logs and have NPC analyze them
53+
grep ERROR /var/log/application.log | npc sample "What are the common error patterns?"
54+
55+
# Use curl to get API response and analyze with NPC
56+
curl -s https://api.example.com/data | npc sample "Analyze this API response"
57+
58+
# Create a multi-line prompt using heredoc
59+
cat << EOF | npc sample
60+
I need to understand how to structure a React application.
61+
What are the best practices for component organization?
62+
Should I use Redux or Context API for state management?
63+
EOF
64+
65+
66+
67+
68+
# Chain NPC commands using xargs
69+
npc search "machine learning algorithms" | xargs -I {} npc sample "Explain {} in detail"
70+
71+
# Use output from one NPC command as input to another
72+
npc sample "Generate 5 test cases" | npc sample "Convert these test cases to JavaScript code"
73+
74+
# Use NPC to generate code and then analyze it
75+
npc sample "Write a Python sorting algorithm" | npc sample "Review this code for efficiency"
76+
77+
# Generate image description and then create image
78+
npc sample "Describe a futuristic cityscape" | xargs npc vixynt

0 commit comments

Comments
 (0)