sumedhphadke commited on
Commit
775ea72
ยท
1 Parent(s): 9758a71

updated session vars

Browse files
Files changed (3) hide show
  1. .gitignore +4 -0
  2. app.py +33 -17
  3. chainlit.md +14 -0
.gitignore CHANGED
@@ -205,3 +205,7 @@ cython_debug/
205
  marimo/_static/
206
  marimo/_lsp/
207
  __marimo__/
 
 
 
 
 
205
  marimo/_static/
206
  marimo/_lsp/
207
  __marimo__/
208
+
209
+ # Chainlit
210
+
211
+ .chainlit/
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import os
2
- import contextvars
3
  import chainlit as cl
4
  from dotenv import load_dotenv
5
  from langchain_anthropic import ChatAnthropic
@@ -56,28 +55,40 @@ def get_chain():
56
  @cl.on_chat_start
57
  async def on_chat_start():
58
  """Initialize the chain when a chat session starts."""
 
 
 
 
 
59
  # Check if API key is available
60
  if not ANTHROPIC_API_KEY:
61
- await cl.Message(
62
- content="โŒ Error: ANTHROPIC_API_KEY not configured. Please set it in Space settings โ†’ Variables and secrets."
63
- ).send()
 
 
 
 
64
  return
65
 
66
- # Create the chain
67
- chain = get_chain()
68
  if chain is None:
69
- await cl.Message(
70
- content="โŒ Error: Could not initialize chain. Please check API key configuration."
71
- ).send()
 
 
 
 
72
  return
73
 
74
- # Store the chain in user session
75
- cl.user_session.set("chain", chain)
76
-
77
  # Send welcome message - according to prompt section 1.6, ask ONE question: "What are you working on?"
78
- await cl.Message(
79
- content="What are you working on?"
80
- ).send()
 
 
 
81
 
82
 
83
  @cl.on_message
@@ -86,9 +97,14 @@ async def on_message(message: cl.Message):
86
  # Retrieve the chain from user session
87
  chain = cl.user_session.get("chain")
88
 
 
89
  if chain is None:
90
- await cl.Message(content="Error: Chain not initialized. Please refresh the page.").send()
91
- return
 
 
 
 
92
 
93
  # Create a message object for streaming
94
  msg = cl.Message(content="")
 
1
  import os
 
2
  import chainlit as cl
3
  from dotenv import load_dotenv
4
  from langchain_anthropic import ChatAnthropic
 
55
  @cl.on_chat_start
56
  async def on_chat_start():
57
  """Initialize the chain when a chat session starts."""
58
+ # Store the chain in user session first (before any messages)
59
+ chain = get_chain()
60
+ if chain:
61
+ cl.user_session.set("chain", chain)
62
+
63
  # Check if API key is available
64
  if not ANTHROPIC_API_KEY:
65
+ try:
66
+ await cl.Message(
67
+ content="โŒ Error: ANTHROPIC_API_KEY not configured. Please set it in Space settings โ†’ Variables and secrets."
68
+ ).send()
69
+ except LookupError:
70
+ # ContextVar error - chain will be initialized on first message instead
71
+ pass
72
  return
73
 
74
+ # Create the chain if not already created
 
75
  if chain is None:
76
+ try:
77
+ await cl.Message(
78
+ content="โŒ Error: Could not initialize chain. Please check API key configuration."
79
+ ).send()
80
+ except LookupError:
81
+ # ContextVar error - chain will be initialized on first message instead
82
+ pass
83
  return
84
 
 
 
 
85
  # Send welcome message - according to prompt section 1.6, ask ONE question: "What are you working on?"
86
+ try:
87
+ await cl.Message(content="What are you working on?").send()
88
+ except LookupError:
89
+ # ContextVar error - message will be sent on first user message instead
90
+ # The chain is already stored in user_session, so on_message will work
91
+ pass
92
 
93
 
94
  @cl.on_message
 
97
  # Retrieve the chain from user session
98
  chain = cl.user_session.get("chain")
99
 
100
+ # If chain is None, try to initialize it (in case on_chat_start had ContextVar issues)
101
  if chain is None:
102
+ chain = get_chain()
103
+ if chain:
104
+ cl.user_session.set("chain", chain)
105
+ else:
106
+ await cl.Message(content="Error: Chain not initialized. Please refresh the page.").send()
107
+ return
108
 
109
  # Create a message object for streaming
110
  msg = cl.Message(content="")
chainlit.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Welcome to Chainlit! ๐Ÿš€๐Ÿค–
2
+
3
+ Hi there, Developer! ๐Ÿ‘‹ We're excited to have you on board. Chainlit is a powerful tool designed to help you prototype, debug and share applications built on top of LLMs.
4
+
5
+ ## Useful Links ๐Ÿ”—
6
+
7
+ - **Documentation:** Get started with our comprehensive [Chainlit Documentation](https://docs.chainlit.io) ๐Ÿ“š
8
+ - **Discord Community:** Join our friendly [Chainlit Discord](https://discord.gg/k73SQ3FyUh) to ask questions, share your projects, and connect with other developers! ๐Ÿ’ฌ
9
+
10
+ We can't wait to see what you create with Chainlit! Happy coding! ๐Ÿ’ป๐Ÿ˜Š
11
+
12
+ ## Welcome screen
13
+
14
+ To modify the welcome screen, edit the `chainlit.md` file at the root of your project. If you do not want a welcome screen, just leave this file empty.