add ai image base

This commit is contained in:
XIE7654
2025-07-21 12:24:11 +08:00
parent 3131480afa
commit 816668530c
12 changed files with 449 additions and 2 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_image_task(self, prompt, **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,51 @@
from langchain_deepseek import ChatDeepSeek
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 = 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
@staticmethod
def create_image_task(api_key, model, prompt: str, style='<watercolor>', size='1024*1024', n=1):
"""创建异步图片生成任务"""
rsp = ImageSynthesis.async_call(
api_key=api_key,
model=model,
prompt=prompt,
n=n,
style=style,
size=size
)
if rsp.status_code == HTTPStatus.OK:
return rsp
else:
raise Exception(f"Failed, status_code: {rsp.status_code}, code: {rsp.code}, message: {rsp.message}")
@staticmethod
def fetch_image_task_status(task):
"""获取异步图片任务状态"""
status = ImageSynthesis.fetch(task)
if status.status_code == HTTPStatus.OK:
return status.output.task_status
else:
raise Exception(f"Failed, status_code: {status.status_code}, code: {status.code}, message: {status.message}")