From ed3e325962c91acf26cdf9c7ed2a6aed7dcebd37 Mon Sep 17 00:00:00 2001 From: XIE7654 <765462425@qq.com> Date: Wed, 16 Jul 2025 14:18:39 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4ai=E5=AF=B9=E8=AF=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/ai/chat.py | 26 -- backend/ai/langchain_client.py | 25 -- backend/ai/routing.py | 7 - backend/backend/asgi.py | 23 +- backend/requirements.txt | 7 +- docker-compose.dev.yml | 2 +- web/apps/web-antd/src/views/ai/chat/index.vue | 285 ------------------ 7 files changed, 13 insertions(+), 362 deletions(-) delete mode 100644 backend/ai/chat.py delete mode 100644 backend/ai/langchain_client.py delete mode 100644 backend/ai/routing.py delete mode 100644 web/apps/web-antd/src/views/ai/chat/index.vue diff --git a/backend/ai/chat.py b/backend/ai/chat.py deleted file mode 100644 index 0431b2e..0000000 --- a/backend/ai/chat.py +++ /dev/null @@ -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})) \ No newline at end of file diff --git a/backend/ai/langchain_client.py b/backend/ai/langchain_client.py deleted file mode 100644 index aa215f6..0000000 --- a/backend/ai/langchain_client.py +++ /dev/null @@ -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)]) \ No newline at end of file diff --git a/backend/ai/routing.py b/backend/ai/routing.py deleted file mode 100644 index ba82f67..0000000 --- a/backend/ai/routing.py +++ /dev/null @@ -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()), -] \ No newline at end of file diff --git a/backend/backend/asgi.py b/backend/backend/asgi.py index 8421d44..6aa1b52 100644 --- a/backend/backend/asgi.py +++ b/backend/backend/asgi.py @@ -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() - ), -}) \ No newline at end of file +application = get_asgi_application() diff --git a/backend/requirements.txt b/backend/requirements.txt index c51b739..0f5b931 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -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 \ No newline at end of file +django-ninja==1.4.3 \ No newline at end of file diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 1a6a3bf..c32c4a4 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -94,7 +94,7 @@ services: command: > sh -c "python manage.py makemigrations && python manage.py migrate && - daphne -b 0.0.0.0 backend.asgi:application" + python manage.py runserver 0.0.0.0:8000" web: build: diff --git a/web/apps/web-antd/src/views/ai/chat/index.vue b/web/apps/web-antd/src/views/ai/chat/index.vue deleted file mode 100644 index 2e32cf5..0000000 --- a/web/apps/web-antd/src/views/ai/chat/index.vue +++ /dev/null @@ -1,285 +0,0 @@ - - - - - - - 🧑: {{ msg.content }} - 🤖: {{ msg.content }} - - AI 正在思考... - - - - 发送 - - - - -