66import json
77import logging
88import os
9+ from strenum import StrEnum
910from urllib .parse import urlparse
1011
11- # you can also set https://models.github.ai/inference if you prefer
12+ # you can also set https://api.githubcopilot.com if you prefer
1213# but beware that your taskflows need to reference the correct model id
13- # since the Modeld API uses it's own id schema, use -l with your desired
14+ # since different APIs use their own id schema, use -l with your desired
1415# endpoint to retrieve the correct id names to use for your taskflow
15- COPILOT_API_ENDPOINT = os .getenv ('COPILOT_API_ENDPOINT' , default = 'https://api.githubcopilot.com' )
16+ AI_API_ENDPOINT = os .getenv ('AI_API_ENDPOINT' , default = 'https://models.github.ai/inference' )
17+
18+ # Enumeration of currently supported API endpoints.
19+ class AI_API_ENDPOINT_ENUM (StrEnum ):
20+ AI_API_MODELS_GITHUB = 'models.github.ai'
21+ AI_API_GITHUBCOPILOT = 'api.githubcopilot.com'
22+
1623COPILOT_INTEGRATION_ID = 'vscode-chat'
1724
1825# assume we are >= python 3.9 for our type hints
1926def list_capi_models (token : str ) -> dict [str , dict ]:
2027 """Retrieve a dictionary of available CAPI models"""
2128 models = {}
2229 try :
23- match urlparse (COPILOT_API_ENDPOINT ).netloc :
24- case 'api.githubcopilot.com' :
30+ netloc = urlparse (AI_API_ENDPOINT ).netloc
31+ match netloc :
32+ case AI_API_ENDPOINT_ENUM .AI_API_GITHUBCOPILOT :
2533 models_catalog = 'models'
26- case 'models.github.ai' :
34+ case AI_API_ENDPOINT_ENUM . AI_API_MODELS_GITHUB :
2735 models_catalog = 'catalog/models'
2836 case _:
29- raise ValueError (f"Unsupported Model Endpoint: { COPILOT_API_ENDPOINT } " )
30- r = httpx .get (httpx .URL (COPILOT_API_ENDPOINT ).join (models_catalog ),
37+ raise ValueError (f"Unsupported Model Endpoint: { AI_API_ENDPOINT } " )
38+ r = httpx .get (httpx .URL (AI_API_ENDPOINT ).join (models_catalog ),
3139 headers = {
3240 'Accept' : 'application/json' ,
3341 'Authorization' : f'Bearer { token } ' ,
3442 'Copilot-Integration-Id' : COPILOT_INTEGRATION_ID
3543 })
3644 r .raise_for_status ()
3745 # CAPI vs Models API
38- match urlparse ( COPILOT_API_ENDPOINT ). netloc :
39- case 'api.githubcopilot.com' :
46+ match netloc :
47+ case AI_API_ENDPOINT_ENUM . AI_API_GITHUBCOPILOT :
4048 models_list = r .json ().get ('data' , [])
41- case 'models.github.ai' :
49+ case AI_API_ENDPOINT_ENUM . AI_API_MODELS_GITHUB :
4250 models_list = r .json ()
51+ case _:
52+ raise ValueError (f"Unsupported Model Endpoint: { AI_API_ENDPOINT } " )
4353 for model in models_list :
4454 models [model .get ('id' )] = dict (model )
4555 except httpx .RequestError as e :
@@ -51,17 +61,17 @@ def list_capi_models(token: str) -> dict[str, dict]:
5161 return models
5262
5363def supports_tool_calls (model : str , models : dict ) -> bool :
54- match urlparse (COPILOT_API_ENDPOINT ).netloc :
55- case 'api.githubcopilot.com' :
64+ match urlparse (AI_API_ENDPOINT ).netloc :
65+ case AI_API_ENDPOINT_ENUM . AI_API_GITHUBCOPILOT :
5666 return models .get (model , {}).\
5767 get ('capabilities' , {}).\
5868 get ('supports' , {}).\
5969 get ('tool_calls' , False )
60- case 'models.github.ai' :
70+ case AI_API_ENDPOINT_ENUM . AI_API_MODELS_GITHUB :
6171 return 'tool-calling' in models .get (model , {}).\
6272 get ('capabilities' , [])
6373 case _:
64- raise ValueError (f"Unsupported Model Endpoint: { COPILOT_API_ENDPOINT } " )
74+ raise ValueError (f"Unsupported Model Endpoint: { AI_API_ENDPOINT } " )
6575
6676def list_tool_call_models (token : str ) -> dict [str , dict ]:
6777 models = list_capi_models (token )
0 commit comments