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

Merged
merged 6 commits into from
Sep 20, 2024

Conversation

FredLL-Avaiga
Copy link
Member

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

resolves #1807

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: 8ab8fe6 by action🐍

namnguyen20999
namnguyen20999 previously approved these changes Sep 19, 2024
namnguyen20999
namnguyen20999 previously approved these changes 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.

🤔

namnguyen20999
namnguyen20999 previously approved these changes Sep 20, 2024
Copy link
Contributor

Coverage report for ./frontend/taipy-gui

Caution

Coverage does not meet threshold
Branches coverage not met for global: expected >=80%, but got 69.18714555765595%

St.
Category Percentage Covered / Total
🟢 Statements
87.67% (+0.03% 🔼)
3194/3643
🟡 Branches
69.19% (+0.17% 🔼)
2196/3174
🟢 Functions
82.98% (+0.33% 🔼)
585/705
🟢 Lines
88.21% (+0% 🔼)
2956/3351

Test suite run success

647 tests passing in 43 suites.

Report generated by 🧪jest coverage report action from 8ab8fe6

Copy link
Member

@namnguyen20999 namnguyen20999 left a comment

Choose a reason for hiding this comment

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

lgtm

@FredLL-Avaiga FredLL-Avaiga merged commit cdc9561 into develop Sep 20, 2024
160 checks passed
@FredLL-Avaiga FredLL-Avaiga deleted the feature/chat-support-markdown-pre-raw branch September 20, 2024 08:34
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.

Chat controls support different rendering mode like Text
4 participants