Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| """ | |
| GitLab Duo Chat โ OpenAI API Proxy ๆต่ฏๅฎขๆท็ซฏ | |
| ============================================== | |
| ๆผ็คบๅฆไฝไฝฟ็จ OpenAI SDK ่ฐ็จ GitLab Duo Chat ไปฃ็ใ | |
| ไฝฟ็จๆนๅผ๏ผ | |
| # 1. ๅ ๅฏๅจไปฃ็ๆๅก | |
| python server.py | |
| # 2. ่ฟ่กๆต่ฏๅฎขๆท็ซฏ | |
| python test_client.py | |
| # ๆ็ดๆฅ็จ curl: | |
| # ่งไธๆน curl ็คบไพ | |
| """ | |
| import json | |
| import sys | |
| import time | |
| import os | |
| # ============================================================ | |
| # ๆนๅผ 1: ไฝฟ็จ OpenAI SDK (ๆจ่) | |
| # ============================================================ | |
| def test_with_openai_sdk(): | |
| """ไฝฟ็จๅฎๆน OpenAI SDK ่ฐ็จไปฃ็""" | |
| try: | |
| from openai import OpenAI | |
| except ImportError: | |
| print("โ ๏ธ ้่ฆๅฎ่ฃ openai: pip install openai") | |
| return False | |
| # ไป็ฏๅขๅ้ๆ้ ็ฝฎ่ฏปๅ | |
| base_url = os.environ.get("OPENAI_BASE_URL", "http://localhost:8080/v1") | |
| api_key = os.environ.get("OPENAI_API_KEY", "gitlab-proxy-key") # ๅฏไธบไปปๆๅผ๏ผๆๅกซๅ ฅๅฎ้ cookie/token | |
| print(f"๐ ่ฟๆฅๅฐ: {base_url}") | |
| client = OpenAI(base_url=base_url, api_key=api_key) | |
| # ---- ๆต่ฏ 1: ๅๅบๆจกๅ ---- | |
| print("\n" + "=" * 50) | |
| print("๐ ๆต่ฏ 1: ๅๅบๅฏ็จๆจกๅ") | |
| print("=" * 50) | |
| try: | |
| models = client.models.list() | |
| for m in models.data: | |
| print(f" โ {m.id} (owned_by: {m.owned_by})") | |
| except Exception as e: | |
| print(f" โ ๅคฑ่ดฅ: {e}") | |
| # ---- ๆต่ฏ 2: ้ๆตๅผๅฏน่ฏ ---- | |
| print("\n" + "=" * 50) | |
| print("๐ฌ ๆต่ฏ 2: ้ๆตๅผๅฏน่ฏ") | |
| print("=" * 50) | |
| try: | |
| response = client.chat.completions.create( | |
| model="claude-opus-4.8", | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful assistant."}, | |
| {"role": "user", "content": "What is 2+2? Reply with just the number."}, | |
| ], | |
| stream=False, | |
| ) | |
| msg = response.choices[0].message | |
| print(f" ๐ค ๅๅค: {msg.content}") | |
| print(f" ๐ Token ไฝฟ็จ: prompt={response.usage.prompt_tokens}, completion={response.usage.completion_tokens}") | |
| print(f" ๐ ๅฎๆๅๅ : {response.choices[0].finish_reason}") | |
| print(f" ๐ ๆจกๅ: {response.model}") | |
| except Exception as e: | |
| print(f" โ ๅคฑ่ดฅ: {e}") | |
| # ---- ๆต่ฏ 3: ๆตๅผๅฏน่ฏ ---- | |
| print("\n" + "=" * 50) | |
| print("๐ ๆต่ฏ 3: ๆตๅผๅฏน่ฏ (SSE)") | |
| print("=" * 50) | |
| try: | |
| stream = client.chat.completions.create( | |
| model="claude-opus-4.8", | |
| messages=[ | |
| {"role": "user", "content": "Count from 1 to 5, one per line."}, | |
| ], | |
| stream=True, | |
| ) | |
| print(" ๐ก ๆตๅผ่พๅบ:") | |
| full_content = [] | |
| for chunk in stream: | |
| delta = chunk.choices[0].delta | |
| content = delta.content or "" | |
| if content: | |
| print(f" {content}", end="", flush=True) | |
| full_content.append(content) | |
| print(f"\n\n โ ๅฎๆ! ๆป้ฟๅบฆ: {len(''.join(full_content))} ๅญ็ฌฆ") | |
| except Exception as e: | |
| print(f" โ ๅคฑ่ดฅ: {e}") | |
| return True | |
| # ============================================================ | |
| # ๆนๅผ 2: ไฝฟ็จ httpx / requests (ๅ็ HTTP) | |
| # ============================================================ | |
| def test_with_httpx(): | |
| """ไฝฟ็จๅ็ HTTP ่ฐ็จ๏ผๆ ้ openai SDK๏ผ""" | |
| try: | |
| import httpx | |
| except ImportError: | |
| print("โ ๏ธ ้่ฆๅฎ่ฃ httpx: pip install httpx") | |
| return False | |
| base_url = os.environ.get("PROXY_URL", "http://localhost:8080").rstrip("/") | |
| print(f"\n{'=' * 50}") | |
| print(f"๐ง ๅ็ HTTP ๆต่ฏ ({base_url})") | |
| print("=" * 50) | |
| # ๆตๅผ่ฏทๆฑ็คบไพ | |
| print("\n๐ ๆตๅผ่ฏทๆฑ:") | |
| with httpx.stream( | |
| "POST", | |
| f"{base_url}/v1/chat/completions", | |
| json={ | |
| "model": "claude-opus-4.8", | |
| "messages": [{"role": "user", "content": "Say hello in Chinese."}], | |
| "stream": True, | |
| }, | |
| timeout=120, | |
| ) as resp: | |
| print(f" Status: {resp.status_code}") | |
| for line in resp.iter_lines(): | |
| if line.startswith("data: ") and "[DONE]" not in line: | |
| data = json.loads(line[6:]) | |
| content = data.get("choices", [{}])[0].get("delta", {}).get("content", "") | |
| if content: | |
| print(f" {content}", end="", flush=True) | |
| elif "[DONE]" in line: | |
| print("\n โ [DONE]") | |
| return True | |
| # ============================================================ | |
| # ๆนๅผ 3: curl ๅฝไปคๅ่ | |
| # ============================================================ | |
| def print_curl_examples(): | |
| """ๆๅฐๅฏ็ดๆฅไฝฟ็จ็ curl ๅฝไปค""" | |
| proxy_url = os.environ.get("PROXY_URL", "http://localhost:8080") | |
| print("\n" + "=" * 60) | |
| print("๐ curl ๅฝไปคๅ่") | |
| print("=" * 60) | |
| print(""" | |
| # ===== 1. ๅๅบๆจกๅ ===== | |
| curl {proxy_url}/v1/models \\ | |
| -H "Authorization: Bearer any-token" | |
| # ===== 2. ้ๆตๅผๅฏน่ฏ ===== | |
| curl {proxy_url}/v1/chat/completions \\ | |
| -H "Content-Type: application/json" \\ | |
| -H "Authorization: Bearer your-cookie-or-token" \\ | |
| -d '{{ | |
| "model": "claude-opus-4.8", | |
| "messages": [{{"role": "user", "content": "Hello!"}}], | |
| "stream": false | |
| }}' | |
| # ===== 3. ๆตๅผๅฏน่ฏ (SSE) ===== | |
| curl {proxy_url}/v1/chat/completions \\ | |
| -H "Content-Type: application/json" \\ | |
| -H "Authorization: Bearer your-cookie-or-token" \\ | |
| -d '{{ | |
| "model": "claude-opus-4.8", | |
| "messages": [{{"role": "user", "content": "Write a haiku."}}], | |
| "stream": true | |
| }}' | |
| # ===== 4. ๅๆข่ดฆๅท (่ฟ่กๆถ) ===== | |
| curl {proxy_url}/v1/accounts/switch \\ | |
| -H "Content-Type: application/json" \\ | |
| -d '{{"auth_type": "cookie", "auth_value": "_gitlab_session=NEW_SESSION"}}' | |
| # ===== 5. ๆฅ็ๅฝๅ่ดฆๅทไฟกๆฏ ===== | |
| curl {proxy_url}/v1/accounts/info \\ | |
| -H "Authorization: Bearer any-token" | |
| # ===== 6. ๅฅๅบทๆฃๆฅ ===== | |
| curl {proxy_url}/health | |
| """.format(proxy_url=proxy_url)) | |
| # ============================================================ | |
| # Main | |
| # ============================================================ | |
| if __name__ == "__main__": | |
| print("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ") | |
| print("โ GitLab Duo Chat โ OpenAI API ๆต่ฏๅฎขๆท็ซฏ โ") | |
| print("โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ") | |
| # Print curl examples always | |
| print_curl_examples() | |
| # Run tests based on args | |
| mode = sys.argv[1] if len(sys.argv) > 1 else "all" | |
| if mode in ("all", "sdk"): | |
| test_with_openai_sdk() | |
| if mode in ("all", "http"): | |
| test_with_httpx() | |
| print("\nโจ ๆๆๆต่ฏๅฎๆ!") | |