def build_conversation_history()

in search/genai-101/code_examples/chat-app-code_es-8-15/backend/services/llm_service.py [0:0]


def build_conversation_history(history, user_message, ai_response):
    """
    Function to build converstation history for the LLM
    Keep 2 messages from the user and 2 messages from the AI
    When the count of messages from the user and AI each is greater than 2,
     keep the last 2 messages as is
     make a call to the LLM to summarize the conversation and keep the summary

    Summary is kept in the "system" role

    structure
    [
      {"role": "system", "content": "Conversation summary:  [summary here]"},
      {"role": "user", "content": "2 messages ago"},
      {"role": "assistant", "content": "2 responses ago"},
      {"role": "user", "content": "1 message ago"},
      {"role": "assistant", "content": "1 response ago"}
    ]
    """

    logging.info("Starting to build conversation history function")
    if len(history) < 4:
        logging.info("History is less than 4 messages. Adding new messages to history")
        history.extend([
            {
                "role": "user",
                "content": user_message
            },
            {
                "role": "assistant",
                "content": ai_response
            }
        ])
        new_history = history
    else:
        logging.info("History is greater than 4 messages. Summarizing conversation")
        summary_prompt = f"""
You are a conversation summarizer specializing in restaurant recommendations, reviews, and related topics. Your task is to create a detailed and accurate summary of the conversation history, incorporating the new message provided. This summary will be used as context for future interactions, so ensure it retains all relevant details, especially the specific restaurants discussed, their ratings, and key points mentioned by the user.

Rules:
1. Summarize the entire conversation, including the provided history and the new message.
2. Prioritize the restaurants mentioned in the latest user queries and assistant responses. Maintain focus on these restaurants in subsequent interactions.
3. Ensure that restaurant names, ratings, specific food items, and any notable characteristics (e.g., speed of service, atmosphere, cuisine type) are clearly retained and highlighted in the summary.
4. Avoid introducing irrelevant details or shifting focus to restaurants not part of the immediate conversation.
5. Use a structured format optimized for AI processing, keeping relevant information at the forefront. Length may exceed 150 words if necessary to maintain context.
6. Highlight any unresolved questions or areas that may require further clarification in future interactions.

Current conversation history:
{history}

New User message:
{user_message}

New Assistant response:
{ai_response}

Provide your summary in the following format:
SUMMARY: [Detailed summary focusing on the specific restaurants discussed, their key characteristics, and any user preferences or questions.]
KEY RESTAURANTS: [List of restaurant names mentioned in the conversation, along with their ratings and any specific details relevant to the user’s queries.]
RELEVANT DETAILS: [List any specific food items, service attributes, or user preferences mentioned.]
TOPICS: [List of key topics discussed, such as speed of service, food quality, or atmosphere.]
UNRESOLVED: [Any open questions or issues that may need further attention.]
        """

        # call the LLM to summarize the conversation
        # summary = query_aws_bedrock_nonstreaming(summary_prompt)
        summary = es_chat_completion(summary_prompt)
        logging.info(f"LLM Summary of history: {summary}")

        # Build new history
        new_history = [
            {
                "role": "system",
                "content": summary
            },
            history[-2],
            history[-1],
            {
                "role": "user",
                "content": user_message
            },
            {
                "role": "assistant",
                "content": ai_response
            }
        ]

    logging.info(f"New conversation history: {new_history}")

    return new_history