gitlab-duo / test_client.py
chinazhv's picture
Upload 13 files
205e29d verified
Raw
History Blame Contribute Delete
7.06 kB
#!/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โœจ ๆ‰€ๆœ‰ๆต‹่ฏ•ๅฎŒๆˆ!")