|
import os |
|
import yaml |
|
import requests |
|
import logging |
|
|
|
# Logging configuration |
|
logging.basicConfig( |
|
level=logging.DEBUG, |
|
format='%(asctime)s - %(levelname)s - %(message)s' |
|
) |
|
logger = logging.getLogger(__name__) |
|
|
|
# Get API key from environment variable |
|
logger.info("Getting API key from environment variable...") |
|
api_key = os.getenv('TOGETHERAI_API_KEY') |
|
if not api_key: |
|
logger.error("TOGETHERAI_API_KEY environment variable is not set") |
|
raise ValueError("TOGETHERAI_API_KEY environment variable is not set") |
|
logger.debug(f"API key: {api_key[:4]}...{api_key[-4:]}") |
|
|
|
# API endpoint |
|
url = "https://api.together.xyz/v1/models" |
|
logger.info(f"API endpoint: {url}") |
|
|
|
# headers |
|
headers = { |
|
"accept": "application/json", |
|
"Authorization": f"Bearer {api_key}" |
|
} |
|
logger.debug(f"Request headers: {headers}") |
|
|
|
# make request |
|
logger.info("Sending API request...") |
|
try: |
|
response = requests.get(url, headers=headers) |
|
response.raise_for_status() |
|
logger.info(f"API response received: status code {response.status_code}") |
|
except requests.exceptions.RequestException as e: |
|
logger.error(f"API request failed: {str(e)}") |
|
raise |
|
|
|
# parse JSON response |
|
logger.info("Parsing JSON response...") |
|
try: |
|
data = response.json() |
|
logger.debug(f"Received data: {data[:100]}...") # Display first 100 characters only |
|
except ValueError as e: |
|
logger.error(f"JSON parsing failed: {str(e)}") |
|
raise |
|
|
|
# extract an ordered list of unique model IDs |
|
logger.info("Extracting chat model IDs...") |
|
try: |
|
model_ids = sorted( |
|
[ |
|
model['id'] |
|
for model in data |
|
if model['type'] == 'chat' |
|
] |
|
) |
|
logger.info(f"Number of models extracted: {len(model_ids)}") |
|
logger.debug(f"Model ID list: {model_ids}") |
|
except (KeyError, TypeError) as e: |
|
logger.error(f"Model ID extraction failed: {str(e)}") |
|
raise |
|
|
|
logger.info("Creating YAML configuration data...") |
|
yaml_data = { |
|
'name': 'together.ai', |
|
'apiKey': '${TOGETHERAI_API_KEY}', |
|
'baseURL': 'https://api.together.xyz', |
|
'models': { |
|
'default': model_ids, |
|
'fetch': False |
|
}, |
|
'titleConvo': True, |
|
'titleModel': 'togethercomputer/llama-2-7b-chat', |
|
'summarize': False, |
|
'summaryModel': 'togethercomputer/llama-2-7b-chat', |
|
'forcePrompt': False, |
|
'modelDisplayLabel': 'together.ai' |
|
} |
|
logger.debug(f"Created YAML data structure: {yaml_data}") |
|
|
|
# Output in YAML format |
|
logger.info("Converting to YAML format and outputting...") |
|
try: |
|
yaml_output = yaml.dump(yaml_data, sort_keys=False, default_flow_style=False) |
|
print(yaml_output) |
|
logger.info("Processing complete") |
|
except yaml.YAMLError as e: |
|
logger.error(f"YAML conversion failed: {str(e)}") |
|
raise |