Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| """ | |
| ChatBIA Streaming API ν μ€νΈ ν΄λΌμ΄μΈνΈ | |
| """ | |
| import requests | |
| import json | |
| import sys | |
| import io | |
| if sys.platform == 'win32': | |
| sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') | |
| def test_streaming_chat(base_url: str, message: str, mode: str = "bsl"): | |
| """ | |
| μ€νΈλ¦¬λ° μ±ν ν μ€νΈ | |
| Args: | |
| base_url: API μλ² URL (μ: http://localhost:8000) | |
| message: μ¬μ©μ λ©μμ§ | |
| mode: "bsl" λλ "general" | |
| """ | |
| url = f"{base_url}/chat/stream" | |
| payload = { | |
| "message": message, | |
| "mode": mode, | |
| "max_tokens": 512, | |
| "temperature": 0.7 | |
| } | |
| print(f"π μμ² μ μ‘: {message}") | |
| print(f"π‘ λͺ¨λ: {mode}") | |
| print("=" * 60) | |
| try: | |
| # μ€νΈλ¦¬λ° μμ² | |
| response = requests.post( | |
| url, | |
| json=payload, | |
| stream=True, # μ€μ: μ€νΈλ¦¬λ° νμ±ν | |
| headers={ | |
| "Content-Type": "application/json", | |
| "Accept": "text/event-stream" | |
| }, | |
| timeout=120 # 2λΆ νμμμ (μΆ©λΆν κΈ΄ μκ°) | |
| ) | |
| if response.status_code != 200: | |
| print(f"β μ€λ₯: {response.status_code}") | |
| print(response.text) | |
| return | |
| print("β μλ΅ μμ μ€...\n") | |
| full_text = "" | |
| token_count = 0 | |
| # SSE μ€νΈλ¦Ό μ½κΈ° | |
| for line in response.iter_lines(): | |
| if line: | |
| line_str = line.decode('utf-8') | |
| # SSE νμ: "data: {json}" | |
| if line_str.startswith("data: "): | |
| data_str = line_str[6:] # "data: " μ κ±° | |
| try: | |
| data = json.loads(data_str) | |
| # μλ¬ μ²΄ν¬ | |
| if "error" in data: | |
| print(f"\nβ μλ² μ€λ₯: {data['error']}") | |
| break | |
| # ν ν° μΆλ ₯ | |
| if not data.get("done", False): | |
| token = data.get("token", "") | |
| print(token, end="", flush=True) | |
| full_text += token | |
| token_count = data.get("token_count", token_count) | |
| else: | |
| # μλ£ | |
| print(f"\n\nβ μλ£!") | |
| print(f"π ν ν° μ: {data.get('token_count', token_count)}") | |
| print(f"π― λͺ¨λ: {data.get('mode', mode)}") | |
| break | |
| except json.JSONDecodeError as e: | |
| print(f"\nβ οΈ JSON νμ± μ€λ₯: {e}") | |
| print(f" μλ³Έ: {data_str}") | |
| print("\n" + "=" * 60) | |
| print(f"μ 체 μλ΅ κΈΈμ΄: {len(full_text)} κΈμ") | |
| except requests.exceptions.Timeout: | |
| print("β νμμμ μ€λ₯: μλ² μλ΅μ΄ λ무 λ립λλ€.") | |
| except requests.exceptions.ConnectionError: | |
| print("β μ°κ²° μ€λ₯: μλ²μ μ°κ²°ν μ μμ΅λλ€.") | |
| except Exception as e: | |
| print(f"β μμμΉ λͺ»ν μ€λ₯: {e}") | |
| def test_regular_chat(base_url: str, message: str, mode: str = "bsl"): | |
| """ | |
| μΌλ° (λΉμ€νΈλ¦¬λ°) μ±ν ν μ€νΈ | |
| """ | |
| url = f"{base_url}/chat" | |
| payload = { | |
| "message": message, | |
| "mode": mode, | |
| "max_tokens": 512, | |
| "temperature": 0.7 | |
| } | |
| print(f"π μΌλ° μμ² μ μ‘: {message}") | |
| print(f"π‘ λͺ¨λ: {mode}") | |
| print("=" * 60) | |
| try: | |
| response = requests.post(url, json=payload, timeout=60) | |
| if response.status_code == 200: | |
| result = response.json() | |
| print("β μλ΅:\n") | |
| print(result["response"]) | |
| print(f"\nπ ν ν° μ: {result['tokens']}") | |
| else: | |
| print(f"β μ€λ₯: {response.status_code}") | |
| print(response.text) | |
| except Exception as e: | |
| print(f"β μ€λ₯: {e}") | |
| if __name__ == "__main__": | |
| # λ‘컬 ν μ€νΈ | |
| BASE_URL = "http://localhost:8000" | |
| # Hugging Face Spaces ν μ€νΈ (λ°°ν¬ ν) | |
| # BASE_URL = "https://your-username-chatbia-server.hf.space" | |
| print("=" * 60) | |
| print("ChatBIA Streaming API ν μ€νΈ") | |
| print("=" * 60) | |
| # ν μ€νΈ 1: μ€νΈλ¦¬λ° μ±ν (BSL λͺ¨λ) | |
| print("\n[ν μ€νΈ 1] μ€νΈλ¦¬λ° μ±ν - BSL λͺ¨λ\n") | |
| test_streaming_chat( | |
| BASE_URL, | |
| "5μ²λ§μ μ€λΉμ κ°κ°μκ° κ³μ°ν΄μ€. λ΄μ©μ°μλ 10λ μ΄κ³ μμ‘΄κ°μΉλ 10%μΌ.", | |
| mode="bsl" | |
| ) | |
| print("\n" + "=" * 60 + "\n") | |
| # ν μ€νΈ 2: μ€νΈλ¦¬λ° μ±ν (μΌλ° λͺ¨λ) | |
| print("\n[ν μ€νΈ 2] μ€νΈλ¦¬λ° μ±ν - μΌλ° λͺ¨λ\n") | |
| test_streaming_chat( | |
| BASE_URL, | |
| "νκ³μμ κ°κ°μκ°μ΄ λμΌ?", | |
| mode="general" | |
| ) | |
| print("\n" + "=" * 60 + "\n") | |
| # ν μ€νΈ 3: μΌλ° μ±ν (λΉμ€νΈλ¦¬λ°) - λΉκ΅μ© | |
| print("\n[ν μ€νΈ 3] μΌλ° μ±ν (λΉμ€νΈλ¦¬λ°) - λΉκ΅μ©\n") | |
| test_regular_chat( | |
| BASE_URL, | |
| "μλ νμΈμ!", | |
| mode="bsl" | |
| ) | |