Langchain-Chatchat icon indicating copy to clipboard operation
Langchain-Chatchat copied to clipboard

想在使用文件对话时使用自己的提示词,想要的回答时结构化的格式输出,我在自定义提示词的时候如何写我需要结构化方式的回答?

Open Ptianyu opened this issue 1 year ago • 6 comments

Ptianyu avatar May 16 '24 03:05 Ptianyu

To customize your prompt for structured format responses in file dialogs, you can use Python's str.format method for dynamic prompt generation. Here's a concise guide:

  1. Create a Custom Prompt Template: Use str.format with placeholders ({}) for dynamic content. This allows you to insert variables like user input or context into the prompt.

  2. Define Variables for the Template: Identify the variables you need (e.g., user input, context) and prepare them to be inserted into the template at runtime.

  3. Render the Custom Prompt: Fill in the template with your variables to generate the final prompt to be used in your dialog system.

  4. Handle Structured Responses: Ensure your system can parse and handle the structured responses, which might include actions, observations, and other structured data.

Example code snippet to illustrate these steps:

class CustomPromptTemplate(StringPromptTemplate):
    template: str
    tools: List[Tool]

    def format(self, **kwargs) -> str:
        # Example of adding intermediate steps and tool descriptions
        kwargs["agent_scratchpad"] = "\n".join([f"Thought: {action.log}" for action, _ in kwargs.get("intermediate_steps", [])])
        kwargs["tools"] = "\n".join([f"{tool.name}: {tool.description}" for tool in self.tools])
        return self.template.format(**kwargs)

# Rendering the custom prompt
custom_prompt_template = CustomPromptTemplate(template="Your custom prompt here with variables like {input}")
rendered_prompt = custom_prompt_template.format(input="Your specific question or input")

# Simulate getting a structured response
def get_response_for_custom_prompt(prompt):
    # Logic to process the prompt and return a structured response
    return {"action": "ExampleAction", "data": "ExampleData"}

# Handling the structured response
response = get_response_for_custom_prompt(rendered_prompt)
print(response)

This approach allows you to dynamically generate prompts tailored to your needs and handle responses in a structured format, facilitating easier parsing and processing of the data.

Details

Sources


About Dosu This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

dosubot[bot] avatar May 16 '24 03:05 dosubot[bot]

插眼

Dannissss0704 avatar May 16 '24 06:05 Dannissss0704

prompt_config.py里改提示词,我把中文的改成英文了,这样老外可以看到英文回复

blackwhites avatar May 16 '24 07:05 blackwhites

我想以xxx:xxx xxx:xxx的格式输出 但是他的输出格式是xxx:xxx xxx:xxx 我已经在提示词写了以第一种格式输出可回答还是第二种

Ptianyu avatar May 16 '24 07:05 Ptianyu

没明白你的意思,能具体些?

blackwhites avatar May 17 '24 02:05 blackwhites

1-prompt中举例说明回复格式 2-用符合格式的数据微调模型

CRonaldo1997 avatar May 22 '24 03:05 CRonaldo1997