Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,51 @@ pip install shell-gpt
```
By default, ShellGPT uses OpenAI's API and GPT-4 model. You'll need an API key, you can generate one [here](https://beta.openai.com/account/api-keys). You will be prompted for your key which will then be stored in `~/.config/shell_gpt/.sgptrc`. OpenAI API is not free of charge, please refer to the [OpenAI pricing](https://openai.com/pricing) for more information.

### Azure OpenAI Provider
ShellGPT also supports Azure OpenAI provider. To use Azure OpenAI, you need to configure several Azure-specific parameters:

#### 1. Set the Provider
```shell
export OPENAI_PROVIDER=azure-openai
```

#### 2. Configure Azure Resource Endpoint
```shell
export AZURE_RESOURCE_ENDPOINT=https://your-resource.cognitiveservices.azure.com
```

#### 3. Configure Deployment Name
```shell
export AZURE_DEPLOYMENT_NAME=your-deployment-name
```

#### 4. Set API Version
```shell
export API_VERSION=2025-01-01-preview
```

#### 5. Set API Key
```shell
export OPENAI_API_KEY=your_azure_openai_api_key
```

#### Configuration File
You can also set these in your configuration file `~/.config/shell_gpt/.sgptrc`:
```text
OPENAI_PROVIDER=azure-openai
AZURE_RESOURCE_ENDPOINT=https://your-resource.cognitiveservices.azure.com
AZURE_DEPLOYMENT_NAME=your-deployment-name
API_VERSION=2025-01-01-preview
OPENAI_API_KEY=your_azure_openai_api_key
```

#### URL Structure
Azure OpenAI uses a different URL structure than standard OpenAI:
- **Standard OpenAI**: `https://api.openai.com/v1/chat/completions`
- **Azure OpenAI**: Uses the `AzureOpenAI` client which automatically constructs the correct URL format

The Azure OpenAI provider uses the official `AzureOpenAI` client from the OpenAI library, which handles the endpoint, deployment name, and API version automatically.

> [!TIP]
> Alternatively, you can use locally hosted open source models which are available for free. To use local models, you will need to run your own LLM backend server such as [Ollama](https://github.com/ollama/ollama). To set up ShellGPT with Ollama, please follow this comprehensive [guide](https://github.com/TheR1D/shell_gpt/wiki/Ollama).
>
Expand Down Expand Up @@ -508,4 +553,4 @@ ENTRYPOINT ["sgpt"]

## Additional documentation
* [Azure integration](https://github.com/TheR1D/shell_gpt/wiki/Azure)
* [Ollama integration](https://github.com/TheR1D/shell_gpt/wiki/Ollama)
* [Ollama integration](https://github.com/TheR1D/shell_gpt/wiki/Ollama)
199 changes: 199 additions & 0 deletions examples/azure_openai_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#!/usr/bin/env python3
"""
Example script demonstrating ShellGPT with Azure OpenAI provider.

This script shows how to configure and use ShellGPT with Azure OpenAI.
"""

import os
import subprocess
import sys
from pathlib import Path

def setup_azure_openai_config():
"""Set up Azure OpenAI configuration."""
config_dir = Path.home() / ".config" / "shell_gpt"
config_file = config_dir / ".sgptrc"

# Create config directory if it doesn't exist
config_dir.mkdir(parents=True, exist_ok=True)

# Read existing config or create new one
config_lines = []
if config_file.exists():
with open(config_file, 'r') as f:
config_lines = f.readlines()

# Check if OPENAI_PROVIDER is already set
provider_set = any(line.startswith("OPENAI_PROVIDER=") for line in config_lines)

if not provider_set:
config_lines.append("OPENAI_PROVIDER=azure-openai\n")

with open(config_file, 'w') as f:
f.writelines(config_lines)
print(f"✅ Added OPENAI_PROVIDER=azure-openai to {config_file}")
else:
print(f"✅ OPENAI_PROVIDER already configured in {config_file}")


def run_sgpt_example():
"""Run a simple ShellGPT example with Azure OpenAI."""
print("\n🚀 Running ShellGPT example with Azure OpenAI...")
print("Note: Shell commands in interactive mode will prompt for Execute/Describe/Abort")

# Set environment variable for this session
os.environ["OPENAI_PROVIDER"] = "azure-openai"

try:
# Example 1: Simple question
print("\n📝 Example 1: Simple question")
result = subprocess.run(
["sgpt", "What is the capital of France?"],
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0:
print(f"Response: {result.stdout.strip()}")
else:
print(f"Error: {result.stderr.strip()}")

# Example 2: Shell command generation (non-interactive)
print("\n💻 Example 2: Shell command generation (non-interactive)")
print("Using --no-interaction flag to avoid interactive prompts")
result = subprocess.run(
["sgpt", "--shell", "--no-interaction", "list all files in current directory"],
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0:
print(f"Generated command: {result.stdout.strip()}")
else:
print(f"Error: {result.stderr.strip()}")


# Example 3: Code generation
print("\n🐍 Example 3: Code generation")
result = subprocess.run(
["sgpt", "--code", "hello world in python"],
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0:
print(f"Generated code:\n{result.stdout.strip()}")
else:
print(f"Error: {result.stderr.strip()}")

# Example 4: Chat mode
print("\n💬 Example 4: Chat mode")
result = subprocess.run(
["sgpt", "--chat", "test_session", "Remember my name is Alice"],
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0:
print(f"Chat response: {result.stdout.strip()}")
else:
print(f"Error: {result.stderr.strip()}")

except subprocess.TimeoutExpired:
print("❌ Request timed out. Please check your Azure OpenAI configuration.")
except FileNotFoundError:
print("❌ ShellGPT not found. Please install it first: pip install shell-gpt")
except Exception as e:
print(f"❌ Error: {e}")

def check_configuration():
"""Check if Azure OpenAI is properly configured."""
print("🔍 Checking Azure OpenAI Configuration")
print("=" * 50)

# Check if provider is set to azure-openai
if os.getenv("OPENAI_PROVIDER") != "azure-openai":
print("\n⚠️ Warning: OPENAI_PROVIDER not set to azure-openai.")
print(" Please set: export OPENAI_PROVIDER=azure-openai")
print("\n Or add it to your config file:")
print(" echo 'OPENAI_PROVIDER=azure-openai' >> ~/.config/shell_gpt/.sgptrc")

# Check if Azure-specific configuration is set
if not os.getenv("AZURE_RESOURCE_ENDPOINT"):
print("\n⚠️ Warning: AZURE_RESOURCE_ENDPOINT not configured.")
print(" For Azure OpenAI, please set your resource endpoint:")
print(" export AZURE_RESOURCE_ENDPOINT=https://your-resource.cognitiveservices.azure.com")
print("\n Or add it to your config file:")
print(" echo 'AZURE_RESOURCE_ENDPOINT=https://your-resource.cognitiveservices.azure.com' >> ~/.config/shell_gpt/.sgptrc")

if not os.getenv("AZURE_DEPLOYMENT_NAME"):
print("\n⚠️ Warning: AZURE_DEPLOYMENT_NAME not configured.")
print(" For Azure OpenAI, please set your deployment name:")
print(" export AZURE_DEPLOYMENT_NAME=your-deployment-name")
print("\n Or add it to your config file:")
print(" echo 'AZURE_DEPLOYMENT_NAME=your-deployment-name' >> ~/.config/shell_gpt/.sgptrc")

if not os.getenv("API_VERSION"):
print("\n⚠️ Warning: API_VERSION not configured.")
print(" For Azure OpenAI, please set your API version:")
print(" export API_VERSION=2025-01-01-preview")
print("\n Or add it to your config file:")
print(" echo 'API_VERSION=2025-01-01-preview' >> ~/.config/shell_gpt/.sgptrc")

if not os.getenv("OPENAI_API_KEY"):
print("\n⚠️ Warning: OPENAI_API_KEY not configured.")
print(" Please set your Azure OpenAI API key:")
print(" export OPENAI_API_KEY=your_azure_openai_api_key")
print("\n Or add it to your config file:")
print(" echo 'OPENAI_API_KEY=your_azure_openai_api_key' >> ~/.config/shell_gpt/.sgptrc")

# Show example configuration
print("\n📋 Example Azure OpenAI Configuration:")
print(" export OPENAI_PROVIDER=azure-openai")
print(" export AZURE_RESOURCE_ENDPOINT=https://er-biz-svcs-us.cognitiveservices.azure.com")
print(" export AZURE_DEPLOYMENT_NAME=erbizgpt4o")
print(" export API_VERSION=2025-01-01-preview")
print(" export OPENAI_API_KEY=your_azure_openai_api_key")

print("\n🔗 This will use the AzureOpenAI client with:")
print(" - Endpoint: https://er-biz-svcs-us.cognitiveservices.azure.com")
print(" - Deployment: erbizgpt4o")
print(" - API Version: 2025-01-01-preview")
print(" - Model parameter: erbizgpt4o (deployment name)")

def main():
"""Main function."""
print("🔧 ShellGPT Azure OpenAI Provider Example")
print("=" * 50)

# Check if ShellGPT is installed
try:
subprocess.run(["sgpt", "--version"], capture_output=True, check=True)
except (subprocess.CalledProcessError, FileNotFoundError):
print("❌ ShellGPT not found. Please install it first:")
print(" pip install shell-gpt")
sys.exit(1)

# Set up configuration
setup_azure_openai_config()

# Check configuration
check_configuration()

# Run examples
run_sgpt_example()


print("\n✅ Example completed!")
print("\n📚 For more information, see:")
print(" - README.md for detailed usage")
print(" - Azure OpenAI documentation: https://docs.microsoft.com/en-us/azure/cognitive-services/openai/")
print("\n💡 Tips for using ShellGPT with Azure OpenAI:")
print(" - Use --no-interaction for non-interactive shell commands")
print(" - Use --shell for interactive command generation")
print(" - Use --code for pure code generation")
print(" - Use --chat for conversation mode")

if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions sgpt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"SHELL_INTERACTION": os.getenv("SHELL_INTERACTION ", "true"),
"OS_NAME": os.getenv("OS_NAME", "auto"),
"SHELL_NAME": os.getenv("SHELL_NAME", "auto"),
"OPENAI_PROVIDER": os.getenv("OPENAI_PROVIDER", "openai"),
"API_VERSION": os.getenv("API_VERSION", "2024-02-15-preview"),
"AZURE_DEPLOYMENT_NAME": os.getenv("AZURE_DEPLOYMENT_NAME", ""),
"AZURE_RESOURCE_ENDPOINT": os.getenv("AZURE_RESOURCE_ENDPOINT", ""),
# New features might add their own config variables here.
}

Expand Down
Loading