feat: Add new tools and configurations for Castle platform
- Introduced `uv.lock` for dependency management with various packages including `pytest`, `colorama`, and `pluggy`. - Added `pyrightconfig.json` for Python type checking configuration. - Expanded `recommendations.md` with detailed scaling recommendations and project management strategies. - Created shared `ruff.toml` for consistent linting across projects. - Developed `Castle Tools` with various utilities including Android backup, browser automation, document conversion, and search tools. - Implemented `backup-collect` and `schedule` tools for system administration tasks. - Enhanced `search` functionality with indexing and querying capabilities using Tantivy. - Added comprehensive documentation for each tool, including usage examples and installation instructions.
This commit is contained in:
18
tools/gpt/pyproject.toml
Normal file
18
tools/gpt/pyproject.toml
Normal file
@@ -0,0 +1,18 @@
|
||||
[project]
|
||||
name = "castle-gpt"
|
||||
version = "0.1.0"
|
||||
description = "Castle GPT tools"
|
||||
requires-python = ">=3.11"
|
||||
dependencies = [
|
||||
"openai>=1.6.0",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
gpt = "gpt.gpt:main"
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[tool.hatch.build.targets.wheel]
|
||||
packages = ["src/gpt"]
|
||||
0
tools/gpt/src/gpt/__init__.py
Normal file
0
tools/gpt/src/gpt/__init__.py
Normal file
157
tools/gpt/src/gpt/gpt.md
Normal file
157
tools/gpt/src/gpt/gpt.md
Normal file
@@ -0,0 +1,157 @@
|
||||
---
|
||||
command: gpt
|
||||
script: gpt/gpt.py
|
||||
description: A simple OpenAI text generation utility
|
||||
version: 1.0.0
|
||||
category: gpt
|
||||
---
|
||||
|
||||
# gpt
|
||||
|
||||
Interact with OpenAI's GPT models from the command line. Send prompts and receive AI-generated responses.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- OpenAI API key
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
cd /data/repos/toolkit
|
||||
make install
|
||||
which gpt
|
||||
```
|
||||
|
||||
## Setup
|
||||
|
||||
```bash
|
||||
# Set API key
|
||||
export OPENAI_API_KEY=sk-...
|
||||
|
||||
# Add to ~/.bashrc or ~/.profile to persist
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic Usage
|
||||
|
||||
```bash
|
||||
# Send a prompt
|
||||
gpt "Explain quantum computing in simple terms"
|
||||
|
||||
# Use specific model
|
||||
gpt --model gpt-4 "Write a Python function to sort a list"
|
||||
|
||||
# Read prompt from file
|
||||
gpt < prompt.txt
|
||||
|
||||
# Interactive mode
|
||||
echo "What is the capital of France?" | gpt
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
```bash
|
||||
gpt [options] "prompt"
|
||||
|
||||
Options:
|
||||
prompt Text prompt for GPT (required)
|
||||
--model MODEL GPT model to use (default: gpt-4o-mini)
|
||||
--temperature TEMP Creativity level 0.0-2.0 (default: 0.7)
|
||||
--max-tokens NUM Maximum response length (default: 1000)
|
||||
--system PROMPT System prompt to set behavior
|
||||
-h, --help Show help message
|
||||
```
|
||||
|
||||
## Supported Models
|
||||
|
||||
- `gpt-4o` - Most capable, best for complex tasks
|
||||
- `gpt-4o-mini` - Fast and affordable (default)
|
||||
- `gpt-4-turbo` - Large context window
|
||||
- `gpt-4` - High capability
|
||||
- `gpt-3.5-turbo` - Fast and economical
|
||||
|
||||
## Examples
|
||||
|
||||
### Simple Questions
|
||||
|
||||
```bash
|
||||
gpt "What is the weather like today?"
|
||||
gpt "Explain recursion"
|
||||
```
|
||||
|
||||
### Code Generation
|
||||
|
||||
```bash
|
||||
gpt "Write a Python function to calculate fibonacci numbers"
|
||||
gpt --model gpt-4 "Create a React component for a todo list"
|
||||
```
|
||||
|
||||
### Text Processing
|
||||
|
||||
```bash
|
||||
# Summarize a file
|
||||
cat article.txt | gpt "Summarize this article in 3 bullet points"
|
||||
|
||||
# Translate
|
||||
echo "Hello world" | gpt "Translate to French"
|
||||
|
||||
# Code review
|
||||
cat script.py | gpt "Review this code and suggest improvements"
|
||||
```
|
||||
|
||||
### With System Prompts
|
||||
|
||||
```bash
|
||||
gpt --system "You are a Python expert" "How do I use asyncio?"
|
||||
gpt --system "Respond in haiku form" "Describe AI"
|
||||
```
|
||||
|
||||
### Batch Processing
|
||||
|
||||
```bash
|
||||
# Process multiple prompts
|
||||
for question in "What is AI?" "What is ML?" "What is DL?"; do
|
||||
gpt "$question" >> answers.txt
|
||||
done
|
||||
```
|
||||
|
||||
## Features
|
||||
|
||||
- **Multiple models** - Choose from various GPT models
|
||||
- **Flexible input** - Prompt as argument or from stdin
|
||||
- **Customizable** - Adjust temperature, tokens, system prompts
|
||||
- **Pipe-friendly** - Works in Unix pipelines
|
||||
- **Fast** - Direct API access
|
||||
|
||||
## Environment Variables
|
||||
|
||||
- `OPENAI_API_KEY` - Your OpenAI API key (required)
|
||||
|
||||
## Tips
|
||||
|
||||
- Use `gpt-4o-mini` for fast, cheap responses (default)
|
||||
- Use `gpt-4` or `gpt-4o` for complex reasoning
|
||||
- Lower temperature (0.1-0.3) for factual responses
|
||||
- Higher temperature (0.8-1.5) for creative writing
|
||||
- System prompts help control tone and expertise level
|
||||
|
||||
## Cost Considerations
|
||||
|
||||
- `gpt-4o-mini` is most economical for general use
|
||||
- `gpt-4` costs more but provides higher quality
|
||||
- Limit max-tokens to control costs
|
||||
- Monitor usage in OpenAI dashboard
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### API Key Not Found
|
||||
|
||||
```bash
|
||||
echo $OPENAI_API_KEY # Should show your key
|
||||
export OPENAI_API_KEY=sk-...
|
||||
```
|
||||
|
||||
### Rate Limits
|
||||
|
||||
If you hit rate limits, wait a moment and retry, or upgrade your OpenAI plan.
|
||||
151
tools/gpt/src/gpt/gpt.py
Executable file
151
tools/gpt/src/gpt/gpt.py
Executable file
@@ -0,0 +1,151 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
gpt: A simple OpenAI text generation utility
|
||||
|
||||
Generates text using OpenAI's GPT models based on user prompts.
|
||||
|
||||
Usage: gpt [options] [prompt]
|
||||
|
||||
Examples:
|
||||
gpt "Write a haiku about programming" # Generate text from prompt
|
||||
gpt -m gpt-4 "Explain quantum computing" # Use specific model
|
||||
cat prompt.txt | gpt # Read prompt from stdin
|
||||
gpt -t 0.7 "Write creative story" # Adjust temperature
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
import argparse
|
||||
import openai
|
||||
from typing import Optional
|
||||
|
||||
# Default settings
|
||||
DEFAULT_MODEL = "gpt-3.5-turbo"
|
||||
DEFAULT_TEMPERATURE = 0.7
|
||||
DEFAULT_MAX_TOKENS = 500
|
||||
|
||||
|
||||
def get_api_key() -> Optional[str]:
|
||||
"""Get OpenAI API key from environment or config file."""
|
||||
# First check environment variable
|
||||
api_key = os.environ.get("OPENAI_API_KEY")
|
||||
if api_key:
|
||||
return api_key
|
||||
|
||||
# Then check config file
|
||||
config_paths = [
|
||||
os.path.expanduser("~/.config/openai/config.json"),
|
||||
os.path.expanduser("~/.openai/config.json"),
|
||||
]
|
||||
|
||||
for path in config_paths:
|
||||
if os.path.exists(path):
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
config = json.load(f)
|
||||
if "api_key" in config:
|
||||
return config["api_key"]
|
||||
except (json.JSONDecodeError, IOError):
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def generate_text(prompt: str, model: str, temperature: float, max_tokens: int) -> str:
|
||||
"""Generate text using OpenAI's API."""
|
||||
api_key = get_api_key()
|
||||
if not api_key:
|
||||
sys.stderr.write("Error: OpenAI API key not found.\n")
|
||||
sys.stderr.write(
|
||||
"Please set OPENAI_API_KEY environment variable or configure it in ~/.config/openai/config.json\n"
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
client = openai.OpenAI(api_key=api_key)
|
||||
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
model=model,
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a helpful assistant."},
|
||||
{"role": "user", "content": prompt},
|
||||
],
|
||||
temperature=temperature,
|
||||
max_tokens=max_tokens,
|
||||
)
|
||||
content = response.choices[0].message.content
|
||||
return content if content is not None else ""
|
||||
except Exception as e:
|
||||
sys.stderr.write(f"Error: {str(e)}\n")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function to parse arguments and generate text."""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Generate text using OpenAI's GPT models",
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
epilog=__doc__.split("\n\n", 1)[1] if __doc__ else "",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"prompt",
|
||||
nargs="?",
|
||||
help="The prompt for text generation (default: reads from stdin)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-m",
|
||||
"--model",
|
||||
default=DEFAULT_MODEL,
|
||||
help=f"The GPT model to use (default: {DEFAULT_MODEL})",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-t",
|
||||
"--temperature",
|
||||
type=float,
|
||||
default=DEFAULT_TEMPERATURE,
|
||||
help=f"Temperature for text generation (default: {DEFAULT_TEMPERATURE})",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-n",
|
||||
"--max-tokens",
|
||||
type=int,
|
||||
default=DEFAULT_MAX_TOKENS,
|
||||
help=f"Maximum tokens in the response (default: {DEFAULT_MAX_TOKENS})",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-j", "--json", action="store_true", help="Output result as JSON"
|
||||
)
|
||||
parser.add_argument("-v", "--version", action="version", version="gpt 1.0.0")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Read from stdin or argument
|
||||
if args.prompt:
|
||||
prompt = args.prompt
|
||||
else:
|
||||
prompt = sys.stdin.read().strip()
|
||||
if not prompt:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
# Generate text
|
||||
result = generate_text(
|
||||
prompt=prompt,
|
||||
model=args.model,
|
||||
temperature=args.temperature,
|
||||
max_tokens=args.max_tokens,
|
||||
)
|
||||
|
||||
# Output results
|
||||
if args.json:
|
||||
json.dump({"prompt": prompt, "result": result}, sys.stdout)
|
||||
else:
|
||||
print(result)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user