95 lines
3.0 KiB
Python
95 lines
3.0 KiB
Python
from fastapi.testclient import TestClient
|
|
|
|
from yuqei_ai_platform_api.config import AiPlatformSettings
|
|
from yuqei_ai_platform_api.main import create_app
|
|
from yuqei_ai_platform_api.repository import InMemoryAiPlatformRepository
|
|
|
|
|
|
def make_client() -> TestClient:
|
|
repository = InMemoryAiPlatformRepository()
|
|
repository.seed_defaults()
|
|
app = create_app(AiPlatformSettings(environment="test"), repository=repository)
|
|
return TestClient(app)
|
|
|
|
|
|
def test_model_config_can_be_hot_updated_and_listed() -> None:
|
|
client = make_client()
|
|
|
|
response = client.post(
|
|
"/api/v1/model-configs",
|
|
json={
|
|
"provider": "openai-compatible",
|
|
"model": "legal-pro",
|
|
"display_name": "Legal Pro",
|
|
"base_url": "https://ai.example.test/v1",
|
|
"temperature": 0.1,
|
|
"max_tokens": 8192,
|
|
"enabled": True,
|
|
"is_default": True,
|
|
},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
created = response.json()
|
|
assert created["model"] == "legal-pro"
|
|
assert created["is_default"] is True
|
|
|
|
list_response = client.get("/api/v1/model-configs")
|
|
assert list_response.status_code == 200
|
|
configs = list_response.json()
|
|
assert configs[0]["model"] == "legal-pro"
|
|
assert sum(1 for config in configs if config["is_default"]) == 1
|
|
|
|
|
|
def test_knowledge_document_can_be_added_and_searched() -> None:
|
|
client = make_client()
|
|
|
|
add_response = client.post(
|
|
"/api/v1/knowledge-documents",
|
|
json={
|
|
"knowledge_base_id": "enterprise",
|
|
"title": "公司租赁审查规范",
|
|
"source_type": "internal_policy",
|
|
"reference": "租赁-001",
|
|
"content": "房屋租赁合同必须明确押金返还条件。",
|
|
},
|
|
)
|
|
assert add_response.status_code == 200
|
|
|
|
search_response = client.get("/api/v1/knowledge-documents/search", params={"keyword": "押金"})
|
|
assert search_response.status_code == 200
|
|
assert search_response.json()[0]["title"] == "公司租赁审查规范"
|
|
|
|
|
|
def test_legal_qa_uses_knowledge_and_records_audit() -> None:
|
|
client = make_client()
|
|
client.post(
|
|
"/api/v1/knowledge-documents",
|
|
json={
|
|
"knowledge_base_id": "laws-cn",
|
|
"title": "中华人民共和国劳动合同法",
|
|
"source_type": "law",
|
|
"reference": "第十九条",
|
|
"content": "试用期不得超过法定上限。",
|
|
},
|
|
)
|
|
|
|
response = client.post(
|
|
"/api/v1/legal/qa",
|
|
json={
|
|
"question": "劳动合同试用期怎么约定?",
|
|
"knowledge_base_ids": ["laws-cn"],
|
|
},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
payload = response.json()
|
|
assert payload["citations"][0]["reference"] == "第十九条"
|
|
|
|
audit_response = client.get("/api/v1/audit/ai-runs")
|
|
assert audit_response.status_code == 200
|
|
audits = audit_response.json()
|
|
assert audits[0]["operation"] == "legal_qa"
|
|
assert audits[0]["citations_count"] >= 1
|
|
assert audits[0]["status"] == "succeeded"
|