Find answers from the community

Updated 3 months ago

how do i give a reactagent a system prompt

how do i give a reactagent a system prompt
k
t
12 comments
Please stand by @tomi as I search the knowledge base. I normally take about 20 seconds or so. :wave:
----------------------
To give a ReActAgent a system prompt, you can use the update_prompts method. Here's a step-by-step guide:

  1. First, you need to import the necessary modules and create an instance of ReActAgent. You can do this with the following code:
Plain Text
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI

llm = OpenAI(model="gpt-3.5-turbo-0613")  # or any other model you prefer

agent = ReActAgent.from_tools(
    query_engine_tools,  # replace with your list of tools
    llm=llm,
    verbose=True,
)

(Source: LlamaIndex Docs ())

  1. Define your custom system prompt. You can do this by creating an instance of PromptTemplate with your custom string:
Plain Text
from llama_index.core import PromptTemplate

react_system_header_str = """\
You are designed to help with a variety of tasks, from answering questions   
to providing summaries to other types of analyses.

# Add more details to your prompt here...
"""

react_system_prompt = PromptTemplate(react_system_header_str)

(Source: LlamaIndex Docs ())

  1. Finally, you can update the system prompt of your ReActAgent with the update_prompts method:
Plain Text
agent.update_prompts({"agent_worker:system_prompt": react_system_prompt})

(Source: LlamaIndex Docs ())

Please replace the react_system_header_str with your own custom prompt and query_engine_tools with your list of tools.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @tomi, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:

Perhaps try again in 15mins?
@kapa.ai i want to use the default llamaindex system prompt and add another arbitrary prompt to it
Let me check the knowledge base for you. One moment! :books:
----------------------
To use the default LlamaIndex system prompt and add another arbitrary prompt to it, you can follow these steps:

  1. First, you need to get the default prompt. LlamaIndex uses a set of default prompt templates that work well out of the box. You can find these default prompts in the LlamaIndex GitHub repository here ().
  1. Once you have the default prompt, you can add your arbitrary prompt to it. Here's an example of how you can do this:
Plain Text
from llama_index.core import PromptTemplate

# This is an example. Replace this with the actual default prompt
default_prompt_str = "This is the default prompt.\n"

# This is your arbitrary prompt
arbitrary_prompt_str = "This is an arbitrary prompt.\n"

# Combine the default prompt and the arbitrary prompt
combined_prompt_str = default_prompt_str + arbitrary_prompt_str

# Create a PromptTemplate with the combined prompt
combined_prompt = PromptTemplate(combined_prompt_str)


  1. Finally, you can use the update_prompts method to update the system prompt:
Plain Text
agent.update_prompts({"agent_worker:system_prompt": combined_prompt})


Please replace default_prompt_str with the actual default prompt and arbitrary_prompt_str with your arbitrary prompt.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai i'm usign ReActAgentWorker
Let me check the knowledge base for you. One moment! :books:
----------------------
To use the default LlamaIndex system prompt and add another arbitrary prompt to it with ReActAgentWorker, you can follow these steps:

  1. First, you need to get the default prompt. LlamaIndex uses a set of default prompt templates that work well out of the box. You can find these default prompts in the LlamaIndex GitHub repository here ().
  1. Once you have the default prompt, you can add your arbitrary prompt to it. Here's an example of how you can do this:
Plain Text
from llama_index.core import PromptTemplate

# This is an example. Replace this with the actual default prompt
default_prompt_str = "This is the default prompt.\n"

# This is your arbitrary prompt
arbitrary_prompt_str = "This is an arbitrary prompt.\n"

# Combine the default prompt and the arbitrary prompt
combined_prompt_str = default_prompt_str + arbitrary_prompt_str

# Create a PromptTemplate with the combined prompt
combined_prompt = PromptTemplate(combined_prompt_str)


  1. Finally, you can use the update_prompts method to update the system prompt:
Plain Text
agent_worker.update_prompts({{"system_prompt": combined_prompt}})


Please replace default_prompt_str with the actual default prompt and arbitrary_prompt_str with your arbitrary prompt.

Note: The update_prompts method is available in both ReActAgent and ReActAgentWorker. The difference is that for ReActAgentWorker, you don't need to prefix the key with agent_worker:.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
@kapa.ai
i'm gettig this:
Plain Text
    raise ValueError(f"Module {module_name} not found.")
ValueError: Module agent_worker not found.
Let me check the knowledge base for you. One moment! :books:
----------------------
The error message you're seeing typically occurs when the system is unable to find a module named agent_worker. This could be due to a few reasons:

  1. The module agent_worker does not exist in your current project or environment. Make sure that you have correctly named your modules and that the module is in the correct directory.
  1. You might be trying to access the agent_worker module before it has been properly imported or defined. Ensure that you have imported the module using the correct syntax. For example:
Plain Text
from llama_index.core.agent import ReActAgentWorker


  1. If you're trying to update the prompts of a ReActAgentWorker instance, make sure you're not prefixing the key with agent_worker:. The correct syntax should be:
Plain Text
agent_worker.update_prompts({"system_prompt": combined_prompt})


If you're still encountering issues, it would be helpful to see more of your code to better understand the context in which this error is occurring.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord