Skip to content

Instantly share code, notes, and snippets.

@mrexodia
Created April 18, 2025 21:50
Show Gist options
  • Save mrexodia/c451d506a13aeda8437c8d7b12c69c37 to your computer and use it in GitHub Desktop.
Save mrexodia/c451d506a13aeda8437c8d7b12c69c37 to your computer and use it in GitHub Desktop.
Automatically generate LiteLLM config for all models in LM Studio
import argparse
import json
import urllib.request
import urllib.error
import sys
import yaml
def generate(prefix, model_id, endpoint, api_key):
return {
"model_name": f"{prefix}/{model_id}",
"litellm_params": {
"custom_llm_provider": "openai",
"model": f"{model_id}",
"api_key": api_key,
"api_base": endpoint,
},
}
def main():
parser = argparse.ArgumentParser(description="Generate LiteLLM configuration for LM Studio models")
parser.add_argument("--endpoint", required=True, help="LM Studio API endpoint URL (e.g. http://localhost:1234/v1)")
parser.add_argument("--prefix", required=True, help="Prefix for model names")
parser.add_argument("--api-key", default="sk-litellm", help="API key (default: sk-litellm)")
args = parser.parse_args()
# Make sure endpoint doesn't end with a trailing slash
endpoint = args.endpoint.rstrip('/')
# Request models from LM Studio API
models_url = f"{endpoint}/models"
try:
req = urllib.request.Request(models_url)
response = urllib.request.urlopen(req)
models_data = json.loads(response.read().decode('utf-8'))
if 'data' in models_data:
configurations = []
for model in models_data['data']:
model_id = model.get('id')
if model_id:
config = generate(args.prefix, model_id, args.endpoint, args.api_key)
configurations.append(config)
# Create output structure with model_list
output_data = {"model_list": configurations}
# Print configurations as YAML
print(yaml.dump(output_data, default_flow_style=False, sort_keys=False).strip())
else:
print("Error: Unexpected response format from LM Studio API", file=sys.stderr)
sys.exit(1)
except urllib.error.URLError as e:
print(f"Error connecting to LM Studio API: {e}", file=sys.stderr)
sys.exit(1)
except json.JSONDecodeError:
print("Error: Invalid JSON response from LM Studio API", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment