GrokAgenticWorkforce / pages /10_Gemini-ChatOnly-SP.py
eaglelandsonce's picture
Rename pages/10_Gemini-ChatOnly.py to pages/10_Gemini-ChatOnly-SP.py
8261967 verified
import streamlit as st
import uuid
import google.generativeai as genai
from io import BytesIO # Import BytesIO
# Set your API key
api_key = "AIzaSyC70u1sN87IkoxOoIj4XCAPw97ae2LZwNM" # Replace with your actual API key
genai.configure(api_key=api_key)
# Configure the generative AI model
generation_config = genai.GenerationConfig(
temperature=0.9,
max_output_tokens=3000
)
# Safety settings configuration
safety_settings = [
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_NONE",
},
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_NONE",
},
]
# Initialize session state for chat history
if 'chat_history' not in st.session_state:
st.session_state['chat_history'] = []
st.title("Gemini Chatbot")
# Display chat history
def display_chat_history():
for entry in st.session_state['chat_history']:
st.markdown(f"{entry['role'].title()}: {entry['parts'][0]['text']}")
# Function to clear conversation history
def clear_conversation():
st.session_state['chat_history'] = []
# Send message function with system prompt
def send_message():
user_input = st.session_state.user_input
if user_input:
# Define a system prompt that provides context for the model's generation
system_prompt = "AI Planner System Prompt: As the AI Planner, your primary task is to assist in the development of a coherent and engaging book. You will be responsible for organizing the overall structure, defining the plot or narrative, and outlining the chapters or sections. To accomplish this, you will need to use your understanding of storytelling principles and genre conventions, as well as any specific information provided by the user, to create a well-structured framework for the book."
prompts = [entry['parts'][0]['text'] for entry in st.session_state['chat_history']]
prompts.append(user_input)
# Combine the system prompt with the chat history
chat_history_str = system_prompt + "\n" + "\n".join(prompts)
model = genai.GenerativeModel(
model_name='gemini-pro',
generation_config=generation_config,
safety_settings=safety_settings
)
response = model.generate_content([{"role": "user", "parts": [{"text": chat_history_str}]}])
response_text = response.text if hasattr(response, "text") else "No response text found."
if response_text:
st.session_state['chat_history'].append({"role": "model", "parts":[{"text": response_text}]})
st.session_state.user_input = ''
display_chat_history()
# User input text area
user_input = st.text_area(
"Enter your message here:",
value="",
key="user_input"
)
# Send message button
send_button = st.button(
"Send",
on_click=send_message
)
# Clear conversation button
clear_button = st.button("Clear Conversation", on_click=clear_conversation)