2023-05-13 00:05:31 +03:00
|
|
|
import streamlit as st
|
2023-05-14 11:30:03 +03:00
|
|
|
from streamlit.logger import get_logger
|
2023-05-13 00:05:31 +03:00
|
|
|
from langchain.chains import ConversationalRetrievalChain
|
|
|
|
from langchain.memory import ConversationBufferMemory
|
|
|
|
from langchain.llms import OpenAI
|
2023-05-14 11:30:03 +03:00
|
|
|
from langchain.chat_models import ChatAnthropic
|
|
|
|
from langchain.vectorstores import SupabaseVectorStore
|
2023-05-13 00:05:31 +03:00
|
|
|
|
2023-05-14 11:30:03 +03:00
|
|
|
memory = ConversationBufferMemory(
|
|
|
|
memory_key="chat_history", return_messages=True)
|
|
|
|
openai_api_key = st.secrets.openai_api_key
|
|
|
|
anthropic_api_key = st.secrets.anthropic_api_key
|
|
|
|
logger = get_logger(__name__)
|
2023-05-13 00:05:31 +03:00
|
|
|
|
2023-05-14 11:30:03 +03:00
|
|
|
|
|
|
|
def chat_with_doc(model, vector_store: SupabaseVectorStore):
|
|
|
|
question = st.text_area("## Ask a question")
|
2023-05-13 00:05:31 +03:00
|
|
|
button = st.button("Ask")
|
|
|
|
if button:
|
2023-05-14 11:30:03 +03:00
|
|
|
if model.startswith("gpt"):
|
|
|
|
logger.info('Using OpenAI model %s', model)
|
|
|
|
qa = ConversationalRetrievalChain.from_llm(
|
|
|
|
OpenAI(
|
|
|
|
model_name=st.session_state['model'], openai_api_key=openai_api_key, temperature=st.session_state['temperature'], max_tokens=st.session_state['max_tokens']), vector_store.as_retriever(), memory=memory, verbose=True)
|
|
|
|
result = qa({"question": question})
|
|
|
|
logger.info('Result: %s', result)
|
|
|
|
st.write(result["answer"])
|
|
|
|
elif anthropic_api_key and model.startswith("claude"):
|
|
|
|
logger.info('Using Anthropics model %s', model)
|
|
|
|
qa = ConversationalRetrievalChain.from_llm(
|
|
|
|
ChatAnthropic(
|
|
|
|
model=st.session_state['model'], anthropic_api_key=anthropic_api_key, temperature=st.session_state['temperature'], max_tokens_to_sample=st.session_state['max_tokens']), vector_store.as_retriever(), memory=memory, verbose=True, max_tokens_limit=102400)
|
|
|
|
result = qa({"question": question})
|
|
|
|
logger.info('Result: %s', result)
|
|
|
|
st.write(result["answer"])
|