rchrdgwr commited on
Commit
41bc383
β€’
1 Parent(s): 0779250

completed first tutorial

Browse files
Files changed (2) hide show
  1. .chainlit/config.toml +3 -3
  2. app.py +31 -20
.chainlit/config.toml CHANGED
@@ -29,13 +29,13 @@ multi_modal = true
29
 
30
  # Allows user to use speech to text
31
  [features.speech_to_text]
32
- enabled = false
33
  # See all languages here https://github.com/JamesBrill/react-speech-recognition/blob/HEAD/docs/API.md#language-string
34
- # language = "en-US"
35
 
36
  [UI]
37
  # Name of the app and chatbot.
38
- name = "Chatbot"
39
 
40
  # Show the readme while the conversation is empty.
41
  show_readme_as_default = true
 
29
 
30
  # Allows user to use speech to text
31
  [features.speech_to_text]
32
+ enabled = true
33
  # See all languages here https://github.com/JamesBrill/react-speech-recognition/blob/HEAD/docs/API.md#language-string
34
+ language = "en-US"
35
 
36
  [UI]
37
  # Name of the app and chatbot.
38
+ name = "Chainlit TutorialChatbot"
39
 
40
  # Show the readme while the conversation is empty.
41
  show_readme_as_default = true
app.py CHANGED
@@ -5,18 +5,27 @@ from langchain_core.prompts import ChatPromptTemplate
5
  from langchain_core.runnables.config import RunnableConfig
6
  from langchain_openai import ChatOpenAI
7
 
 
 
 
 
 
8
  load_dotenv()
9
  openai_api_key = os.getenv("OPENAI_API_KEY")
10
 
11
- @cl.action_callback("hide_button")
 
 
12
  async def on_action(action):
13
  await cl.Message(content=f"Hiding the button").send()
14
  await action.remove()
15
 
16
- @cl.action_callback("show_text")
17
  async def on_action(action):
18
- cl.user_session.set("language", "english")
19
- await cl.Message(content="The button was clicked and this text was shown").send()
 
 
20
 
21
  @cl.action_callback("english")
22
  async def on_action(action):
@@ -28,6 +37,8 @@ async def on_action(action):
28
  cl.user_session.set("language", "icelandic")
29
  await cl.Message(content="Responses from the Chatbot will be in Icelandic").send()
30
 
 
 
31
  user_template = """
32
  Question:
33
  {question}
@@ -43,9 +54,10 @@ system_template = """
43
  Respond in the language provided below. If no language is provided, use Italian.
44
  """
45
 
46
- #############################################
47
- ### On Chat Start (Session Start) Section ###
48
- #############################################
 
49
  @cl.on_chat_start
50
  async def on_chat_start():
51
  # create a chain
@@ -58,16 +70,17 @@ async def on_chat_start():
58
  cl.user_session.set("chain", simple_chain)
59
 
60
  response = await cl.AskActionMessage(
61
- content="Do you want to see the buttons?",
62
  actions=[
63
  cl.Action(name="yes", value="yes", label="βœ… Yes"),
64
  cl.Action(name="no", value="no", label="❌ No"),
 
65
  ],
66
  ).send()
67
  if response and response.get("value") == "yes":
68
  actions = [
69
- cl.Action(name="hide_button", value="hide-button", description="Hide this button"),
70
- cl.Action(name="show_text", value="show_text", description="Show text")
71
  ]
72
 
73
  await cl.Message(content="Different actions", actions=actions).send()
@@ -85,19 +98,17 @@ async def on_chat_start():
85
 
86
  await cl.Message(content="Ask the chatbot a question. Then click the Icelandic button and ask again.").send()
87
 
88
-
 
 
 
89
  @cl.on_message
90
  async def main(message: cl.Message):
 
91
  chain = cl.user_session.get("chain")
92
  language = cl.user_session.get("language", "english")
93
  question = message.content
94
 
95
- msg = cl.Message(content="")
96
- # handle streaming of LLM responses
97
- async for chunk in chain.astream(
98
- {"question": question, "language": language},
99
- config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
100
- ):
101
- await msg.stream_token(chunk.content)
102
-
103
- await msg.send()
 
5
  from langchain_core.runnables.config import RunnableConfig
6
  from langchain_openai import ChatOpenAI
7
 
8
+ # general code area
9
+ # - use for functions that are used in multiple places
10
+ # - use for variables that are used in multiple places
11
+ # - use for running code for all sessions
12
+
13
  load_dotenv()
14
  openai_api_key = os.getenv("OPENAI_API_KEY")
15
 
16
+ # Demonstrate how to handle actions
17
+
18
+ @cl.action_callback("Hide button")
19
  async def on_action(action):
20
  await cl.Message(content=f"Hiding the button").send()
21
  await action.remove()
22
 
23
+ @cl.action_callback("Show text")
24
  async def on_action(action):
25
+ # any processing can be done here
26
+ await cl.Message(content=f"Button clicked has this value: {action.value}").send()
27
+
28
+ # demonstrate how to change parameters of the LLM through actions
29
 
30
  @cl.action_callback("english")
31
  async def on_action(action):
 
37
  cl.user_session.set("language", "icelandic")
38
  await cl.Message(content="Responses from the Chatbot will be in Icelandic").send()
39
 
40
+ # templates for the LLM
41
+
42
  user_template = """
43
  Question:
44
  {question}
 
54
  Respond in the language provided below. If no language is provided, use Italian.
55
  """
56
 
57
+ #################################################
58
+ ### On Chat Start (Session Start) Section ###
59
+ ### Use this for pre-session setup processing ###
60
+ #################################################
61
  @cl.on_chat_start
62
  async def on_chat_start():
63
  # create a chain
 
70
  cl.user_session.set("chain", simple_chain)
71
 
72
  response = await cl.AskActionMessage(
73
+ content="Do you want to experiment with buttons?",
74
  actions=[
75
  cl.Action(name="yes", value="yes", label="βœ… Yes"),
76
  cl.Action(name="no", value="no", label="❌ No"),
77
+ cl.Action(name="maybe", value="maybe", label="❌ Maybe"),
78
  ],
79
  ).send()
80
  if response and response.get("value") == "yes":
81
  actions = [
82
+ cl.Action(name="Hide button", value="hide_button", description="Hide this button"),
83
+ cl.Action(name="Show text", value="show_text", description="Show thetext")
84
  ]
85
 
86
  await cl.Message(content="Different actions", actions=actions).send()
 
98
 
99
  await cl.Message(content="Ask the chatbot a question. Then click the Icelandic button and ask again.").send()
100
 
101
+ #################################################
102
+ ### On Message Section ###
103
+ ### Use this for processing each user message ###
104
+ #################################################
105
  @cl.on_message
106
  async def main(message: cl.Message):
107
+ # get the session variables
108
  chain = cl.user_session.get("chain")
109
  language = cl.user_session.get("language", "english")
110
  question = message.content
111
 
112
+ response = chain.invoke({"question": question, "language": language})
113
+ print(response.content)
114
+ await cl.Message(content=response.content).send()