Delete app_NoLangSmith.py
Browse files- app_NoLangSmith.py +0 -194
app_NoLangSmith.py
DELETED
|
@@ -1,194 +0,0 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import openai
|
| 3 |
-
import random
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# Fetch the OpenAI API key from Streamlit secrets
|
| 7 |
-
OPENAI_API_KEY = st.secrets["OPENAI_API_KEY"]
|
| 8 |
-
# Retrieve the OpenAI API Key from secrets
|
| 9 |
-
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
| 10 |
-
|
| 11 |
-
# # Fetch Pinecone API key and environment from Streamlit secrets
|
| 12 |
-
PINECONE_API_KEY = st.secrets["PINECONE_API_KEY"]
|
| 13 |
-
# # AUTHENTICATE/INITIALIZE PINCONE SERVICE
|
| 14 |
-
from pinecone import Pinecone
|
| 15 |
-
# PINECONE_API_KEY = "555c0e70-331d-4b43-aac7-5b3aac5078d6"
|
| 16 |
-
pc = Pinecone(api_key=PINECONE_API_KEY)
|
| 17 |
-
|
| 18 |
-
# # Define the name of the Pinecone index
|
| 19 |
-
index_name = 'mimtssinkqa'
|
| 20 |
-
|
| 21 |
-
# Initialize the OpenAI embeddings object
|
| 22 |
-
from langchain_openai import OpenAIEmbeddings
|
| 23 |
-
embeddings = OpenAIEmbeddings(openai_api_key=OPENAI_API_KEY)
|
| 24 |
-
|
| 25 |
-
# LOAD VECTOR STORE FROM EXISTING INDEX
|
| 26 |
-
from langchain_community.vectorstores import Pinecone
|
| 27 |
-
vector_store = Pinecone.from_existing_index(index_name='mimtssinkqa', embedding=embeddings)
|
| 28 |
-
|
| 29 |
-
def ask_with_memory(vector_store, query, chat_history=[]):
|
| 30 |
-
from langchain_openai import ChatOpenAI
|
| 31 |
-
from langchain.chains import ConversationalRetrievalChain
|
| 32 |
-
from langchain.memory import ConversationBufferMemory
|
| 33 |
-
|
| 34 |
-
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate
|
| 35 |
-
|
| 36 |
-
llm = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=0.5, openai_api_key=OPENAI_API_KEY)
|
| 37 |
-
|
| 38 |
-
retriever = vector_store.as_retriever(search_type='similarity', search_kwargs={'k': 3})
|
| 39 |
-
|
| 40 |
-
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
| 41 |
-
|
| 42 |
-
system_template = r'''
|
| 43 |
-
Article Title: 'Intensifying Literacy Instruction: Essential Practices.'
|
| 44 |
-
Article Focus: The main focus of the article is reading and the secondary focus is writing.
|
| 45 |
-
Expertise: Assume the role of an expert literacy coach with in-depth knowledge of the Simple View of Reading, School-Wide Positive Behavioral Interventions and Supports (SWPBIS), and Social Emotional Learning (SEL).
|
| 46 |
-
Audience: Tailor your response for teachers and administrators seeking to enhance literacy instruction within their educational settings.
|
| 47 |
-
Response Requirements: Provide an answer utilizing the context provided. Unless specifically requested by the user, avoid mentioning the article's header.
|
| 48 |
-
Cover all necessary details relevant to the question posed, drawing on your expertise in literacy instruction and the Simple View of Reading.
|
| 49 |
-
Utilize paragraphs for detailed and descriptive explanations, and bullet points for highlighting key points or steps, ensuring the information is easily understood.
|
| 50 |
-
Conclude with a recapitulation of main points, summarizing the essential takeaways from your response.
|
| 51 |
-
----------------
|
| 52 |
-
Context: ```{context}```
|
| 53 |
-
'''
|
| 54 |
-
|
| 55 |
-
user_template = '''
|
| 56 |
-
Question: ```{question}```
|
| 57 |
-
Chat History: ```{chat_history}```
|
| 58 |
-
'''
|
| 59 |
-
|
| 60 |
-
messages= [
|
| 61 |
-
SystemMessagePromptTemplate.from_template(system_template),
|
| 62 |
-
HumanMessagePromptTemplate.from_template(user_template)
|
| 63 |
-
]
|
| 64 |
-
|
| 65 |
-
qa_prompt = ChatPromptTemplate.from_messages (messages)
|
| 66 |
-
|
| 67 |
-
chain = ConversationalRetrievalChain.from_llm(llm=llm, retriever=retriever, memory=memory,chain_type='stuff', combine_docs_chain_kwargs={'prompt': qa_prompt}, verbose=False
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
-
result = chain.invoke({'question': query, 'chat_history': st.session_state['history']})
|
| 71 |
-
# Append to chat history as a dictionary
|
| 72 |
-
st.session_state['history'].append((query, result['answer']))
|
| 73 |
-
|
| 74 |
-
return (result['answer'])
|
| 75 |
-
|
| 76 |
-
# Initialize chat history
|
| 77 |
-
if 'history' not in st.session_state:
|
| 78 |
-
st.session_state['history'] = []
|
| 79 |
-
|
| 80 |
-
# # STREAMLIT APPLICATION SETUP WITH PASSWORD
|
| 81 |
-
|
| 82 |
-
# Define the correct password
|
| 83 |
-
# correct_password = "MiBLSi"
|
| 84 |
-
|
| 85 |
-
#Add the image with a specified width
|
| 86 |
-
image_width = 300 # Set the desired width in pixels
|
| 87 |
-
st.image('MTSS.ai_Logo.png', width=image_width)
|
| 88 |
-
st.subheader('Ink QA™ | Dynamic PDFs')
|
| 89 |
-
|
| 90 |
-
# Using Markdown for formatted text
|
| 91 |
-
st.markdown("""
|
| 92 |
-
Resource: **Intensifying Literacy Instruction: Essential Practices**
|
| 93 |
-
""", unsafe_allow_html=True)
|
| 94 |
-
|
| 95 |
-
with st.sidebar:
|
| 96 |
-
# Password input field
|
| 97 |
-
# password = st.text_input("Enter Password:", type="password")
|
| 98 |
-
|
| 99 |
-
st.image('mimtss.png', width=200)
|
| 100 |
-
st.image('Literacy_Cover.png', width=200)
|
| 101 |
-
st.link_button("View | Download", "https://mimtsstac.org/sites/default/files/session-documents/Intensifying%20Literacy%20Instruction%20-%20Essential%20Practices%20%28NATIONAL%29.pdf")
|
| 102 |
-
|
| 103 |
-
Audio_Header_text = """
|
| 104 |
-
**Tune into Dr. St. Martin's introduction**"""
|
| 105 |
-
st.markdown(Audio_Header_text)
|
| 106 |
-
|
| 107 |
-
# Path or URL to the audio file
|
| 108 |
-
audio_file_path = 'Audio_Introduction_Literacy.m4a'
|
| 109 |
-
# Display the audio player widget
|
| 110 |
-
st.audio(audio_file_path, format='audio/mp4', start_time=0)
|
| 111 |
-
|
| 112 |
-
# Citation text with Markdown formatting
|
| 113 |
-
citation_Content_text = """
|
| 114 |
-
**Citation**
|
| 115 |
-
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.
|
| 116 |
-
|
| 117 |
-
**Table of Contents**
|
| 118 |
-
* **Introduction**: pg. 1
|
| 119 |
-
* **Intensifying Literacy Instruction: Essential Practices**: pg. 4
|
| 120 |
-
* **Purpose**: pg. 4
|
| 121 |
-
* **Practice 1**: Knowledge and Use of a Learning Progression for Developing Skilled Readers and Writers: pg. 6
|
| 122 |
-
* **Practice 2**: Design and Use of an Intervention Platform as the Foundation for Effective Intervention: pg. 13
|
| 123 |
-
* **Practice 3**: On-going Data-Based Decision Making for Providing and Intensifying Interventions: pg. 16
|
| 124 |
-
* **Practice 4**: Adaptations to Increase the Instructional Intensity of the Intervention: pg. 20
|
| 125 |
-
* **Practice 5**: Infrastructures to Support Students with Significant and Persistent Literacy Needs: pg. 24
|
| 126 |
-
* **Motivation and Engagement**: pg. 28
|
| 127 |
-
* **Considerations for Understanding How Students' Learning and Behavior are Enhanced**: pg. 28
|
| 128 |
-
* **Summary**: pg. 29
|
| 129 |
-
* **Endnotes**: pg. 30
|
| 130 |
-
* **Acknowledgment**: pg. 39
|
| 131 |
-
"""
|
| 132 |
-
st.markdown(citation_Content_text)
|
| 133 |
-
|
| 134 |
-
# if password == correct_password:
|
| 135 |
-
# Define a list of possible placeholder texts
|
| 136 |
-
placeholders = [
|
| 137 |
-
'Example: Summarize the article in 200 words or less',
|
| 138 |
-
'Example: What are the essential practices?',
|
| 139 |
-
'Example: I am a teacher, why is this resource important?',
|
| 140 |
-
'Example: How can this resource support my instruction in reading and writing?',
|
| 141 |
-
'Example: Does this resource align with the learning progression for developing skilled readers and writers?',
|
| 142 |
-
'Example: How does this resource address the needs of students scoring below the 20th percentile?',
|
| 143 |
-
'Example: Are there assessment tools included in this resource to monitor student progress?',
|
| 144 |
-
'Example: Does this resource provide guidance on data collection and analysis for monitoring student outcomes?',
|
| 145 |
-
"Example: How can this resource be used to support students' social-emotional development?",
|
| 146 |
-
"Example: How does this resource align with the district's literacy goals and objectives?",
|
| 147 |
-
'Example: What research and evidence support the effectiveness of this resource?',
|
| 148 |
-
'Example: Does this resource provide guidance on implementation fidelity'
|
| 149 |
-
]
|
| 150 |
-
|
| 151 |
-
# Select a random placeholder from the list
|
| 152 |
-
if 'placeholder' not in st.session_state:
|
| 153 |
-
st.session_state.placeholder = random.choice(placeholders)
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
# CLEAR THE TEXT BOX
|
| 157 |
-
with st.form("Question",clear_on_submit=True):
|
| 158 |
-
q = st.text_input(label='Ask a Question | Send a Prompt', placeholder=st.session_state.placeholder, value='', )
|
| 159 |
-
submitted = st.form_submit_button("Submit")
|
| 160 |
-
|
| 161 |
-
st.divider()
|
| 162 |
-
|
| 163 |
-
if submitted:
|
| 164 |
-
with st.spinner('Thinking...'):
|
| 165 |
-
answer = ask_with_memory(vector_store, q, st.session_state.history)
|
| 166 |
-
|
| 167 |
-
# st.write(q)
|
| 168 |
-
st.write(f"**{q}**")
|
| 169 |
-
|
| 170 |
-
import time
|
| 171 |
-
import random
|
| 172 |
-
|
| 173 |
-
def stream_answer():
|
| 174 |
-
for word in answer.split(" "):
|
| 175 |
-
yield word + " "
|
| 176 |
-
# time.sleep(0.02)
|
| 177 |
-
time.sleep(random.uniform(0.03, 0.08))
|
| 178 |
-
|
| 179 |
-
st.write(stream_answer)
|
| 180 |
-
|
| 181 |
-
# Display the response in a text area
|
| 182 |
-
# st.text_area('Response: ', value=answer, height=400, key="response_text_area")
|
| 183 |
-
# OR to display as Markdown (interprets Markdown formatting)
|
| 184 |
-
# st.markdown(answer)
|
| 185 |
-
|
| 186 |
-
st.success('Powered by MTSS GPT. AI can make mistakes. Consider checking important information.')
|
| 187 |
-
|
| 188 |
-
st.divider()
|
| 189 |
-
|
| 190 |
-
# # Prepare chat history text for display
|
| 191 |
-
history_text = "\n\n".join(f"Q: {entry[0]}\nA: {entry[1]}" for entry in reversed(st.session_state.history))
|
| 192 |
-
|
| 193 |
-
# Display chat history
|
| 194 |
-
st.text_area('Chat History', value=history_text, height=800)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|