feat: 修改drawing 为django 接口

This commit is contained in:
XIE7654
2025-10-31 22:16:55 +08:00
parent e4aa6ad18c
commit e5ec2fec56
15 changed files with 401 additions and 34 deletions

View File

View File

@@ -0,0 +1,17 @@
from langchain_deepseek import ChatDeepSeek
from llm.base import MultiModalAICapability
class DeepSeekAdapter(MultiModalAICapability):
def __init__(self, api_key, model, **kwargs):
self.llm = ChatDeepSeek(api_key=api_key, model=model, streaming=True)
async def chat(self, messages, **kwargs):
# 兼容 DeepSeek 的调用方式
return await self.llm.ainvoke(messages)
async def stream_chat(self, messages, **kwargs):
async for chunk in self.llm.astream(messages):
yield chunk

View File

@@ -0,0 +1,21 @@
# 假设有 google genai sdk
# from google_genai import GenAI
from llm.base import MultiModalAICapability
class GoogleGenAIAdapter(MultiModalAICapability):
def __init__(self, api_key, model, **kwargs):
self.api_key = api_key
self.model = model
# self.llm = GenAI(api_key=api_key, model=model)
async def chat(self, messages, **kwargs):
# return await self.llm.chat(messages)
raise NotImplementedError("Google GenAI chat未实现")
async def stream_chat(self, messages, **kwargs):
# async for chunk in self.llm.stream_chat(messages):
# yield chunk
raise NotImplementedError("Google GenAI stream_chat未实现")
# 其他能力同理

View File

@@ -0,0 +1,25 @@
from llm.base import MultiModalAICapability
from langchain_openai import ChatOpenAI
# from openai import OpenAI # 如需图片/音频/视频等API
class OpenAIAdapter(MultiModalAICapability):
def __init__(self, api_key, model, **kwargs):
self.llm = ChatOpenAI(api_key=api_key, model=model, streaming=True)
self.api_key = api_key
async def chat(self, messages, **kwargs):
return await self.llm.ainvoke(messages)
async def stream_chat(self, messages, **kwargs):
async for chunk in self.llm.astream(messages):
yield chunk
# 如需图片生成DALL·E可实现如下
def create_drawing_task(self, **kwargs):
# 伪代码,需用 openai.Image.create
# import openai
# response = openai.Image.create(api_key=self.api_key, prompt=prompt, ...)
# return response
raise NotImplementedError("OpenAI 图片生成请用 openai.Image.create 实现")
# 其他能力同理

View File

@@ -0,0 +1,44 @@
from langchain_community.chat_models import ChatTongyi
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis
import os
from llm.base import MultiModalAICapability
class TongYiAdapter(MultiModalAICapability):
def __init__(self, api_key, model, **kwargs):
self.api_key = api_key
self.model = model
self.llm = ChatTongyi(api_key=api_key, model=model, streaming=True)
async def chat(self, messages, **kwargs):
# 兼容 DeepSeek 的调用方式
return await self.llm.ainvoke(messages)
async def stream_chat(self, messages, **kwargs):
async for chunk in self.llm.astream(messages):
yield chunk
def create_drawing_task(self, prompt: str, style='watercolor', size='1024*1024', n=1, **kwargs):
"""创建异步图片生成任务"""
rsp = ImageSynthesis.async_call(
api_key=self.api_key,
model=self.model,
prompt=prompt,
n=n,
style=f'<{style}>',
size=size
)
return rsp
def fetch_drawing_task_status(self, task):
"""获取异步图片任务状态"""
rsp = ImageSynthesis.fetch(task, api_key=self.api_key)
return rsp