Spaces:
Build error
Build error
| #!/usr/bin/env python3 | |
| """ | |
| Simple script untuk test koneksi Novita AI dengan endpoint yang benar | |
| """ | |
| import os | |
| import requests | |
| import json | |
| def test_novita_connection(): | |
| """Test koneksi ke Novita AI dengan endpoint yang benar""" | |
| api_key = os.getenv('NOVITA_API_KEY') | |
| if not api_key: | |
| print("β NOVITA_API_KEY tidak ditemukan") | |
| return False | |
| print(f"π API Key: {api_key[:10]}...{api_key[-10:]}") | |
| print("π Testing koneksi ke Novita AI...") | |
| # Use the correct endpoint | |
| base_url = "https://api.novita.ai/openai" | |
| headers = { | |
| "Authorization": f"Bearer {api_key}", | |
| "Content-Type": "application/json" | |
| } | |
| try: | |
| # Test models endpoint | |
| print(f"π Testing: {base_url}/models") | |
| response = requests.get(f"{base_url}/models", headers=headers, timeout=10) | |
| print(f" Status: {response.status_code}") | |
| if response.status_code == 200: | |
| print("β Koneksi berhasil!") | |
| models = response.json() | |
| print(f"π Found {len(models.get('data', []))} models") | |
| return True | |
| else: | |
| print(f"β Error: {response.status_code} - {response.text}") | |
| return False | |
| except Exception as e: | |
| print(f"β Error: {e}") | |
| return False | |
| def test_chat_completion(): | |
| """Test chat completion dengan model sederhana""" | |
| api_key = os.getenv('NOVITA_API_KEY') | |
| if not api_key: | |
| print("β NOVITA_API_KEY tidak ditemukan") | |
| return False | |
| base_url = "https://api.novita.ai/openai" | |
| headers = { | |
| "Authorization": f"Bearer {api_key}", | |
| "Content-Type": "application/json" | |
| } | |
| # Test dengan model yang ringan | |
| payload = { | |
| "model": "meta-llama/llama-3.2-1b-instruct", | |
| "messages": [ | |
| {"role": "user", "content": "Hello! How are you today?"} | |
| ], | |
| "max_tokens": 50, | |
| "temperature": 0.7 | |
| } | |
| try: | |
| print(f"π Testing chat completion...") | |
| response = requests.post(f"{base_url}/chat/completions", headers=headers, json=payload, timeout=30) | |
| print(f" Status: {response.status_code}") | |
| if response.status_code == 200: | |
| result = response.json() | |
| print("β Chat completion berhasil!") | |
| print(f"π Response: {result.get('choices', [{}])[0].get('message', {}).get('content', 'No content')}") | |
| return True | |
| else: | |
| print(f"β Error: {response.status_code} - {response.text}") | |
| return False | |
| except Exception as e: | |
| print(f"β Error: {e}") | |
| return False | |
| def main(): | |
| print("π Novita AI Simple Test") | |
| print("=" * 40) | |
| # Test connection | |
| if test_novita_connection(): | |
| print("\nπ Koneksi berhasil! Sekarang test chat completion...") | |
| # Test chat completion | |
| if test_chat_completion(): | |
| print("\nπ Semua test berhasil! Novita AI siap digunakan.") | |
| print("\nπ Next steps:") | |
| print("1. Gunakan script novita_ai_setup_v2.py untuk fine-tuning") | |
| print("2. Atau gunakan script test_model.py untuk testing") | |
| print("3. Monitor usage di dashboard Novita AI") | |
| else: | |
| print("\nβ οΈ Chat completion gagal, tapi koneksi OK") | |
| else: | |
| print("\nβ Koneksi gagal. Cek API key dan endpoint") | |
| if __name__ == "__main__": | |
| main() | |