quivr/question.py
shaun e08995835a Support for Anthropics Models
This update enhances the "Second Brain" application by adding support for Anthropics AI models. Users can now use not only OpenAI's GPT-3/4, but also Anthropics' Claude models to store and query their knowledge.

Key changes include:

Added an anthropic_api_key field in the secrets configuration file. Introduced a selection for different AI models including GPT-3, GPT-4, and various versions of Claude. Updated question handling to be model-agnostic, and added support for Anthropics' Claude models in the question processing workflow. Modified the streamlit interface to allow users to input their choice of model, control the "temperature" of the model's responses, and set the max tokens limit. Upgraded requirements.txt file with the latest version of the Anthropics library. This update empowers users to leverage different AI models based on their needs, providing a more flexible and robust tool for knowledge management.
2023-05-14 01:30:03 -07:00

36 lines
1.8 KiB
Python

import streamlit as st
from streamlit.logger import get_logger
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
from langchain.llms import OpenAI
from langchain.chat_models import ChatAnthropic
from langchain.vectorstores import SupabaseVectorStore
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__)
def chat_with_doc(model, vector_store: SupabaseVectorStore):
question = st.text_area("## Ask a question")
button = st.button("Ask")
if button:
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"])