feat: openai_url key factory

This commit is contained in:
ai-business-hql
2025-07-31 19:53:56 +08:00
parent 272f943655
commit 2db8899ac8
5 changed files with 73 additions and 50 deletions
+37
View File
@@ -0,0 +1,37 @@
from agents import Agent, OpenAIChatCompletionsModel
from dotenv import dotenv_values
from openai import AsyncOpenAI
import os
from agents._config import set_default_openai_api
from agents.tracing import set_tracing_disabled
def load_env_config():
"""Load environment variables from .env.llm file"""
from dotenv import load_dotenv
env_file_path = os.path.join(os.path.dirname(__file__), '.env.llm')
if os.path.exists(env_file_path):
# load_dotenv(env_file_path)
print(f"Loaded environment variables from {env_file_path}")
return dotenv_values(env_file_path)
else:
print(f"Warning: .env.llm not found at {env_file_path}")
# Load environment configuration
load_env_config()
set_default_openai_api("chat_completions")
set_tracing_disabled(True)
def create_agent(**kwargs) -> Agent:
# 通过用户配置拿/环境变量
client = AsyncOpenAI(
api_key="aib_ComfyUI_Copilot_8d28a3",
base_url="https://comfyui-copilot-server-pre.onrender.com/v1",
)
model_name = os.environ.get("OPENAI_MODEL", "gemini-2.5-flash")
model = OpenAIChatCompletionsModel(model_name, openai_client=client)
return Agent(model=model, **kwargs)
+23
View File
@@ -0,0 +1,23 @@
import os
from agents._config import set_default_openai_api
from agents.tracing import set_tracing_disabled
def load_env_config():
"""Load environment variables from .env.llm file"""
from dotenv import load_dotenv
env_file_path = os.path.join(os.path.dirname(__file__), '.env.llm')
if os.path.exists(env_file_path):
load_dotenv(env_file_path)
print(f"Loaded environment variables from {env_file_path}")
else:
print(f"Warning: .env.llm not found at {env_file_path}")
# Load environment configuration
load_env_config()
set_default_openai_api("chat_completions")
set_tracing_disabled(True)
+7 -27
View File
@@ -1,16 +1,11 @@
'''
Debug Agent for ComfyUI Workflow Error Analysis
'''
import os
import json
import requests
from typing import List, Dict, Any, Optional
from agents._config import set_default_openai_api
from agents.agent import Agent
from ..agent_factory import create_agent
from .. import core
from agents.items import ItemHelpers
from agents.run import Runner
from agents.tool import function_tool
from agents.tracing import set_tracing_disabled
from ..utils.globals import get_language
from ..service.workflow_rewrite_tools import *
from openai.types.responses import ResponseTextDeltaEvent
@@ -24,22 +19,7 @@ import uuid
import execution
# Load environment variables from server.env
def load_env_config():
"""Load environment variables from .env.llm file"""
from dotenv import load_dotenv
env_file_path = os.path.join(os.path.dirname(__file__), '.env.llm')
if os.path.exists(env_file_path):
load_dotenv(env_file_path)
print(f"Loaded environment variables from {env_file_path}")
else:
print(f"Warning: .env.llm not found at {env_file_path}")
# Load environment configuration
load_env_config()
set_default_openai_api("chat_completions")
set_tracing_disabled(True)
@function_tool
async def run_workflow(session_id: str) -> str:
@@ -219,7 +199,7 @@ async def debug_workflow_errors(workflow_data: Dict[str, Any], config: Dict[str,
)
print(f"Workflow saved with version ID: {save_result}")
agent = Agent(
agent = create_agent(
name="ComfyUI-Debug-Coordinator",
instructions=f"""You are a ComfyUI workflow debugging coordinator. Your role is to analyze workflow errors and coordinate with specialized agents to fix them.
@@ -227,7 +207,7 @@ async def debug_workflow_errors(workflow_data: Dict[str, Any], config: Dict[str,
**Your Process:**
1. **Validate the workflow**: Use run_workflow("{session_id}") to validate the workflow and capture any errors
2. **Analyze errors**: If errors occur, use analyze_error_type() to determine the error type and hand off to the appropriate specialist:
2. **Analyze errors**: If errors occur, use analyze_error_type() to determine the error type and hand off to the appropriate specialist. Note that analyze_error_type can help you determine the error type and which agent to hand off to, but it's only for reference. You still need to judge based on the current error information to determine which type of error it is:
- Hand off to Link Agent for connection-related errors (missing connections, disconnected inputs, node linking issues)
- Hand off to Parameter Agent for parameter-related errors (value_not_in_list, missing models, invalid values)
- Hand off to Workflow Bugfix Default Agent for other structural issues (node compatibility, complex workflow restructuring)
@@ -257,7 +237,7 @@ Start by validating the workflow to see its current state.""",
tools=[run_workflow, analyze_error_type, save_current_workflow],
)
workflow_bugfix_default_agent = Agent(
workflow_bugfix_default_agent = create_agent(
name="Workflow Bugfix Default Agent",
model="us.anthropic.claude-sonnet-4-20250514-v1:0",
handoff_description="""
@@ -303,7 +283,7 @@ Start by validating the workflow to see its current state.""",
handoffs=[agent],
)
link_agent = Agent(
link_agent = create_agent(
name="Link Agent",
model="us.anthropic.claude-sonnet-4-20250514-v1:0",
handoff_description="""
@@ -390,7 +370,7 @@ Start by validating the workflow to see its current state.""",
handoffs=[agent],
)
parameter_agent = Agent(
parameter_agent = create_agent(
name="Parameter Agent",
model="us.anthropic.claude-sonnet-4-20250514-v1:0",
handoff_description="""
+4 -22
View File
@@ -6,6 +6,7 @@ LastEditTime: 2025-07-31 16:01:57
FilePath: /comfyui_copilot/backend/service/mcp-client.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
from .. import core
import asyncio
import os
import traceback
@@ -17,31 +18,12 @@ from agents.items import ItemHelpers
from agents.mcp import MCPServerSse
from agents.run import Runner
from agents.tracing import set_tracing_disabled
from ..agent_factory import create_agent
from ..service.workflow_rewrite_agent import create_workflow_rewrite_agent
from openai.types.responses import ResponseTextDeltaEvent
from openai import APIError
# Load environment variables from server.env
def load_env_config():
"""Load environment variables from .env.llm file"""
from dotenv import load_dotenv
env_file_path = os.path.join(os.path.dirname(__file__), '.env.llm')
if os.path.exists(env_file_path):
load_dotenv(env_file_path)
print(f"Loaded environment variables from {env_file_path}")
else:
print(f"Warning: .env.llm not found at {env_file_path}")
# Load environment configuration
load_env_config()
set_default_openai_api("chat_completions")
set_tracing_disabled(True)
# @function_tool
# def get_weather(city: str) -> str:
# return f"The weather in {city} is sunny."
class ImageData:
"""Image data structure to match reference implementation"""
@@ -88,7 +70,7 @@ async def comfyui_agent_invoke(messages: List[Dict[str, Any]], images: List[Imag
# 创建带有session_id的workflow_rewrite_agent实例
workflow_rewrite_agent_instance = create_workflow_rewrite_agent(session_id)
agent = Agent(
agent = create_agent(
name="ComfyUI-Copilot",
instructions=f"""You are a powerful AI assistant for designing image processing workflows, capable of automating problem-solving using tools and commands.
+2 -1
View File
@@ -12,6 +12,7 @@ import json
import time
import uuid
from ..agent_factory import create_agent
from ..utils.globals import get_language
from ..service.workflow_rewrite_tools import *
@@ -21,7 +22,7 @@ def create_workflow_rewrite_agent(session_id: str):
"""创建带有session_id的workflow_rewrite_agent实例"""
language = get_language()
return Agent(
return create_agent(
name="Workflow Rewrite Agent",
model="us.anthropic.claude-sonnet-4-20250514-v1:0",
handoff_description="""