run-llama / llama_index

LlamaIndex is a data framework for your LLM applications
https://docs.llamaindex.ai
MIT License
33.29k stars 4.66k forks source link

[Question]: Defining Prompt Template for OpenAIAgent #14273

Open anantgupta129 opened 2 weeks ago

anantgupta129 commented 2 weeks ago

Question Validation

Question

How to use pormpt template in OpenAI agent

agent = OpenAIAgent.from_tools(tool_spec, llm=llm, verbose=True)
agent.get_prompts()
>> []
from llama_index.core import PromptTemplate

REACT_SYSTEM_PROMPT = PromptTemplate(react_system_header_str)
agent.update_prompts({"agent_worker:system_prompt": REACT_SYSTEM_PROMPT})
>>  for module_name, sub_prompt_dict in sub_prompt_dicts.items():
     [74](file:///C:/Users/anant/miniconda3/envs/super/Lib/site-packages/llama_index/core/prompts/mixin.py:74)     if module_name not in prompt_modules:
---> [75](file:///C:/Users/anant/miniconda3/envs/super/Lib/site-packages/llama_index/core/prompts/mixin.py:75)         raise ValueError(f"Module {module_name} not found.")
     [76](file:///C:/Users/anant/miniconda3/envs/super/Lib/site-packages/llama_index/core/prompts/mixin.py:76)     module = prompt_modules[module_name]
     [77](file:///C:/Users/anant/miniconda3/envs/super/Lib/site-packages/llama_index/core/prompts/mixin.py:77)     module.update_prompts(sub_prompt_dict)

ValueError: Module agent_worker not found.

With this i also have some additional questions:

dosubot[bot] commented 2 weeks ago

To use prompt templates with OpenAIAgent and resolve the ValueError: Module agent_worker not found error, follow these steps:

  1. Define the Prompt Template:

    from llama_index.core import PromptTemplate
    
    REACT_SYSTEM_PROMPT = PromptTemplate(react_system_header_str)
  2. Update Prompts: Ensure that the keys used in the update_prompts method do not contain the ":" character and are correctly prefixed by their sub-modules as "namespaces". For example, use agent:system_prompt instead of agent_worker:system_prompt.

    agent.update_prompts({"agent:system_prompt": REACT_SYSTEM_PROMPT})
  3. Check Available Prompts: Before updating, you can check the available prompts to ensure you are using the correct keys.

    prompts = agent.get_prompts()
    print(prompts)

    This will help you identify the correct keys to use when updating prompts.

Additional Notes:

By following these steps, you should be able to use prompt templates with OpenAIAgent and avoid the ValueError.

To continue talking to Dosu, mention @dosu.

logan-markewich commented 2 weeks ago

@anantgupta129 there is no prompt template for an openai agent

It is just sending the chat history and list of tools through the openai client.

So in that sense, the only prompt is the chat history, and the schemas of tools

anantgupta129 commented 2 weeks ago

hey @logan-markewich follow-up questions

llm = AzureOpenAI(
    model=os.getenv("AOAI_COMPLETION_MODEL"),
    deployment_name=os.getenv("AOAI_DEPLOYMENT_NAME_COMPLETION"),
    api_key=os.getenv("AZURE_OPENAI_API_KEY"),
    azure_endpoint=os.getenv("AOAI_ENDPOINT"),
    api_version=os.getenv("AOAI_API_VERSION"),
)