61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
|
|
import os
|
|
import sys
|
|
import json
|
|
from flask import Flask
|
|
|
|
# Add backend directory to path
|
|
sys.path.append(os.path.join(os.getcwd(), 'backend'))
|
|
|
|
from app import create_app, db
|
|
from app.models import Agent, User
|
|
from app.services.integrations.dify import DifyService
|
|
|
|
app = create_app()
|
|
|
|
with app.app_context():
|
|
print("Testing Chat Flow...")
|
|
|
|
# Get Agent
|
|
agent = Agent.query.filter_by(name='人事规章').first()
|
|
if not agent:
|
|
print("Error: Agent '人事规章' not found.")
|
|
sys.exit(1)
|
|
|
|
print(f"Using Agent: {agent.name} (ID: {agent.id})")
|
|
print(f"API Base: {agent.api_base_url}")
|
|
|
|
# Get User (or create dummy)
|
|
user = User.query.first()
|
|
if not user:
|
|
print("Creating dummy user for test...")
|
|
user = User(username='test_chat_user', email='test_chat@example.com', role='user')
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
|
|
print(f"Using User: {user.username} (ID: {user.id})")
|
|
|
|
# Test stream_chat
|
|
print("\n--- Starting Stream Chat ---")
|
|
try:
|
|
query = "请问年假有多少天?"
|
|
generator = DifyService.stream_chat(query, user.id, agent)
|
|
|
|
full_response = ""
|
|
for chunk in generator:
|
|
print(f"Chunk: {chunk.strip()}")
|
|
if chunk.startswith("data: "):
|
|
data = json.loads(chunk[6:])
|
|
if data.get('event') == 'message':
|
|
full_response += data.get('answer', '')
|
|
|
|
print("\n--- Stream Finished ---")
|
|
print(f"Full Response: {full_response}")
|
|
|
|
except Exception as e:
|
|
print(f"\nError during chat: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
print("\nTest Complete.")
|