跳至内容

分配角色

我们如何提高模型在开放式任务上的性能?

角色提示(或称人格提示)是为模型分配一个角色。角色可以是

  • 特定于查询您是一位才华横溢的作家。请写一首诗。
  • 通用/社交您是一个乐于助人的人工智能助手。请写一首诗。

实现

import openai
import instructor
from pydantic import BaseModel

client = instructor.from_openai(openai.OpenAI())


class Response(BaseModel):
    poem: str


def role_prompting(query, role):
    return client.chat.completions.create(
        model="gpt-4o",
        response_model=Response,
        messages=[
            {
                "role": "system",
                "content": f"{role} {query}",
            },
        ],
    )


if __name__ == "__main__":
    query = "Write me a short poem about coffee."
    role = "You are a renowned poet."

    response = role_prompting(query, role)
    print(response.poem)
    """
    In the morning's gentle light,
    A brew of warmth, dark and bright.
    Awakening dreams, so sweet,
    In every sip, the day we greet.

    Through the steam, stories spin,
    A liquid muse, caffeine within.
    Moments pause, thoughts unfold,
    In coffee's embrace, we find our gold.
    """

更多角色提示

要了解选择角色的系统方法,请查看 RoleLLM

有关社交角色的更多示例,请查看这篇关于系统提示中社交角色评估的文章。

要了解如何使用多个角色,请查看Multi-Persona Self-Collaboration

参考资料

1: RoleLLM: Benchmarking, Eliciting, and Enhancing Role-Playing Abilities of Large Lanuage Models
2: Is "A Helpful Assistant" the Best Role for Large Language Models? A Systematic Evaluation of Social Roles in System Prompts
3: Unleashing the Emergent Cognitive Synergy in Large Lanuage Models: A Task-Solving Agent through Multi-Persona Self-Collaboration