import streamlit as st import random import time import os from langchain_together import ChatTogether from langchain_text_splitters import RecursiveCharacterTextSplitter from langchain_community.document_loaders import TextLoader from langchain_core.prompts import ChatPromptTemplate from langchain_community.vectorstores import FAISS from langchain_core.output_parsers import StrOutputParser from langchain_core.runnables import RunnablePassthrough from langchain_together import TogetherEmbeddings os.environ["TOGETHER_API_KEY"] = os.getenv("API_TOKEN") # os.environ["TOGETHER_API_KEY"] = "bafbab854ae828c3b90f675c45c8263e9404d278b5694909ea0855f437b9d1f3" #load loader = TextLoader("Resume_data.txt") documents = loader.load() # split it into chunks text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) vectorstore = FAISS.from_documents(docs, TogetherEmbeddings(model="togethercomputer/m2-bert-80M-8k-retrieval") ) retriever = vectorstore.as_retriever() print("assigning model") model = ChatTogether( # model = "meta-llama/Meta-Llama-3-8B-Instruct-Lite", # model = "deepseek-ai/DeepSeek-R1-Distill-Llama-70B-free", model = "meta-llama/Llama-3.3-70B-Instruct-Turbo-Free", # model="meta-llama/Llama-3-70b-chat-hf", temperature=0.0, max_tokens=500,) prompt = ChatPromptTemplate([ ("system", """You are an assistant for question-answering tasks. If you don't know the answer, just say that "i don't know". answer as if real person is responding. and if user greets then greet back"""), ("user", "context : {context}, Question: {question}") ]) chain = ( {"context": retriever, "question": RunnablePassthrough()} | prompt | model | StrOutputParser() ) st.title("Chat with me") # Initialize chat history if "messages" not in st.session_state: st.session_state.messages = [] # Display chat messages from history on app rerun for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) # Accept user input if prompt := st.chat_input("What is up?"): # Display user message in chat message container with st.chat_message("user"): st.markdown(prompt) # Add user message to chat history st.session_state.messages.append({"role": "user", "content": prompt}) ############################################ # Streamed response emulator def response_generator(): query = f"{prompt}" if query != "None": for m in chain.stream(query): yield m time.sleep(0.01) else: yield "Hi, How can i help you?" ########################################### # Display assistant response in chat message container with st.chat_message("assistant"): response = st.write_stream(response_generator()) # Add assistant response to chat history st.session_state.messages.append({"role": "assistant", "content": response})