Skip to content

how to use local model #31

@wfxu

Description

@wfxu

I use model qwen for local host,it is trouble for changing model, so i share my code text.

step 1: custom your model

CUSTOM_OPENAI_URL = os.getenv("BASE_MODEL") 
CUSTOM_API_KEY = os.getenv("FEEYO_API_KEY") 
CUSTOM_MODEL = "local/Qwen2.5-32B-Instruct-AWQ"

custom_openai_client = AsyncOpenAI(
    base_url=CUSTOM_OPENAI_URL,
    api_key=CUSTOM_API_KEY
)

set_default_openai_client(client=custom_openai_client, use_for_tracing=False)
set_default_openai_api("chat_completions")
set_tracing_disabled(disabled=True)

step 2: change model

guardrail_agent = Agent(
    model=my_model,
    name="Relevance Guardrail",
    instructions=(
        "Determine if the user's message is highly unrelated to a normal customer service "
        "conversation with an airline (flights, bookings, baggage, check-in, flight status, policies, loyalty programs, etc.). "
        "Important: You are ONLY evaluating the most recent user message, not any of the previous messages from the chat history"
        "It is OK for the customer to send messages such as 'Hi' or 'OK' or any other messages that are at all conversational, "
        "but if the response is non-conversational, it must be somewhat related to airline travel. "
        "Return is_relevant=True if it is, else False, plus a brief reasoning."
    ),
    output_type=RelevanceOutput,
)

step 3: custom model provider

class CustomModelProvider(ModelProvider):
    def get_model(self, model_name: str | None) -> Model:
        return OpenAIChatCompletionsModel(model=model_name or CUSTOM_MODEL, openai_client=custom_openai_client)


CUSTOM_MODEL_PROVIDER = CustomModelProvider()

step 4: change runner model provider

result = await Runner.run(current_agent, state["input_items"], context=state["context"], run_config=RunConfig(model_provider=CUSTOM_MODEL_PROVIDER))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions