|
import os |
|
|
|
|
|
os.environ['OPENAI_API_KEY'] = 'sk-' |
|
|
|
import pandas as pd |
|
import requests |
|
import streamlit as st |
|
from IPython.core.display import HTML |
|
from langchain.agents import AgentType, initialize_agent, load_tools |
|
from langchain.chat_models import ChatOpenAI |
|
from PIL import Image |
|
from rmrkl import ChatZeroShotAgent, RetryAgentExecutor |
|
|
|
from chemcrow.agents import ChemCrow |
|
from chemcrow.agents.prompts import (FORMAT_INSTRUCTIONS, QUESTION_PROMPT, |
|
SUFFIX) |
|
from chemcrow.frontend.streamlit_callback_handler import \ |
|
StreamlitCallbackHandlerChem |
|
from langchain.callbacks import StreamlitCallbackHandler |
|
from chemcrow.frontend.utils import cdk |
|
|
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
ss = st.session_state |
|
|
|
|
|
agent = ChemCrow( |
|
|
|
model='gpt-4', |
|
temp=0.1, |
|
).agent_executor |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
icon = Image.open('assets/logo0.png') |
|
st.set_page_config( |
|
page_title="ChemCrow", |
|
page_icon = icon |
|
) |
|
|
|
|
|
st.markdown( |
|
""" |
|
<style> |
|
[data-testid="stSidebar"][aria-expanded="true"]{ |
|
min-width: 450px; |
|
max-width: 450px; |
|
} |
|
""", |
|
unsafe_allow_html=True, |
|
) |
|
|
|
|
|
|
|
def on_api_key_change(): |
|
api_key = ss.get('api_key') or os.getenv('OPENAI_API_KEY') |
|
os.environ["OPENAI_API_KEY"] = api_key |
|
|
|
|
|
|
|
with st.sidebar: |
|
chemcrow_logo = Image.open('assets/chemcrow-logo-bold-new.png') |
|
st.image(chemcrow_logo) |
|
|
|
|
|
st.markdown('Input your OpenAI API key.') |
|
st.text_input('OpenAI API key', type='password', key='api_key', on_change=on_api_key_change, label_visibility="collapsed") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(st.session_state) |
|
|
|
if prompt := st.chat_input(): |
|
st.chat_message("user").write(prompt) |
|
with st.chat_message("assistant"): |
|
st_callback = StreamlitCallbackHandlerChem( |
|
st.container(), |
|
max_thought_containers = 4, |
|
collapse_completed_thoughts = False, |
|
output_placeholder=st.session_state |
|
) |
|
|
|
|
|
response = agent.run(prompt, callbacks=[st_callback]) |
|
st.write(response) |
|
|
|
|
|
|
|
|
|
|