Skip to content

how to force use tool and send the result to llm? #281

Closed
@moseshu

Description

@moseshu

question

I hope that when calling the book agent, the tool of this agent must be called and the result of the tool must be sent to the LLM. The code below to execute. Sometimes the book agent does not execute the tool. If the tool_choise is set to 'required', it will fall into an infinite loop. If it is set to None, the tool may not be executed. How can I solve this problem?

# book agent
book_agent = Agent(
    name="Book Agent",
    instructions="""
    You are a professional book content consultant. Your tasks are:
1. You must use the search_book_content tool to search for relevant content
2. Answer the user's question based on the search results
3. If the search results are not sufficient to answer the question, make it clear
    """,
    tools=[search_book_content],
    model=OpenAIChatCompletionsModel(
        model="gpt-4o-mini-2024-07-18",
        openai_client=openai_client,
    ),
     tool_use_behavior='run_llm_again',
     model_settings=ModelSettings(temperature=0.7,max_tokens=8192, tool_choice=None),
    hooks=CustomAgentHooks(display_name="Book Agent")
)
#
chat_agent = Agent(
    name="Chat Agent",
    instructions="""
   You are a friendly chat assistant who is responsible for daily conversations with users.
Please respond to users' questions in a natural and friendly tone.
Remember to keep the conversation coherent and interesting.
    """,
    model=OpenAIChatCompletionsModel(
        model="gpt-4o-mini-2024-07-18",
        openai_client=openai_client
    ),
    model_settings=ModelSettings(temperature=0.7),
    hooks=CustomAgentHooks(display_name="Chat Agent")
)

to_book_agent = handoff(
    agent=book_agent,
    tool_name_override="handle_book_query",
    tool_description_override="Handling book-related inquiries",
    on_handoff=on_handoff,
    input_type=EscalationData,
)

to_chat_agent = handoff(
    agent=chat_agent,
    tool_name_override="handle_chat",
    tool_description_override="Handling general chat inquiries"
)

triga_agent = Agent(
    name="Triga Agent",
     instructions=prompt_with_handoff_instructions("""You are an intent classification agent. Your task is to classify users into different agents based on their intent.
Book Agent: It handles queries related to book content (such as asking about the author, content, chapter, etc.). You can directly call this agent through the handle_book_query tool.
Chat Agent: Ordinary chat content. You can directly call this agent through handle_chat
    """),
    
    handoffs=[to_book_agent,to_chat_agent]
)
async def main():
    response =  Runner.run_streamed(triga_agent,input="What is the last sentence of Chapter 1?",max_turns=10,run_config=run_config)
    async for event in response.stream_events():
#         print(event)
        if event.type == "raw_response_event" and isinstance(event.data, ResponseTextDeltaEvent):
            print(event.data.delta, end="", flush=True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion about using the SDK

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions