Ground model response based on another conversation

Dear all,

I am trying to use a conversation as grounding context, can you tell me what’s the best way to do it?

I am building an app where the user can create an AI twin to speak for themselves. The user first onboard by talking to the model and provide information, and we use the conversation when generating response for AI twin.

Here’s my current code:

def ai_twin_chat_completion(
    messages: list[dict], onboarding_conversation_messages: list[dict]
) -> str:
    labeled_onboarding_conversation_messages = [
        {
            "role": message["role"],
            "content": f"[Onboarding Context] {message['content']}",
        }
        for message in onboarding_conversation_messages
    ]
    
    labeled_messages = [
        {
            "role": message["role"],
            "content": f"[Current User Message] {message['content']}",
        }
        for message in messages
    ]

    completion = client.beta.chat.completions.parse(
        model="gpt-4o-mini",
        messages=[
            {
                "role": "system",
                "content": (
                  "Mimic the user in [Onboarding Context] and respond to the user in [Current User Message]."
                ),
            },
        ]
        + labeled_onboarding_conversation_messages
        + labeled_messages,
        max_completion_tokens=256,
        temperature=0.7,
        response_format=AiTwinResponse,
    )
    return completion.choices[0].message.parsed

I am trying to figure out a better way to add the onboarding conversation, so that the model can understand it correctly. Any help will be much appreciated.

Thanks!