AgentCoord/backend/AgentCoord/PlanEngine/planOutline_Generator.py
zhaoweijie 6392301833 refactor(LLMAPI): 重构LLM接口以支持新版本OpenAI SDK
- 升级openai依赖至2.x版本并替换旧版SDK调用方式
- 引入OpenAI和AsyncOpenAI客户端实例替代全局配置
- 更新所有聊天完成请求方法以适配新版API格式
- 为异步流式响应处理添加异常捕获和错误提示
- 统一超时时间和最大token数等默认参数设置
- 修复部分变量命名冲突和潜在的空值引用问题
- 添加打印彩色日志的辅助函数避免循环导入问题
2025-11-22 17:01:25 +08:00

99 lines
3.1 KiB
Python

from AgentCoord.util.converter import read_LLM_Completion
from typing import List
from pydantic import BaseModel
import json
PROMPT_PLAN_OUTLINE_GENERATION = """
## Instruction
Based on "Output Format Example", "General Goal", and "Initial Key Object List", output a formatted "Plan_Outline".
## Initial Key Object List (Specify the list of initial key objects available, each initial key object should be the input object of at least one Step)
{InitialObject_List}
## General Goal (Specify the general goal for the collaboration plan)
{General_Goal}
## Output Format Example (Specify the output format)
```json
{{
"Plan_Outline": [
{{
"StepName": "Love Element Brainstorming",
"TaskContent": "Decide the main love element in the love story.",
"InputObject_List": [],
"OutputObject": "Love Element"
}},
{{
"StepName": "Story World Buiding",
"TaskContent": "Build the world setting for the story.",
"InputObject_List": [],
"OutputObject": "World Setting"
}},
{{
"StepName": "Story Outline Drafting",
"TaskContent": "Draft the story outline for the story.",
"InputObject_List": ["Love Element", "World Setting"],
"OutputObject": "Story Outline"
}},
{{
"StepName": "Final Story Writing",
"TaskContent": "Writing the final story.",
"InputObject_List": [],
"OutputObject": "Completed Story"
}}
]
}}
```
## Format Explaination (Explain the Output Format):
StepName: Provide a CONCISE and UNIQUE name for this step, smartly summarize what this step is doing.
TaskContent: Describe the task of the current step.
InputObject_List: The list of the input obejects that will be used in current step.
OutputObject: The name of the final output object of current step.
"""
class Step(BaseModel):
StepName: str
TaskContent: str
InputObject_List: List[str]
OutputObject: str
class PlanOutline(BaseModel):
Plan_Outline: List[Step]
class Config:
extra = "allow"
def generate_PlanOutline(InitialObject_List, General_Goal):
messages = [
{
"role": "system",
"content": "You are a recipe database that outputs recipes in JSON.\n"
f" The JSON object must use the schema: {json.dumps(PlanOutline.model_json_schema(), indent=2)}",
},
{
"role": "system",
"content": PROMPT_PLAN_OUTLINE_GENERATION.format(
InitialObject_List=str(InitialObject_List),
General_Goal=General_Goal,
),
},
]
result = read_LLM_Completion(messages)
if isinstance(result, dict) and "Plan_Outline" in result:
return result["Plan_Outline"]
else:
# 如果格式不正确,返回默认的计划大纲
return [
{
"StepName": "Default Step",
"TaskContent": "Generated default plan step due to format error",
"InputObject_List": [],
"OutputObject": "Default Output"
}
]