移除ai对话

This commit is contained in:
XIE7654
2025-07-16 14:18:39 +08:00
parent 3fe272ffca
commit ed3e325962
7 changed files with 13 additions and 362 deletions

View File

@@ -1,26 +0,0 @@
from channels.generic.websocket import AsyncWebsocketConsumer
import json
from ai.langchain_client import get_ai_reply_stream
from ai.utils import get_first_available_ai_config
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
async def disconnect(self, close_code):
pass
async def receive(self, text_data):
data = json.loads(text_data)
user_message = data.get("message", "")
model, api_key, api_base = await get_first_available_ai_config()
async def send_chunk(chunk):
await self.send(text_data=json.dumps({"is_streaming": True, "message": chunk}))
await get_ai_reply_stream(user_message, send_chunk, model_name=model, api_key=api_key, api_base=api_base)
# 结束标记
await self.send(text_data=json.dumps({"done": True}))

View File

@@ -1,25 +0,0 @@
from langchain.schema import HumanMessage
from langchain_core.callbacks import AsyncCallbackHandler
from langchain_community.chat_models import ChatOpenAI
class MyHandler(AsyncCallbackHandler):
def __init__(self, send_func):
super().__init__()
self.send_func = send_func
async def on_llm_new_token(self, token: str, **kwargs):
await self.send_func(token)
async def get_ai_reply_stream(message: str, send_func, api_key, api_base, model_name):
# 实例化时就带回调
chat = ChatOpenAI(
openai_api_key=api_key,
openai_api_base=api_base,
model_name=model_name,
temperature=0.7,
streaming=True,
callbacks=[MyHandler(send_func)]
)
await chat.ainvoke([HumanMessage(content=message)])

View File

@@ -1,7 +0,0 @@
from django.urls import re_path
from ai.chat import ChatConsumer
websocket_urlpatterns = [
re_path(r'ws/chat/$', ChatConsumer.as_asgi()),
]

View File

@@ -1,17 +1,16 @@
"""
ASGI config for backend project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/
"""
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
# 延迟导入,避免 AppRegistryNotReady 错误
def get_websocket_urlpatterns():
from ai.routing import websocket_urlpatterns
return websocket_urlpatterns
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": URLRouter(
get_websocket_urlpatterns()
),
})
application = get_asgi_application()

View File

@@ -14,9 +14,4 @@ goofish_api==0.0.6
flower==2.0.1
gunicorn==23.0.0
django_redis==6.0.0
django-ninja==1.4.3
openai==1.95
daphne==4.2.1
langchain==0.3.26
langchain-community==0.3.27
channels==4.2.2
django-ninja==1.4.3