textilindo-ai-assistant / test_novita_simple.py
Stefanus Simandjuntak
initial commit
9b4ef96
#!/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()