File size: 6,661 Bytes
7e4014b 60e8923 d2b4a56 7e4014b d2b4a56 60e8923 d2b4a56 dd7f91e 60e8923 2159374 60e8923 20c0b83 7e4014b 20c0b83 7e4014b dd7f91e e2b472e dd7f91e 7e4014b 20c0b83 7e4014b fefc5e6 7e4014b 20c0b83 00bc7cc 20c0b83 dd7f91e 7e4014b dd7f91e 60e8923 7e4014b 60e8923 2159374 60e8923 2159374 60e8923 7e4014b d2b4a56 20c0b83 7e4014b 20c0b83 d2b4a56 60e8923 20c0b83 7e4014b 2159374 7e4014b 2159374 7e4014b e2b472e d2b4a56 60e8923 dd7f91e 60e8923 d2b4a56 dd7f91e d2b4a56 7e4014b dd7f91e 7e4014b dd7f91e 7e4014b b05ff4a 7e4014b 00bc7cc dd7f91e 7e4014b 00bc7cc 7e4014b e2b472e |
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 |
import streamlit as st
import os
import pandas as pd
from command_center import CommandCenter
from process_documents import process_documents
from embed_documents import create_retriever
import json
from langchain.callbacks import get_openai_callback
from langchain.chains import ConversationalRetrievalChain
from langchain_openai import ChatOpenAI
import base64
st.set_page_config(layout="wide")
os.environ["OPENAI_API_KEY"] = "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"
get_references = lambda relevant_docs: " ".join(
[f"[{ref}]" for ref in sorted([ref.metadata["chunk_id"] for ref in relevant_docs])]
)
session_state_2_llm_chat_history = lambda session_state: [
ss[:2] for ss in session_state if not ss[0].startswith("/")
]
ai_message_format = lambda message, references: (
f"{message}\n\n---\n\n{references}" if references != "" else message
)
welcome_message = """
Hi I'm Agent Zeta, your AI assistant, dedicated to making your journey through machine learning research papers as insightful and interactive as possible. Whether you're diving into the latest studies or brushing up on foundational papers, I'm here to help navigate, discuss, and analyze content with you.
Here's a quick guide to getting started with me:
| Command | Description |
|---------|-------------|
| `/upload` | Upload and process documents for our conversation. |
| `/index` | View an index of processed documents to easily navigate your research. |
| `/cost` | Calculate the cost of our conversation, ensuring transparency in resource usage. |
| `/download` | Download conversation data for your records or further analysis. |
<br>
Feel free to use these commands to enhance your research experience. Let's embark on this exciting journey of discovery together!
Use `/man` at any point of time to view this guide again.
"""
def process_documents_wrapper(inputs):
snippets = process_documents(inputs)
st.session_state.retriever = create_retriever(snippets)
st.session_state.source_doc_urls = inputs
st.session_state.index = [snip.metadata["header"] for snip in snippets]
response = f"Uploaded and processed documents {inputs}"
st.session_state.messages.append((f"/upload {inputs}", response, ""))
return response
def index_documents_wrapper(inputs=None):
response = pd.Series(st.session_state.index, name="references").to_markdown()
st.session_state.messages.append(("/index", response, ""))
return response
def calculate_cost_wrapper(inputs=None):
try:
stats_df = pd.DataFrame(st.session_state.costing)
stats_df.loc["total"] = stats_df.sum()
response = stats_df.to_markdown()
except ValueError:
response = "No cost incurred yet"
st.session_state.messages.append(("/cost", response, ""))
return response
def download_conversation_wrapper(inputs=None):
conversation_data = json.dumps(
{
"document_urls": (
st.session_state.source_doc_urls
if "source_doc_urls" in st.session_state
else []
),
"document_snippets": (
st.session_state.index if "index" in st.session_state else []
),
"conversation": [
{"human": message[0], "ai": message[1], "references": message[2]}
for message in st.session_state.messages
],
"costing": (
st.session_state.costing if "costing" in st.session_state else []
),
"total_cost": (
{
k: sum(d[k] for d in st.session_state.costing)
for k in st.session_state.costing[0]
}
if "costing" in st.session_state and len(st.session_state.costing) > 0
else {}
),
}
)
conversation_data = base64.b64encode(conversation_data.encode()).decode()
st.session_state.messages.append(("/download", "Conversation data downloaded", ""))
return f'<a href="data:text/csv;base64,{conversation_data}" download="conversation_data.json">Download Conversation</a>'
def query_llm_wrapper(inputs):
retriever = st.session_state.retriever
qa_chain = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(model="gpt-4-0125-preview", temperature=0),
retriever=retriever,
return_source_documents=True,
chain_type="stuff",
)
relevant_docs = retriever.get_relevant_documents(inputs)
with get_openai_callback() as cb:
result = qa_chain(
{
"question": inputs,
"chat_history": session_state_2_llm_chat_history(
st.session_state.messages
),
}
)
stats = cb
result = result["answer"]
references = get_references(relevant_docs)
st.session_state.messages.append((inputs, result, references))
st.session_state.costing.append(
{
"prompt tokens": stats.prompt_tokens,
"completion tokens": stats.completion_tokens,
"cost": stats.total_cost,
}
)
return result, references
def boot(command_center):
st.write("# Agent Zeta")
if "costing" not in st.session_state:
st.session_state.costing = []
if "messages" not in st.session_state:
st.session_state.messages = []
st.chat_message("ai").write(welcome_message, unsafe_allow_html=True)
for message in st.session_state.messages:
st.chat_message("human").write(message[0])
st.chat_message("ai").write(
ai_message_format(message[1], message[2]), unsafe_allow_html=True
)
if query := st.chat_input():
st.chat_message("human").write(query)
response = command_center.execute_command(query)
if response is None:
pass
elif type(response) == tuple:
result, references = response
st.chat_message("ai").write(
ai_message_format(result, references), unsafe_allow_html=True
)
else:
st.chat_message("ai").write(response, unsafe_allow_html=True)
if __name__ == "__main__":
all_commands = [
("/upload", list, process_documents_wrapper),
("/index", None, index_documents_wrapper),
("/cost", None, calculate_cost_wrapper),
("/download", None, download_conversation_wrapper),
("/man", None, lambda x: welcome_message),
]
command_center = CommandCenter(
default_input_type=str,
default_function=query_llm_wrapper,
all_commands=all_commands,
)
boot(command_center) |