moved tests

This commit is contained in:
2026-01-27 17:28:07 +08:00
parent 3bb961d6c1
commit 01faaa66a8
3 changed files with 261 additions and 2 deletions

View File

@@ -1,107 +0,0 @@
#!/usr/bin/env python3
"""
Minimal test for DashScope Application.call against server_dashscope.py
Instructions:
- Start the DashScope-compatible server first, e.g.:
uvicorn fastapi_server.server_dashscope:app --host 0.0.0.0 --port 8588 --reload
- Set BASE_URL below to the server base URL you started.
- Optionally set environment variables ALI_API_KEY and ALI_APP_ID.
"""
import os
import uuid
from dotenv import load_dotenv
from loguru import logger
from http import HTTPStatus
TAG = __name__
load_dotenv()
try:
from dashscope import Application
import dashscope
except Exception as e:
print("dashscope package not found. Please install it: pip install dashscope")
raise
# <<< Paste your running FastAPI base url here >>>
BASE_URL = os.getenv("DS_BASE_URL", "http://127.0.0.1:8588/api/")
# Params
API_KEY = os.getenv("FAST_AUTH_KEYS", "test-key")
APP_ID = os.getenv("ALI_APP_ID", "test-app")
SESSION_ID = str(uuid.uuid4())
dialogue = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Say 'the world is awesome and beautiful'."},
]
call_params = {
"api_key": API_KEY,
"app_id": "test_app",
"session_id": "123",
"messages": dialogue,
"stream": True,
}
def main():
# Point the SDK to our FastAPI implementation
if BASE_URL and ("/api/" in BASE_URL):
dashscope.base_http_api_url = BASE_URL
# dashscope.base_http_api_url = BASE_URL
print(f"Using base_http_api_url = {dashscope.base_http_api_url}")
print("\nCalling Application.call(stream=True)...\n")
responses = Application.call(**call_params)
try:
last_text = ""
u = ""
for resp in responses:
if resp.status_code != HTTPStatus.OK:
logger.bind(tag=TAG).error(
f"code={resp.status_code}, message={resp.message}, 请参考文档https://help.aliyun.com/zh/model-studio/developer-reference/error-code"
)
continue
current_text = getattr(getattr(resp, "output", None), "text", None)
if current_text is None:
continue
# SDK流式为增量覆盖计算差量输出
if len(current_text) >= len(last_text):
delta = current_text[len(last_text):]
else:
# 避免偶发回退
delta = current_text
if delta:
u = delta
last_text = current_text
logger.info(f"from stream: {u}")
except TypeError:
# 非流式回落(一次性返回)
if responses.status_code != HTTPStatus.OK:
logger.bind(tag=TAG).error(
f"code={responses.status_code}, message={responses.message}, 请参考文档https://help.aliyun.com/zh/model-studio/developer-reference/error-code"
)
u = "【阿里百练API服务响应异常】"
else:
full_text = getattr(getattr(responses, "output", None), "text", "")
logger.bind(tag=TAG).info(
f"【阿里百练API服务】完整响应长度: {len(full_text)}"
)
u = full_text
print("from non-stream: ", u)
except Exception as e:
logger.bind(tag=TAG).error(f"Error: {e}")
u = "【阿里百练API服务响应异常】"
if __name__ == "__main__":
main()

View File

@@ -1,147 +0,0 @@
#!/usr/bin/env python3
"""
Test for OpenAI-compatible API against server_openai.py
Instructions:
- Start the OpenAI-compatible server first, e.g.:
python fastapi_server/server_openai.py --llm_name qwen-plus --llm_provider openai --base_url https://dashscope.aliyuncs.com/compatible-mode/v1
- Or with uvicorn:
uvicorn fastapi_server.server_openai:app --host 0.0.0.0 --port 8589 --reload
- Set BASE_URL and API_KEY environment variables (or in .env file):
OPENAI_BASE_URL=http://127.0.0.1:8589/v1
API_KEY=sk-your-api-key-here
- Make sure the API_KEY matches one of the keys in the server's API_KEYS environment variable
"""
import os
from dotenv import load_dotenv
from loguru import logger
TAG = __name__
load_dotenv()
try:
from openai import OpenAI
except Exception as e:
print("openai package not found. Please install it: pip install openai")
raise
# <<< Paste your running FastAPI base url here >>>
BASE_URL = os.getenv("OPENAI_BASE_URL", "http://127.0.0.1:8589/v1")
API_KEY = os.getenv("FAST_AUTH_KEYS", None)
# Test configuration matching the server setup
# llm_name: "qwen-plus"
# llm_provider: "openai"
# base_url: "https://dashscope.aliyuncs.com/compatible-mode/v1"
# Test messages
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "use calculator to calculate 1234*5641"},
]
def test_streaming():
"""Test streaming chat completion"""
print("\n" + "="*60)
print("Testing STREAMING chat completion...")
print("="*60 + "\n")
if not API_KEY:
logger.warning("API_KEY not set. Set it in environment variable or .env file.")
raise ValueError("API_KEY environment variable is required for authentication")
client = OpenAI(
base_url=BASE_URL,
api_key=API_KEY
)
try:
stream = client.chat.completions.create(
model="qwen-plus", # Using qwen-plus as configured
messages=messages,
stream=True,
extra_body={"thread_id":"2000"}
)
full_response = ""
for chunk in stream:
if chunk.choices[0].delta.content is not None:
content = chunk.choices[0].delta.content
full_response += content
print(content, end="", flush=True)
print("\n\n" + "-"*60)
print(f"Full streaming response length: {len(full_response)}")
print("-"*60)
return full_response
except Exception as e:
logger.error(f"Streaming test error: {e}")
raise
def test_non_streaming():
"""Test non-streaming chat completion"""
print("\n" + "="*60)
print("Testing NON-STREAMING chat completion...")
print("="*60 + "\n")
if not API_KEY:
logger.warning("API_KEY not set. Set it in environment variable or .env file.")
raise ValueError("API_KEY environment variable is required for authentication")
client = OpenAI(
base_url=BASE_URL,
api_key=API_KEY
)
try:
response = client.chat.completions.create(
model="qwen-plus", # Using qwen-plus as configured
messages=messages,
stream=False,
extra_body={"thread_id":"2000"}
)
content = response.choices[0].message.content
print(f"Response: {content}")
print("\n" + "-"*60)
print(f"Full non-streaming response length: {len(content)}")
print(f"Finish reason: {response.choices[0].finish_reason}")
print("-"*60)
return content
except Exception as e:
logger.error(f"Non-streaming test error: {e}")
raise
def main():
print(f"\nUsing base_url = {BASE_URL}")
if API_KEY:
# Show only first 8 chars for security
masked_key = API_KEY[:8] + "..." if len(API_KEY) > 8 else API_KEY
print(f"Using API_KEY = {masked_key}")
else:
print("WARNING: API_KEY not set!")
print()
# Test both streaming and non-streaming
streaming_result = test_streaming()
non_streaming_result = test_non_streaming()
print("\n" + "="*60)
print("SUMMARY")
print("="*60)
print(f"Streaming response length: {len(streaming_result)}")
print(f"Non-streaming response length: {len(non_streaming_result)}")
print("\nBoth tests completed successfully!")
if __name__ == "__main__":
main()