File size: 15,727 Bytes
2e4aaee 67bfc8c 2e4aaee 67bfc8c 2e4aaee d81ac57 2e4aaee 67bfc8c 2e4aaee b30b8bf |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
import streamlit as st
import openai
import random
# Fetch the OpenAI API key from Streamlit secrets
openai_api_key = st.secrets["openai_api_key"]
# Initialize the OpenAI service with API key
openai.api_key = openai_api_key
# # Fetch Pinecone API key and environment from Streamlit secrets
# PINECONE_API_KEY = st.secrets["PINECONE_API_KEY"]
# # pinecone_api_key = '555c0e70-331d-4b43-aac7-5b3aac5078d6'
# pinecone_environment = st.secrets["pinecone_environment"]
# # AUTHENTICATE/INITIALIZE PINCONE SERVICE
# from pinecone import Pinecone
# # pc = Pinecone(api_key=PINECONE_API_KEY)
# pc = Pinecone (api_key= 'YOUR_API_KEY')
PINECONE_API_KEY = "555c0e70-331d-4b43-aac7-5b3aac5078d6"
pc = Pinecone(api_key=PINECONE_API_KEY)
# # Define the name of the Pinecone index
index_name = 'mimtssinkqa'
# Initialize the OpenAI embeddings object
from langchain_openai import OpenAIEmbeddings
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
# LOAD VECTOR STORE FROM EXISTING INDEX
from langchain_community.vectorstores import Pinecone
vector_store = Pinecone.from_existing_index(index_name='mimtssinkqa', embedding=embeddings)
def ask_with_memory(vector_store, query, chat_history=[]):
from langchain_openai import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
llm = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=0.5)
retriever = vector_store.as_retriever(search_type='similarity', search_kwargs={'k': 3})
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
system_template = r'''
Use the following pieces of context to answer the user's question. The title of the article is Intensifying literacy Instruction: Essential Practices. Do not mention the Header unless asked.
----------------
Context: ```{context}```
'''
user_template = '''
Question: ```{question}```
Chat History: ```{chat_history}```
'''
messages= [
SystemMessagePromptTemplate.from_template(system_template),
HumanMessagePromptTemplate.from_template(user_template)
]
qa_prompt = ChatPromptTemplate.from_messages (messages)
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory,chain_type='stuff', combine_docs_chain_kwargs={'prompt': qa_prompt}, verbose=False
)
result = chain.invoke({'question': query, 'chat_history': st.session_state['history']})
# Append to chat history as a dictionary
st.session_state['history'].append((query, result['answer']))
return (result['answer'])
# Initialize chat history
if 'history' not in st.session_state:
st.session_state['history'] = []
# # STREAMLIT APPLICATION SETUP WITH PASSWORD
# Define the correct password
# correct_password = "MiBLSi"
#Add the image with a specified width
image_width = 300 # Set the desired width in pixels
st.image('MTSS.ai_Logo.png', width=image_width)
st.subheader('Ink QA™ | Dynamic PDFs')
# Using Markdown for formatted text
st.markdown("""
Resource: **Intensifying Literacy Instruction: Essential Practices**
""", unsafe_allow_html=True)
with st.sidebar:
# Password input field
# password = st.text_input("Enter Password:", type="password")
st.image('mimtss.png', width=200)
st.image('Literacy_Cover.png', width=200)
st.link_button("View | Download", "https://mimtsstac.org/sites/default/files/session-documents/Intensifying%20Literacy%20Instruction%20-%20Essential%20Practices%20%28NATIONAL%29.pdf")
Audio_Header_text = """
**Tune into Dr. St. Martin's introduction**"""
st.markdown(Audio_Header_text)
# Path or URL to the audio file
audio_file_path = 'Audio_Introduction_Literacy.m4a'
# Display the audio player widget
st.audio(audio_file_path, format='audio/mp4', start_time=0)
# Citation text with Markdown formatting
citation_Content_text = """
**Citation**
St. Martin, K., Vaughn, S., Troia, G., Fien, & H., Coyne, M. (2023). *Intensifying literacy instruction: Essential practices, Version 2.0*. Lansing, MI: MiMTSS Technical Assistance Center, Michigan Department of Education.
**Table of Contents**
* **Introduction**: pg. 1
* **Intensifying Literacy Instruction: Essential Practices**: pg. 4
* **Purpose**: pg. 4
* **Practice 1**: Knowledge and Use of a Learning Progression for Developing Skilled Readers and Writers: pg. 6
* **Practice 2**: Design and Use of an Intervention Platform as the Foundation for Effective Intervention: pg. 13
* **Practice 3**: On-going Data-Based Decision Making for Providing and Intensifying Interventions: pg. 16
* **Practice 4**: Adaptations to Increase the Instructional Intensity of the Intervention: pg. 20
* **Practice 5**: Infrastructures to Support Students with Significant and Persistent Literacy Needs: pg. 24
* **Motivation and Engagement**: pg. 28
* **Considerations for Understanding How Students' Learning and Behavior are Enhanced**: pg. 28
* **Summary**: pg. 29
* **Endnotes**: pg. 30
* **Acknowledgment**: pg. 39
"""
st.markdown(citation_Content_text)
# if password == correct_password:
# Define a list of possible placeholder texts
placeholders = [
'Example: Summarize the article in 200 words or less',
'Example: What are the essential practices?',
'Example: I am a teacher, why is this resource important?',
'Example: How can this resource support my instruction in reading and writing?',
'Example: Does this resource align with the learning progression for developing skilled readers and writers?',
'Example: How does this resource address the needs of students scoring below the 20th percentile?',
'Example: Are there assessment tools included in this resource to monitor student progress?',
'Example: Does this resource provide guidance on data collection and analysis for monitoring student outcomes?',
"Example: How can this resource be used to support students' social-emotional development?",
"Example: How does this resource align with the district's literacy goals and objectives?",
'Example: What research and evidence support the effectiveness of this resource?',
'Example: Does this resource provide guidance on implementation fidelity'
]
# Select a random placeholder from the list
if 'placeholder' not in st.session_state:
st.session_state.placeholder = random.choice(placeholders)
q = st.text_input(label='Ask a question or make a request ', value='', placeholder=st.session_state.placeholder)
# q = st.text_input(label='Ask a question or make a request ', value='')
if q:
with st.spinner('Thinking...'):
answer = ask_with_memory(vector_store, q, st.session_state.history)
# Display the response in a text area
st.text_area('Response: ', value=answer, height=400, key="response_text_area")
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
# Prepare chat history text for display
# history_text = "\n\n".join(f"Q: {entry[0]}\nA: {entry[1]}" for entry in st.session_state.history)
# Prepare chat history text for display in reverse order
history_text = "\n\n".join(f"Q: {entry[0]}\nA: {entry[1]}" for entry in reversed(st.session_state.history))
# Display chat history
st.text_area('Chat History', value=history_text, height=800)
# import streamlit as st
# import pinecone
# from langchain.embeddings.openai import OpenAIEmbeddings
# from langchain.vectorstores import Pinecone, Chroma
# from langchain.chains import RetrievalQA
# from langchain.chat_models import ChatOpenAI
# import tiktoken
# import random
# # Fetch the OpenAI API key from Streamlit secrets
# openai_api_key = st.secrets["openai_api_key"]
# # Fetch Pinecone API key and environment from Streamlit secrets
# pinecone_api_key = st.secrets["pinecone_api_key"]
# pinecone_environment = st.secrets["pinecone_environment"]
# # Initialize Pinecone
# pinecone.init(api_key=pinecone_api_key, environment=pinecone_environment)
# # Define the name of the Pinecone index
# index_name = 'mi-resource-qa'
# # Initialize the OpenAI embeddings object with the hardcoded API key
# embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
# # Define functions
# def insert_or_fetch_embeddings(index_name):
# if index_name in pinecone.list_indexes():
# vector_store = Pinecone.from_existing_index(index_name, embeddings)
# return vector_store
# else:
# raise ValueError(f"Index {index_name} does not exist. Please create it before fetching.")
# # Initialize or fetch Pinecone vector store
# vector_store = insert_or_fetch_embeddings(index_name)
# # calculate embedding cost using tiktoken
# def calculate_embedding_cost(text):
# import tiktoken
# enc = tiktoken.encoding_for_model('text-embedding-ada-002')
# total_tokens = len(enc.encode(text))
# # print(f'Total Tokens: {total_tokens}')
# # print(f'Embedding Cost in USD: {total_tokens / 1000 * 0.0004:.6f}')
# return total_tokens, total_tokens / 1000 * 0.0004
# def ask_with_memory(vector_store, query, chat_history=[]):
# from langchain.chains import ConversationalRetrievalChain
# from langchain.chat_models import ChatOpenAI
# llm = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=1, openai_api_key=openai_api_key)
# # The retriever is created with metadata filter directly in search_kwargs
# # retriever = vector_store.as_retriever(search_type='similarity', search_kwargs={'k': 3, 'filter': {'source': {'$eq': 'https://mimtsstac.org/sites/default/files/session-documents/Intensifying%20Literacy%20Instruction%20-%20Essential%20Practices%20%28NATIONAL%29.pdf'}}})
# retriever = vector_store.as_retriever(search_type='similarity', search_kwargs={'k': 3, 'filter': {'source':'https://mimtsstac.org/sites/default/files/session-documents/Intensifying%20Literacy%20Instruction%20-%20Essential%20Practices%20%28NATIONAL%29.pdf'}})
# chain= ConversationalRetrievalChain.from_llm(llm, retriever)
# result = chain({'question': query, 'chat_history': st.session_state['history']})
# # Append to chat history as a dictionary
# st.session_state['history'].append((query, result['answer']))
# return (result['answer'])
# # Initialize chat history
# if 'history' not in st.session_state:
# st.session_state['history'] = []
# # # STREAMLIT APPLICATION SETUP WITH PASSWORD
# # Define the correct password
# # correct_password = "MiBLSi"
# #Add the image with a specified width
# image_width = 300 # Set the desired width in pixels
# st.image('MTSS.ai_Logo.png', width=image_width)
# st.subheader('Ink QA™ | Dynamic PDFs')
# # Using Markdown for formatted text
# st.markdown("""
# Resource: **Intensifying Literacy Instruction: Essential Practices**
# """, unsafe_allow_html=True)
# with st.sidebar:
# # Password input field
# # password = st.text_input("Enter Password:", type="password")
# st.image('mimtss.png', width=200)
# st.image('Literacy_Cover.png', width=200)
# st.link_button("View | Download", "https://mimtsstac.org/sites/default/files/session-documents/Intensifying%20Literacy%20Instruction%20-%20Essential%20Practices%20%28NATIONAL%29.pdf")
# Audio_Header_text = """
# **Tune into Dr. St. Martin's introduction**"""
# st.markdown(Audio_Header_text)
# # Path or URL to the audio file
# audio_file_path = 'Audio_Introduction_Literacy.m4a'
# # Display the audio player widget
# st.audio(audio_file_path, format='audio/mp4', start_time=0)
# # Citation text with Markdown formatting
# citation_Content_text = """
# **Citation**
# St. Martin, K., Vaughn, S., Troia, G., Fien, & H., Coyne, M. (2023). *Intensifying literacy instruction: Essential practices, Version 2.0*. Lansing, MI: MiMTSS Technical Assistance Center, Michigan Department of Education.
# **Table of Contents**
# * **Introduction**: pg. 1
# * **Intensifying Literacy Instruction: Essential Practices**: pg. 4
# * **Purpose**: pg. 4
# * **Practice 1**: Knowledge and Use of a Learning Progression for Developing Skilled Readers and Writers: pg. 6
# * **Practice 2**: Design and Use of an Intervention Platform as the Foundation for Effective Intervention: pg. 13
# * **Practice 3**: On-going Data-Based Decision Making for Providing and Intensifying Interventions: pg. 16
# * **Practice 4**: Adaptations to Increase the Instructional Intensity of the Intervention: pg. 20
# * **Practice 5**: Infrastructures to Support Students with Significant and Persistent Literacy Needs: pg. 24
# * **Motivation and Engagement**: pg. 28
# * **Considerations for Understanding How Students' Learning and Behavior are Enhanced**: pg. 28
# * **Summary**: pg. 29
# * **Endnotes**: pg. 30
# * **Acknowledgment**: pg. 39
# """
# st.markdown(citation_Content_text)
# # if password == correct_password:
# # Define a list of possible placeholder texts
# placeholders = [
# 'Example: Summarize the article in 200 words or less',
# 'Example: What are the essential practices?',
# 'Example: I am a teacher, why is this resource important?',
# 'Example: How can this resource support my instruction in reading and writing?',
# 'Example: Does this resource align with the learning progression for developing skilled readers and writers?',
# 'Example: How does this resource address the needs of students scoring below the 20th percentile?',
# 'Example: Are there assessment tools included in this resource to monitor student progress?',
# 'Example: Does this resource provide guidance on data collection and analysis for monitoring student outcomes?',
# "Example: How can this resource be used to support students' social-emotional development?",
# "Example: How does this resource align with the district's literacy goals and objectives?",
# 'Example: What research and evidence support the effectiveness of this resource?',
# 'Example: Does this resource provide guidance on implementation fidelity'
# ]
# # Select a random placeholder from the list
# if 'placeholder' not in st.session_state:
# st.session_state.placeholder = random.choice(placeholders)
# q = st.text_input(label='Ask a question or make a request ', value='', placeholder=st.session_state.placeholder)
# # q = st.text_input(label='Ask a question or make a request ', value='')
# k = 3 # Set k to 3
# # # Initialize chat history if not present
# # if 'history' not in st.session_state:
# # st.session_state.history = []
# if q:
# with st.spinner('Thinking...'):
# answer = ask_with_memory(vector_store, q, st.session_state.history)
# # Display the response in a text area
# st.text_area('Response: ', value=answer, height=400, key="response_text_area")
# st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
# # # Prepare chat history text for display
# # history_text = "\n\n".join(f"Q: {entry[0]}\nA: {entry[1]}" for entry in st.session_state.history)
# # Prepare chat history text for display in reverse order
# history_text = "\n\n".join(f"Q: {entry[0]}\nA: {entry[1]}" for entry in reversed(st.session_state.history))
# # Display chat history
# st.text_area('Chat History', value=history_text, height=800)
|