Commit ·
63bdcc6
1
Parent(s): 1bc1f1d
updated start chat
Browse files
app.py
CHANGED
|
@@ -5,36 +5,56 @@ from langchain_anthropic import ChatAnthropic
|
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
|
| 8 |
-
# Load environment variables
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@cl.on_chat_start
|
| 13 |
async def on_chat_start():
|
| 14 |
"""Initialize the chain when a chat session starts."""
|
| 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 |
@cl.on_message
|
|
@@ -60,6 +80,10 @@ async def on_message(message: cl.Message):
|
|
| 60 |
await msg.update()
|
| 61 |
|
| 62 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
await cl.Message(
|
| 64 |
-
content=f"
|
| 65 |
).send()
|
|
|
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate
|
| 6 |
from langchain_core.output_parsers import StrOutputParser
|
| 7 |
|
| 8 |
+
# Load environment variables (Hugging Face Spaces injects secrets automatically)
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
+
# Verify API key is available
|
| 12 |
+
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY")
|
| 13 |
+
if not ANTHROPIC_API_KEY:
|
| 14 |
+
print("WARNING: ANTHROPIC_API_KEY not found in environment variables!")
|
| 15 |
+
|
| 16 |
|
| 17 |
@cl.on_chat_start
|
| 18 |
async def on_chat_start():
|
| 19 |
"""Initialize the chain when a chat session starts."""
|
| 20 |
+
# Check if API key is available
|
| 21 |
+
if not ANTHROPIC_API_KEY:
|
| 22 |
+
await cl.Message(
|
| 23 |
+
content="❌ Error: ANTHROPIC_API_KEY not configured. Please set it in Space settings → Variables and secrets."
|
| 24 |
+
).send()
|
| 25 |
+
return
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
# Initialize the ChatAnthropic model with streaming enabled
|
| 29 |
+
model = ChatAnthropic(
|
| 30 |
+
model="claude-3-5-sonnet-20241022",
|
| 31 |
+
streaming=True,
|
| 32 |
+
temperature=0.7,
|
| 33 |
+
api_key=ANTHROPIC_API_KEY # Explicitly pass the API key
|
| 34 |
+
)
|
| 35 |
|
| 36 |
+
# Create the prompt template with system message for Math Tutor persona
|
| 37 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 38 |
+
("system", "You are a friendly Math Tutor for elementary school students. Explain things simply and encouragingly. Use age-appropriate language and make learning fun!"),
|
| 39 |
+
("human", "{input}")
|
| 40 |
+
])
|
| 41 |
|
| 42 |
+
# Build the LCEL chain: Prompt | Model | Parser
|
| 43 |
+
chain = prompt | model | StrOutputParser()
|
| 44 |
|
| 45 |
+
# Store the chain in user session
|
| 46 |
+
cl.user_session.set("chain", chain)
|
| 47 |
|
| 48 |
+
# Send welcome message
|
| 49 |
+
await cl.Message(
|
| 50 |
+
content="Hello! I'm your friendly Math Tutor. I'm here to help you learn math in a fun and easy way! What would you like to learn today? 🌟"
|
| 51 |
+
).send()
|
| 52 |
+
except Exception as e:
|
| 53 |
+
error_msg = f"❌ Error initializing chain: {str(e)}"
|
| 54 |
+
print(f"Error in on_chat_start: {error_msg}")
|
| 55 |
+
await cl.Message(
|
| 56 |
+
content=f"{error_msg}\n\nPlease check the Space logs and verify ANTHROPIC_API_KEY is set correctly."
|
| 57 |
+
).send()
|
| 58 |
|
| 59 |
|
| 60 |
@cl.on_message
|
|
|
|
| 80 |
await msg.update()
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
+
# Log the full error for debugging
|
| 84 |
+
error_msg = f"❌ Error: {str(e)}"
|
| 85 |
+
# This will show in Hugging Face logs
|
| 86 |
+
print(f"Error in on_message: {error_msg}")
|
| 87 |
await cl.Message(
|
| 88 |
+
content=f"{error_msg}\n\nPlease check the Space logs for more details."
|
| 89 |
).send()
|