rchrdgwr commited on
Commit
0779250
1 Parent(s): 41dc448

buttons, questions

Browse files
Files changed (5) hide show
  1. .env.sample +1 -3
  2. app.py +100 -2
  3. chainlit.md +7 -0
  4. not_what_i_need.txt +0 -132
  5. requirements.txt +6 -0
.env.sample CHANGED
@@ -1,5 +1,3 @@
1
  # !!! DO NOT UPDATE THIS FILE DIRECTLY. MAKE A COPY AND RENAME IT `.env` TO PROCEED !!! #
2
- HF_LLM_ENDPOINT="YOUR_LLM_ENDPOINT_URL_HERE"
3
- HF_EMBED_ENDPOINT="YOUR_EMBED_MODEL_ENDPOINT_URL_HERE"
4
- HF_TOKEN="YOUR_HF_TOKEN_HERE"
5
  # !!! DO NOT UPDATE THIS FILE DIRECTLY. MAKE A COPY AND RENAME IT `.env` TO PROCEED !!! #
 
1
  # !!! DO NOT UPDATE THIS FILE DIRECTLY. MAKE A COPY AND RENAME IT `.env` TO PROCEED !!! #
2
+ OPENAI_API_KEY=
 
 
3
  # !!! DO NOT UPDATE THIS FILE DIRECTLY. MAKE A COPY AND RENAME IT `.env` TO PROCEED !!! #
app.py CHANGED
@@ -1,5 +1,103 @@
1
  import chainlit as cl
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
  @cl.on_message
4
- def main(message: str):
5
- return cl.Message(content=f"You said: {message}")
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import chainlit as cl
2
+ import os
3
+ from dotenv import load_dotenv
4
+ 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):
23
+ cl.user_session.set("language", "english")
24
+ await cl.Message(content="Responses from the Chatbot will be in English").send()
25
+
26
+ @cl.action_callback("icelandic")
27
+ 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}
34
+
35
+ Language:
36
+ {language}
37
+ """
38
+
39
+ system_template = """
40
+ You are a helpful assistant who always speaks in a pleasant tone!
41
+ Do your best to answer the question succinctly and truthfully.
42
+ Think through your answers carefully.
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
52
+ chat_prompt = ChatPromptTemplate.from_messages([
53
+ ("system", system_template),
54
+ ("human", user_template)
55
+ ])
56
+ chat_model = ChatOpenAI(model="gpt-4o-mini")
57
+ simple_chain = chat_prompt | chat_model
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()
74
+ else:
75
+ await cl.Message(content="No buttons for you").send()
76
+
77
+ await cl.Message(content="Lets see how to change the language of the responses from the LLM").send()
78
+
79
+ actions = [
80
+ cl.Action(name="english", value="english", description="English"),
81
+ cl.Action(name="icelandic", value="icelandic", description="Icelandic")
82
+ ]
83
+
84
+ await cl.Message(content="Languages", actions=actions).send()
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()
chainlit.md CHANGED
@@ -1,2 +1,9 @@
1
  # Chainlit Tutorial
2
 
 
 
 
 
 
 
 
 
1
  # Chainlit Tutorial
2
 
3
+ This code demonstrates:
4
+
5
+ - Display of a button
6
+ - How to handle the action when the button is clicked
7
+ - How to hide the button after it has been clicked
8
+ - How to ask a question to the user and handle the answer
9
+
not_what_i_need.txt DELETED
@@ -1,132 +0,0 @@
1
- aiofiles==23.2.1
2
- aiohappyeyeballs==2.4.3
3
- aiohttp==3.10.8
4
- aiosignal==1.3.1
5
- annotated-types==0.7.0
6
- anyio==3.7.1
7
- async-timeout==4.0.3
8
- asyncer==0.0.2
9
- attrs==24.2.0
10
- bidict==0.23.1
11
- certifi==2024.8.30
12
- chainlit==0.7.700
13
- charset-normalizer==3.3.2
14
- click==8.1.7
15
- dataclasses-json==0.5.14
16
- Deprecated==1.2.14
17
- distro==1.9.0
18
- exceptiongroup==1.2.2
19
- faiss-cpu==1.8.0.post1
20
- fastapi==0.100.1
21
- fastapi-socketio==0.0.10
22
- filelock==3.16.1
23
- filetype==1.2.0
24
- frozenlist==1.4.1
25
- fsspec==2024.9.0
26
- googleapis-common-protos==1.65.0
27
- greenlet==3.1.1
28
- grpcio==1.66.2
29
- grpcio-tools==1.62.3
30
- h11==0.14.0
31
- h2==4.1.0
32
- hpack==4.0.0
33
- httpcore==0.17.3
34
- httpx==0.24.1
35
- huggingface-hub==0.25.1
36
- hyperframe==6.0.1
37
- idna==3.10
38
- importlib_metadata==8.4.0
39
- Jinja2==3.1.4
40
- jiter==0.5.0
41
- joblib==1.4.2
42
- jsonpatch==1.33
43
- jsonpointer==3.0.0
44
- langchain==0.3.2
45
- langchain-community==0.3.1
46
- langchain-core==0.3.8
47
- langchain-huggingface==0.1.0
48
- langchain-openai==0.2.0
49
- langchain-qdrant==0.1.4
50
- langchain-text-splitters==0.3.0
51
- # langsmith==0.1.121
52
- Lazify==0.4.0
53
- MarkupSafe==2.1.5
54
- marshmallow==3.22.0
55
- mpmath==1.3.0
56
- multidict==6.1.0
57
- mypy-extensions==1.0.0
58
- nest-asyncio==1.6.0
59
- networkx==3.2.1
60
- numpy==1.26.4
61
- nvidia-cublas-cu12==12.1.3.1
62
- nvidia-cuda-cupti-cu12==12.1.105
63
- nvidia-cuda-nvrtc-cu12==12.1.105
64
- nvidia-cuda-runtime-cu12==12.1.105
65
- nvidia-cudnn-cu12==9.1.0.70
66
- nvidia-cufft-cu12==11.0.2.54
67
- nvidia-curand-cu12==10.3.2.106
68
- nvidia-cusolver-cu12==11.4.5.107
69
- nvidia-cusparse-cu12==12.1.0.106
70
- nvidia-nccl-cu12==2.20.5
71
- nvidia-nvjitlink-cu12==12.6.77
72
- nvidia-nvtx-cu12==12.1.105
73
- openai==1.51.0
74
- opentelemetry-api==1.27.0
75
- opentelemetry-exporter-otlp==1.27.0
76
- opentelemetry-exporter-otlp-proto-common==1.27.0
77
- opentelemetry-exporter-otlp-proto-grpc==1.27.0
78
- opentelemetry-exporter-otlp-proto-http==1.27.0
79
- opentelemetry-instrumentation==0.48b0
80
- opentelemetry-proto==1.27.0
81
- opentelemetry-sdk==1.27.0
82
- opentelemetry-semantic-conventions==0.48b0
83
- orjson==3.10.7
84
- packaging==23.2
85
- pillow==10.4.0
86
- portalocker==2.10.1
87
- protobuf==4.25.5
88
- pydantic==2.9.2
89
- pydantic-settings==2.5.2
90
- pydantic_core==2.23.4
91
- PyJWT==2.9.0
92
- PyMuPDF==1.24.10
93
- PyMuPDFb==1.24.10
94
- python-dotenv==1.0.1
95
- python-engineio==4.9.1
96
- python-graphql-client==0.4.3
97
- python-multipart==0.0.6
98
- python-socketio==5.11.4
99
- PyYAML==6.0.2
100
- qdrant-client==1.11.2
101
- regex==2024.9.11
102
- requests==2.32.3
103
- safetensors==0.4.5
104
- scikit-learn==1.5.2
105
- scipy==1.13.1
106
- sentence-transformers==3.1.1
107
- simple-websocket==1.0.0
108
- sniffio==1.3.1
109
- SQLAlchemy==2.0.35
110
- starlette==0.27.0
111
- sympy==1.13.3
112
- syncer==2.0.3
113
- tenacity==8.5.0
114
- threadpoolctl==3.5.0
115
- tiktoken==0.7.0
116
- tokenizers==0.20.0
117
- tomli==2.0.1
118
- torch==2.4.1
119
- tqdm==4.66.5
120
- transformers==4.45.1
121
- triton==3.0.0
122
- typing-inspect==0.9.0
123
- typing_extensions==4.12.2
124
- uptrace==1.26.0
125
- urllib3==2.2.3
126
- uvicorn==0.23.2
127
- watchfiles==0.20.0
128
- websockets==13.1
129
- wrapt==1.16.0
130
- wsproto==1.2.0
131
- yarl==1.13.1
132
- zipp==3.20.2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1 +1,7 @@
1
  chainlit==0.7.700
 
 
 
 
 
 
 
1
  chainlit==0.7.700
2
+ openai>=1.26.0
3
+ langchain==0.3.0
4
+ langchain-core==0.3.1
5
+ langchain-community==0.3.0
6
+ langchain-openai==0.2.0
7
+