from typing import List
from uuid import uuid4

from langchain.prompts import ChatPromptTemplate

from langchain.schema import AIMessage, HumanMessage, SystemMessage
from langchain_community.chat_models.fake import FakeListChatModel

from honcho import Honcho
from honcho.ext.langchain import langchain_message_converter

app_name = str(uuid4())

honcho = Honcho(
    app_name=app_name, base_url="http://localhost:8000"
)  # uncomment to use local
# honcho = Honcho(app_name=app_name)  # uses demo server at https://demo.honcho.dev
honcho.initialize()

responses = ["Fake LLM Response :)"]
llm = FakeListChatModel(responses=responses)
system = SystemMessage(
    content="You are world class technical documentation writer. Be as concise as possible"
)

user_name = "CLI-Test"
user = honcho.create_user(user_name)
session = user.create_session()


# def langchain_message_converter(messages: List):
#     new_messages = []
#     for message in messages:
#         if message.is_user:
#             new_messages.append(HumanMessage(content=message.content))
#         else:
#             new_messages.append(AIMessage(content=message.content))
#     return new_messages


def chat():
    while True:
        user_input = input("User: ")
        if user_input == "exit":
            session.close()
            break
        user_message = HumanMessage(content=user_input)
        history = list(session.get_messages_generator())
        langchain_history = langchain_message_converter(history)
        prompt = ChatPromptTemplate.from_messages(
            [system, *langchain_history, user_message]
        )
        chain = prompt | llm
        response = chain.invoke({})
        print(type(response))
        print(f"AI: {response.content}")
        session.create_message(is_user=True, content=user_input)
        session.create_message(is_user=False, content=response.content)


chat()

langchain.prompts.ChatPromptTemplate.from_messages langchain_community.chat_models.fake.FakeListChatModel langchain.schema.SystemMessage langchain.schema.HumanMessage
[((356, 415), 'honcho.Honcho', 'Honcho', ([], {'app_name': 'app_name', 'base_url': '"""http://localhost:8000"""'}), "(app_name=app_name, base_url='http://localhost:8000')\n", (362, 415), False, 'from honcho import Honcho\n'), ((596, 634), 'langchain_community.chat_models.fake.FakeListChatModel', 'FakeListChatModel', ([], {'responses': 'responses'}), '(responses=responses)\n', (613, 634), False, 'from langchain_community.chat_models.fake import FakeListChatModel\n'), ((644, 756), 'langchain.schema.SystemMessage', 'SystemMessage', ([], {'content': '"""You are world class technical documentation writer. Be as concise as possible"""'}), "(content=\n    'You are world class technical documentation writer. Be as concise as possible'\n    )\n", (657, 756), False, 'from langchain.schema import AIMessage, HumanMessage, SystemMessage\n'), ((337, 344), 'uuid.uuid4', 'uuid4', ([], {}), '()\n', (342, 344), False, 'from uuid import uuid4\n'), ((1338, 1370), 'langchain.schema.HumanMessage', 'HumanMessage', ([], {'content': 'user_input'}), '(content=user_input)\n', (1350, 1370), False, 'from langchain.schema import AIMessage, HumanMessage, SystemMessage\n'), ((1456, 1492), 'honcho.ext.langchain.langchain_message_converter', 'langchain_message_converter', (['history'], {}), '(history)\n', (1483, 1492), False, 'from honcho.ext.langchain import langchain_message_converter\n'), ((1510, 1586), 'langchain.prompts.ChatPromptTemplate.from_messages', 'ChatPromptTemplate.from_messages', (['[system, *langchain_history, user_message]'], {}), '([system, *langchain_history, user_message])\n', (1542, 1586), False, 'from langchain.prompts import ChatPromptTemplate\n')]