MCPcopy Create free account
hub / github.com/pgadmin-org/pgadmin4 / _fetch_ollama_models

Function _fetch_ollama_models

web/pgadmin/llm/__init__.py:924–980  ·  view source on GitHub ↗

Fetch models from Ollama API. Returns a list of model options with label and value.

(api_url)

Source from the content-addressed store, hash-verified

922
923
924def _fetch_ollama_models(api_url):
925 """
926 Fetch models from Ollama API.
927 Returns a list of model options with label and value.
928 """
929 import urllib.request
930 import urllib.error
931 from pgadmin.llm.utils import validate_api_url
932
933 # Normalize URL
934 api_url = api_url.rstrip('/')
935
936 if not validate_api_url(api_url):
937 raise LLMApiError(
938 'API URL is not in the allowed list. '
939 'Check the ALLOWED_LLM_API_URLS configuration.'
940 )
941
942 url = f'{api_url}/api/tags'
943
944 req = urllib.request.Request(url)
945
946 try:
947 with urllib.request.urlopen(
948 req, timeout=30, context=SSL_CONTEXT
949 ) as response:
950 data = json.loads(response.read().decode('utf-8'))
951 except urllib.error.URLError as e:
952 raise LLMApiError(
953 f'Cannot connect to Ollama: {e.reason}'
954 )
955 except OSError:
956 raise LLMApiError(
957 'Cannot connect to Ollama'
958 )
959
960 models = []
961 for model in data.get('models', []):
962 name = model.get('name', '')
963 if name:
964 # Format size if available
965 size = model.get('size', 0)
966 if size:
967 size_gb = size / (1024 ** 3)
968 label = f"{name} ({size_gb:.1f} GB)"
969 else:
970 label = name
971
972 models.append({
973 'label': label,
974 'value': name
975 })
976
977 # Sort alphabetically
978 models.sort(key=lambda x: x['value'])
979
980 return models
981

Callers 2

get_ollama_modelsFunction · 0.85
refresh_ollama_modelsFunction · 0.85

Calls 5

validate_api_urlFunction · 0.90
LLMApiErrorClass · 0.90
decodeMethod · 0.80
appendMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected