【openai 智能体框架适配国产大模型】

from agents import Agent, Runner, OpenAIChatCompletionsModel, ModelSettings,set_default_openai_client
from openai import AsyncOpenAI
import os
import logging

# Disable OpenAI telemetry/tracing completely
os.environ["OPENAI_TELEMETRY"] = "false"
os.environ["OPENAI_TRACING_ENABLED"] = "false"
os.environ["OPENAI_LOG_LEVEL"] = "ERROR"  # Only show ERROR level logs

# Disable warning outputs
logging.getLogger("openai").setLevel(logging.ERROR)
from openai import AsyncOpenAI
external_client = AsyncOpenAI(
    api_key="你的阿里qwen的key",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    # Additional parameter to disable telemetry
    default_headers={
    
    "x-stainless-telemetry": "false"}
)



agent = Agent(
    name="Assistant", 
    instructions="You are a helpful assistant",
    model=OpenAIChatCompletionsModel(
        model="qwen-max",
        openai_client=external_client,
    ),
    model_settings=ModelSettings(temperature=0.5),
)

result = Runner.run_sync(agent, "写个笑话")
print(result.final_output)

# Code within the code,