feat(新功能): 项目信息提取/生成接口
fix(修复bug): docs(文档变更): refactor(重构): test(增加测试):
This commit is contained in:
33
app/api/api_extraction_project_info.py
Normal file
33
app/api/api_extraction_project_info.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import logging
|
||||||
|
|
||||||
|
from fastapi import APIRouter, HTTPException
|
||||||
|
|
||||||
|
from app.schemas.project_info_extraction import ProjectInfoExtractionModel
|
||||||
|
from app.schemas.response_template import ResponseModel
|
||||||
|
from app.service.project_info_extraction.service_generate_brand_info import ProjectInfoExtraction
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
logger = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/extraction_project_info")
|
||||||
|
def extraction_project_info(request_data: ProjectInfoExtractionModel):
|
||||||
|
"""
|
||||||
|
通过prompt 提取project_name,role ,gender ,style。
|
||||||
|
创建一个具有以下参数的请求体:
|
||||||
|
- **prompt**:
|
||||||
|
|
||||||
|
示例参数:
|
||||||
|
{
|
||||||
|
"prompt": "海边派对主题的系列设计"
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
logger.info(f"extraction_project_info request item is : @@@@@@:{request_data}")
|
||||||
|
service = ProjectInfoExtraction(request_data)
|
||||||
|
data = service.get_result()
|
||||||
|
logger.info(f"extraction_project_info response @@@@@@:{data}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"extraction_project_info Run Exception @@@@@@:{e}")
|
||||||
|
raise HTTPException(status_code=404, detail=str(e))
|
||||||
|
return ResponseModel(data=data)
|
||||||
@@ -5,16 +5,16 @@ from app.api import api_attribute_retrieve, api_query_image
|
|||||||
from app.api import api_brand_dna
|
from app.api import api_brand_dna
|
||||||
from app.api import api_brighten
|
from app.api import api_brighten
|
||||||
from app.api import api_chat_robot
|
from app.api import api_chat_robot
|
||||||
|
from app.api import api_clothing_seg
|
||||||
from app.api import api_design
|
from app.api import api_design
|
||||||
from app.api import api_design_pre_processing
|
from app.api import api_design_pre_processing
|
||||||
|
from app.api import api_extraction_project_info
|
||||||
from app.api import api_generate_image
|
from app.api import api_generate_image
|
||||||
from app.api import api_image2sketch
|
from app.api import api_image2sketch
|
||||||
from app.api import api_mannequins_edit
|
from app.api import api_mannequins_edit
|
||||||
from app.api import api_pose_transform
|
from app.api import api_pose_transform
|
||||||
from app.api import api_prompt_generation
|
from app.api import api_prompt_generation
|
||||||
from app.api import api_clothing_seg
|
|
||||||
from app.api import api_super_resolution
|
from app.api import api_super_resolution
|
||||||
from app.api import api_recommendation
|
|
||||||
from app.api import api_test
|
from app.api import api_test
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
@@ -36,3 +36,4 @@ router.include_router(api_mannequins_edit.router, tags=['api_mannequins_edit'],
|
|||||||
router.include_router(api_agent_generate_image.router, tags=['api_agent_generate_image'], prefix="/api")
|
router.include_router(api_agent_generate_image.router, tags=['api_agent_generate_image'], prefix="/api")
|
||||||
router.include_router(api_pose_transform.router, tags=['api_pose_transform'], prefix="/api")
|
router.include_router(api_pose_transform.router, tags=['api_pose_transform'], prefix="/api")
|
||||||
router.include_router(api_clothing_seg.router, tags=['api_clothing_seg'], prefix="/api")
|
router.include_router(api_clothing_seg.router, tags=['api_clothing_seg'], prefix="/api")
|
||||||
|
router.include_router(api_extraction_project_info.router, tags=['api_extraction_project_info'], prefix="/api")
|
||||||
|
|||||||
5
app/schemas/project_info_extraction.py
Normal file
5
app/schemas/project_info_extraction.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class ProjectInfoExtractionModel(BaseModel):
|
||||||
|
prompt: str
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
|
||||||
|
from langchain_community.chat_models import ChatTongyi
|
||||||
|
from langchain_core.prompts import PromptTemplate
|
||||||
|
|
||||||
|
from app.schemas.project_info_extraction import ProjectInfoExtractionModel
|
||||||
|
|
||||||
|
|
||||||
|
class ProjectInfoExtraction:
|
||||||
|
def __init__(self, request_data):
|
||||||
|
# llm generate brand info init
|
||||||
|
self.model = ChatTongyi(model="qwen2.5-14b-instruct", api_key="sk-7658298c6b99443c98184a5e634fe6ab")
|
||||||
|
|
||||||
|
self.response_schemas = [
|
||||||
|
ResponseSchema(name="project_name", description="project name."),
|
||||||
|
ResponseSchema(name="role", description="The target role of the project."),
|
||||||
|
ResponseSchema(name="gender", description="The gender targeted by the project."),
|
||||||
|
ResponseSchema(name="style", description="Project style.")
|
||||||
|
]
|
||||||
|
self.output_parser = StructuredOutputParser.from_response_schemas(self.response_schemas)
|
||||||
|
self.format_instructions = self.output_parser.get_format_instructions()
|
||||||
|
self.prompt = PromptTemplate(
|
||||||
|
template="你是一个时装品牌的设计师助理。根据用户输入提取出project_name,role ,gender ,style ."
|
||||||
|
"gender部分请用以下:menswear,womenswear,childrenwear,如果全部都适用即all."
|
||||||
|
"如果没有以上内容,需要你根据用户输入随意发挥.\n{format_instructions}\n{question}",
|
||||||
|
input_variables=["question"],
|
||||||
|
partial_variables={"format_instructions": self.format_instructions}
|
||||||
|
)
|
||||||
|
self._input = self.prompt.format_prompt(question=request_data.prompt)
|
||||||
|
|
||||||
|
self.result_data = {}
|
||||||
|
|
||||||
|
def get_result(self):
|
||||||
|
self.llm_extraction_project_info()
|
||||||
|
return self.result_data
|
||||||
|
|
||||||
|
def llm_extraction_project_info(self):
|
||||||
|
output = self.model(self._input.to_messages())
|
||||||
|
project_info = self.output_parser.parse(output.content)
|
||||||
|
self.result_data = project_info
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
request_data = ProjectInfoExtractionModel(
|
||||||
|
prompt="海边派对主题的系列设计"
|
||||||
|
)
|
||||||
|
service = ProjectInfoExtraction(request_data)
|
||||||
|
print(service.get_result())
|
||||||
Reference in New Issue
Block a user