fanyujiebest
fanyujiebest

Reputation: 1

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

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!

Upvotes: 0

Views: 1005

Answers (1)

Sergio B.
Sergio B.

Reputation: 1000

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.

Upvotes: 1

Related Questions