|
|
@@ -150,63 +150,6 @@ class FormatterFactory:
|
|
|
return HumanFormatter(notes)
|
|
|
raise ValueError(f"Unsupported format type: {format_type}")
|
|
|
|
|
|
-async def test_product_mongo(main_key_num=3, format_type: str = "json"):
|
|
|
- db_mongo = BaseMongoManager() # 自动初始化
|
|
|
- product = await Product.find_one(Product.basic_info.name == "电线保护套")
|
|
|
- product_name = product.basic_info.name
|
|
|
- # 使用默认配置
|
|
|
- competitor_data = get_competitor_prompt_data(product)
|
|
|
- competitor_desc = get_field_descriptions(CompetitorCrawlData)
|
|
|
- product_info_desc = get_field_descriptions(ProductImageInfo)
|
|
|
- keyword_result_desc = get_field_descriptions(TrafficKeywordResult)
|
|
|
-
|
|
|
- # 定义输出字段描述
|
|
|
- output_fields = {
|
|
|
- "results":{
|
|
|
- "asin": "商品(竞品)编号",
|
|
|
- "main_key": "主要关键词",
|
|
|
- "monthly_searches": "月搜索量",
|
|
|
- "reason": "分析你选出的关键词理由"
|
|
|
- },
|
|
|
- "supplement": "非必填,仅当你觉得有必要时才提供补充说明。"
|
|
|
- }
|
|
|
- # 格式特定的额外说明信息
|
|
|
- notes = {
|
|
|
- # "json": "json 信息时必须",
|
|
|
- # "human": "人类可读信息时必须"
|
|
|
- }
|
|
|
-
|
|
|
- # 生成输出格式
|
|
|
- output_format = format_output(output_fields, format_type, notes)
|
|
|
-
|
|
|
- logger.info(f"competitor_desc {competitor_desc}")
|
|
|
- logger.info(f"product_info_desc {product_info_desc}")
|
|
|
- logger.info(f"keyword_result_desc {keyword_result_desc}")
|
|
|
-
|
|
|
- analyz_main_keyword_template_str = '''
|
|
|
-各个字段说明:
|
|
|
-{desc}
|
|
|
-
|
|
|
-竞品数据:
|
|
|
-{competitor_data}
|
|
|
-----
|
|
|
-我是日本站的亚马逊运营,我在给产品名称为 {product_name} 选主要关键词,以上数据是我从同类竞品的关键词搜索量数据,总共有 {competitor_count} 个竞品数据。
|
|
|
-请帮我分析这些竞品数据,选出搜索量在1万以上的相同关键词作为主要关键词3个。
|
|
|
-如果竞品的搜索量都不足1万,则从排名前十的关键词中筛选 {main_key_num} 个搜索量最大且相关性最强的词。
|
|
|
-建议结合日本的市场和用户习惯来分析关键词。
|
|
|
-请输出以下格式:
|
|
|
-{output_format}
|
|
|
-'''
|
|
|
- text_qa_template = PromptTemplate(analyz_main_keyword_template_str)
|
|
|
- formatted_output = text_qa_template.format(
|
|
|
- desc=(competitor_desc, product_info_desc, keyword_result_desc),
|
|
|
- product_name=product_name,
|
|
|
- competitor_data=competitor_data,
|
|
|
- competitor_count=len(competitor_data),
|
|
|
- output_format=output_format,
|
|
|
- )
|
|
|
- logger.info(formatted_output)
|
|
|
- return formatted_output
|
|
|
|
|
|
class LLMService:
|
|
|
"""LLM服务抽象类"""
|
|
|
@@ -267,11 +210,26 @@ class AnalysisService:
|
|
|
self.llm_service = llm_service
|
|
|
self.db_manager = db_manager
|
|
|
|
|
|
- async def execute_analysis(self, product:Product, format_type: str = "json") -> Union[dict, str]:
|
|
|
+ async def execute_analysis(self, product:Product, format_type: str = "json", dry_run=False) -> tuple[dict, str]:
|
|
|
prompt = await self._prepare_prompt(product, format_type)
|
|
|
- return await self.llm_service.analyze(prompt)
|
|
|
+ logger.info(f"prompt: {prompt}")
|
|
|
+
|
|
|
+ if dry_run:
|
|
|
+ mock_result = {
|
|
|
+ "results": [{
|
|
|
+ "asin": "MOCK_ASIN",
|
|
|
+ "main_key": "MOCK_KEYWORD",
|
|
|
+ "monthly_searches": "1,000",
|
|
|
+ "reason": "模拟分析结果"
|
|
|
+ }],
|
|
|
+ "supplement": "模拟补充信息"
|
|
|
+ }
|
|
|
+ return mock_result, prompt
|
|
|
+
|
|
|
+ analysis_result = await self.llm_service.analyze(prompt)
|
|
|
+ return analysis_result, prompt
|
|
|
|
|
|
- async def _prepare_prompt(self, product: Product, format_type: str, main_key_num=3) -> str:
|
|
|
+ async def _prepare_prompt(self, product: Product, format_type: str = "json", main_key_num: int = 3) -> str:
|
|
|
competitor_data = get_competitor_prompt_data(product)
|
|
|
output_fields = {
|
|
|
"results": {
|