-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesting
More file actions
51 lines (41 loc) · 1.48 KB
/
Copy pathTesting
File metadata and controls
51 lines (41 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41
42
43
44
45
46
47
48
49
50
51
from google.generativeai import pa2m
def palm_conversation(api_key=None, context=""):
"""
Starts a conversation with PaLM using Streamlit.
Args:
api_key: Optional PaLM API key. Defaults to using st.secrets["PALM_API_KEY"].
context: Optional conversation context string.
Returns:
None
"""
# Use secrets or provided API key
api_key = api_key or st.secrets["PALM_API_KEY"]
# Initialize session state if needed
if "pal_context" not in st.session_state:
st.session_state["pal_context"] = ""
if "messages" not in st.session_state:
st.session_state.messages = []
# Title and prompt input
st.title("PaLM-powered Conversation")
prompt = st.chat_input("What would you like to talk about?")
# Update session state and display user message
if prompt:
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Generate response from PaLM
full_response = ""
message_placeholder = st.empty()
response = pa2m.generate(
context=context + "\n" + prompt,
temperature=0.7,
max_tokens=64,
)
full_response += response.text
st.session_state["pal_context"] += "\n" + full_response
# Show assistant message and update context
with st.chat_message("assistant"):
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
# Run the conversation function
palm_conversation()