Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chat support markdown pre raw #1810

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

FredLL-Avaiga
Copy link
Member

@FredLL-Avaiga FredLL-Avaiga commented Sep 19, 2024

from typing import List

import numpy as np

import taipy.gui.builder as tgb
from taipy.gui import Gui, Icon, State, notify


def request(state: State, prompt: str) -> str:
    """
    Send a prompt to the GPT-4 API and return the response.

    Args:
        - state: The current state of the app.
        - prompt: The prompt to send to the API.

    Returns:
        The response from the API.
    """
    random_response = [
        "How are you?",
        "What are you doing?",
        "What is your name?",
        "Who are you?",
    ]

    return random_response[np.random.randint(0, 4)]


# Initialize conversation context
base_conversation: List[List[str]] = [
    ["1", "Who are you?", "Human"],
    ["2", "Hi! I am GPT-4. How can I help you today?", "AI"],
]
conversation = None
users: List[List[str | Icon]] = [
    ["Human", Icon("/images/human_icon.png", "Human.png")],
    ["AI", Icon("/images/ai_icon.png", "AI.png")],
]


def on_init(state: State) -> None:
    """
    Initialize the app.

    Args:
        - state: The current state of the app.
    """
    state.conversation = base_conversation.copy()



def send_message(state: State, var_name: str, payload: dict = None) -> None:
    """
    Send the user's message to the API and update the context.

    Args:
        - state: The current state of the app.
    """
    if payload:
        args = payload.get("args", [])
        current_user_message = args[2]
        sender = args[3]

        notify(state, "info", "Sending message...")
        answer = request(state, current_user_message).replace("\n", "")

        state.conversation += [
            [
                f"{len(state.conversation) + 1}",
                current_user_message,
                sender,
            ]
        ]

        state.conversation += [[f"{len(state.conversation) + 1}", answer, "AI"]]
    notify(state, "success", "Response received!")



# Building the layout
with tgb.Page() as page:
    tgb.toggle(theme=True)
    tgb.chat(
        "{conversation}",
        users=users,
        on_action=send_message,
        sender_id="Human",
        show_sender=True,
        height="80vh",
        mode="md",
    )


# Main entry point
if __name__ == "__main__":
    client = None

    gui = Gui(page)
    gui.run(title="Taipy Simple Chat")

@FredLL-Avaiga FredLL-Avaiga added 📈 Improvement Improvement of a feature. 🖰 GUI Related to GUI 🟧 Priority: High Must be addressed as soon labels Sep 19, 2024
@FredLL-Avaiga FredLL-Avaiga self-assigned this Sep 19, 2024
Copy link
Member

@FabienLelaquais FabienLelaquais left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant!

Copy link
Member

@dinhlongviolin1 dinhlongviolin1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too easy for you

Copy link
Contributor

github-actions bot commented Sep 19, 2024

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
19343 16287 84% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
taipy/gui/_renderers/factory.py 96% 🟢
TOTAL 96% 🟢

updated for commit: 9b2a3db by action🐍

namnguyen20999
namnguyen20999 previously approved these changes Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🖰 GUI Related to GUI 📈 Improvement Improvement of a feature. 🟧 Priority: High Must be addressed as soon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants