File size: 4,048 Bytes
61e298a
0779250
 
 
 
 
 
41bc383
 
 
 
 
0779250
 
 
41bc383
 
 
0779250
 
 
 
41bc383
0779250
41bc383
 
 
 
0779250
 
 
 
 
 
 
 
 
 
 
41bc383
 
0779250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41bc383
 
 
 
0779250
 
 
 
 
 
 
 
 
 
 
 
41bc383
0779250
 
 
41bc383
0779250
 
 
 
41bc383
 
0779250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41bc383
 
 
 
61e298a
0779250
41bc383
0779250
 
 
 
41bc383
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import chainlit as cl
import os
from dotenv import load_dotenv
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables.config import RunnableConfig
from langchain_openai import ChatOpenAI

# general code area 
# - use for functions that are used in multiple places
# - use for variables that are used in multiple places
# - use for running code for all sessions

load_dotenv()
openai_api_key = os.getenv("OPENAI_API_KEY")

# Demonstrate how to handle actions

@cl.action_callback("Hide button")
async def on_action(action):
    await cl.Message(content=f"Hiding the button").send()
    await action.remove()

@cl.action_callback("Show text")
async def on_action(action):
    # any processing can be done here
    await cl.Message(content=f"Button clicked has this value: {action.value}").send()

# demonstrate how to change parameters of the LLM through actions

@cl.action_callback("english")
async def on_action(action):
    cl.user_session.set("language", "english")
    await cl.Message(content="Responses from the Chatbot will be in English").send()

@cl.action_callback("icelandic")
async def on_action(action):
    cl.user_session.set("language", "icelandic")
    await cl.Message(content="Responses from the Chatbot will be in Icelandic").send()

# templates for the LLM

user_template = """
        Question:
        {question}

        Language:
        {language}
    """

system_template = """
    You are a helpful assistant who always speaks in a pleasant tone!
    Do your best to answer the question succinctly and truthfully.
    Think through your answers carefully.
    Respond in the language provided below. If no language is provided, use Italian.
""" 

#################################################
### On Chat Start (Session Start) Section     ###
### Use this for pre-session setup processing ###
#################################################
@cl.on_chat_start
async def on_chat_start():
    # create a chain
    chat_prompt = ChatPromptTemplate.from_messages([
        ("system", system_template),
        ("human", user_template)
    ])
    chat_model = ChatOpenAI(model="gpt-4o-mini")
    simple_chain = chat_prompt | chat_model
    cl.user_session.set("chain", simple_chain)

    response = await cl.AskActionMessage(
        content="Do you want to experiment with buttons?",
        actions=[
            cl.Action(name="yes", value="yes", label="βœ… Yes"),
            cl.Action(name="no", value="no", label="❌ No"),
            cl.Action(name="maybe", value="maybe", label="❌ Maybe"),
        ],
    ).send()
    if response and response.get("value") == "yes":
        actions = [
            cl.Action(name="Hide button", value="hide_button", description="Hide this button"),
            cl.Action(name="Show text", value="show_text", description="Show thetext")
        ]

        await cl.Message(content="Different actions", actions=actions).send()
    else: 
        await cl.Message(content="No buttons for you").send()
    
    await cl.Message(content="Lets see how to change the language of the responses from the LLM").send()

    actions = [
        cl.Action(name="english", value="english", description="English"),
        cl.Action(name="icelandic", value="icelandic", description="Icelandic")
    ]

    await cl.Message(content="Languages", actions=actions).send()
    
    await cl.Message(content="Ask the chatbot a question. Then click the Icelandic button and ask again.").send()

#################################################
### On Message Section                        ###
### Use this for processing each user message ###
#################################################   
@cl.on_message
async def main(message: cl.Message):
    # get the session variables
    chain = cl.user_session.get("chain")
    language = cl.user_session.get("language", "english")
    question = message.content

    response = chain.invoke({"question": question, "language": language})
    print(response.content)
    await cl.Message(content=response.content).send()