Spaces:
Sleeping
Sleeping
halimbahae
commited on
Commit
•
95ccce1
1
Parent(s):
a180bd2
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#Importing required packages
|
2 |
+
import streamlit as st
|
3 |
+
import anthropic
|
4 |
+
import uuid
|
5 |
+
|
6 |
+
INIT_PROMPT = """
|
7 |
+
\n\nHuman: You are MapMentor a trainer in Wardley Mapping. You will help the users learn about Wardley Mapping
|
8 |
+
"""
|
9 |
+
|
10 |
+
TRAINING_PROMPT = """
|
11 |
+
Here is an outline for a training course that you will give to the user. It covers the key principles of Wardley Mapping:
|
12 |
+
"""
|
13 |
+
|
14 |
+
INTRO_PROMPT = """
|
15 |
+
Hello, I'm DarijaBot
|
16 |
+
|
17 |
+
"""
|
18 |
+
|
19 |
+
REG_PROMPT = """
|
20 |
+
\n\nHuman: Here is the user's question about Wardley Mapping:
|
21 |
+
<question>
|
22 |
+
{QUESTION}
|
23 |
+
</question>
|
24 |
+
\n\nAssistant: [MapMentor] <response>
|
25 |
+
"""
|
26 |
+
|
27 |
+
MODEL = "claude-3-5-sonnet-20240620"
|
28 |
+
|
29 |
+
new_prompt = []
|
30 |
+
|
31 |
+
if "session_id" not in st.session_state:
|
32 |
+
st.session_state.session_id = str(uuid.uuid4())
|
33 |
+
|
34 |
+
st.set_page_config(page_title="Anthropic - ChatBot")
|
35 |
+
st.sidebar.title("Anthropic - ChatBot")
|
36 |
+
st.sidebar.title("Wardley Mapping Mentor")
|
37 |
+
st.sidebar.divider()
|
38 |
+
st.sidebar.markdown("Developed by Bahae Eddine HALIM](https://linkedin.com/in/halimbahae)", unsafe_allow_html=True)
|
39 |
+
st.sidebar.markdown("Current Version: 0.0.4")
|
40 |
+
st.sidebar.markdown("Using claude-2 API")
|
41 |
+
st.sidebar.markdown(st.session_state.session_id)
|
42 |
+
st.sidebar.divider()
|
43 |
+
|
44 |
+
# Check if the user has provided an API key, otherwise default to the secret
|
45 |
+
user_claude_api_key = st.sidebar.text_input("Enter your Anthropic API Key:", placeholder="sk-...", type="password")
|
46 |
+
|
47 |
+
if "claude_model" not in st.session_state:
|
48 |
+
st.session_state["claude_model"] = MODEL
|
49 |
+
|
50 |
+
if "messages" not in st.session_state:
|
51 |
+
st.session_state["messages"] = []
|
52 |
+
st.session_state.messages.append({"role": "assistant", "content": INTRO_PROMPT})
|
53 |
+
|
54 |
+
if "all_prompts" not in st.session_state:
|
55 |
+
st.session_state["all_prompts"] = INIT_PROMPT + TRAINING_PROMPT
|
56 |
+
|
57 |
+
if user_claude_api_key:
|
58 |
+
# If the user has provided an API key, use it
|
59 |
+
client=anthropic.Anthropic(
|
60 |
+
# defaults to os.environ.get("ANTHROPIC_API_KEY")
|
61 |
+
api_key=user_claude_api_key,
|
62 |
+
)
|
63 |
+
else:
|
64 |
+
st.warning("Please enter your Anthropic Claude API key", icon="⚠️")
|
65 |
+
|
66 |
+
for message in st.session_state.messages:
|
67 |
+
if message["role"] in ["user", "assistant"]:
|
68 |
+
with st.chat_message(message["role"]):
|
69 |
+
new_prompt.append(message["content"])
|
70 |
+
st.markdown(message["content"])
|
71 |
+
|
72 |
+
if user_claude_api_key:
|
73 |
+
if user_input := st.chat_input("How can I help with Wardley Mapping?"):
|
74 |
+
prompt = REG_PROMPT.format(QUESTION = user_input)
|
75 |
+
st.session_state.all_prompts += prompt
|
76 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
77 |
+
with st.chat_message("user"):
|
78 |
+
st.markdown(user_input)
|
79 |
+
with st.chat_message("assistant"):
|
80 |
+
message_placeholder = st.empty()
|
81 |
+
full_response = ""
|
82 |
+
try:
|
83 |
+
for response in client.completions.create(
|
84 |
+
prompt=st.session_state.all_prompts,
|
85 |
+
stop_sequences=["</response>"],
|
86 |
+
model=MODEL,
|
87 |
+
max_tokens_to_sample=500,
|
88 |
+
stream=True,
|
89 |
+
):
|
90 |
+
full_response += response.completion
|
91 |
+
message_placeholder.markdown(full_response + "▌")
|
92 |
+
message_placeholder.markdown(full_response)
|
93 |
+
except anthropic.APIConnectionError as e:
|
94 |
+
st.error("The server could not be reached")
|
95 |
+
print(e.__cause__) # an underlying Exception, likely raised within httpx.
|
96 |
+
except anthropic.RateLimitError as e:
|
97 |
+
st.error("A 429 status code was received; we should back off a bit.")
|
98 |
+
except anthropic.APIStatusError as e:
|
99 |
+
st.error("Another non-200-range status code was received\nTry again later.")
|
100 |
+
st.error(e.status_code)
|
101 |
+
st.error(e.response)
|
102 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
103 |
+
st.session_state.all_prompts += full_response
|