ChatGPT API, how to define a system role only once, so that later conversations are always based on this role

78 Views Asked by At

Like in the code, I have another list of similar questions. Each time I need to pass a description to system to specify its role context. But this is very costly, is there any way I can define the system's role before asking a question or as much as the first time I ask a question, and then all subsequent questions will be based on this role?

    for question in questions:
        completion = client.chat.completions.create(
            model="gpt-3.5-turbo-0125",
            messages=[
                {"role": "system", "content": "You are a professional lawyer, please help me with professional legal questions."},
                {"role": "user", "content": question},
            ],
            # max_tokens=1000,
            # temperature=0.7
        )
        message = completion.choices[0].message
        answer = message.get("content")
        print(answer.encode('utf-8').decode("utf-8"))

I've read about it on the openAI website but haven't found a solution, so any help would be appreciated!

2

There are 2 best solutions below

0
Sergio B. On

You are using the obsolete Chat Completion API. Use the new Assistant API instead (still in beta but ready to be released in a few weeks). The Assistant API is context aware, that is, when you specify the initial instructions (as a system prompt in the Chat API), you'll have to focus on the next questions only. When you create an Assistant it will keep the context of the conversation (that means the system prompt and the following questions/answers) until it expires or you delete it.

0
ADITYA On

Yes, You can define the system's role before asking a question and then reuse that role for subsequent questions.

# Define the system's role outside of the loop
system_role = {"role": "system", "content": "You are a professional lawyer, please help me with professional legal questions."}

for question in questions:
    completion = client.chat.completions.create(
        model="gpt-3.5-turbo-0125",
        messages=[
            system_role,  # Reuse the system's role
            {"role": "user", "content": question},
        ],
        # max_tokens=1000,
        # temperature=0.7
    )
    message = completion.choices[0].message
    answer = message.get("content")
    print(answer.encode('utf-8').decode("utf-8"))