Merge pull request #18 from Shaunwei/support-anthropic-100k

Update Readme and add token counts
This commit is contained in:
Stan Girard 2023-05-15 09:22:52 +02:00 committed by GitHub
commit 35690ef656
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 0 deletions

View File

@ -80,6 +80,7 @@ cp .streamlit/secrets.toml.example .streamlit/secrets.toml
supabase_url = "SUPABASE_URL"
supabase_service_key = "SUPABASE_SERVICE_KEY"
openai_api_key = "OPENAI_API_KEY"
anthropic_api_key = "ANTHROPIC_API_KEY" # Optional
```
- Run the migration script on the Supabase database via the web interface

View File

@ -1,3 +1,4 @@
import anthropic
import streamlit as st
from streamlit.logger import get_logger
from langchain.chains import ConversationalRetrievalChain
@ -13,9 +14,17 @@ anthropic_api_key = st.secrets.anthropic_api_key
logger = get_logger(__name__)
def count_tokens(question, model):
count = f'Words: {len(question.split())}'
if model.startswith("claude"):
count += f' | Tokens: {anthropic.count_tokens(question)}'
return count
def chat_with_doc(model, vector_store: SupabaseVectorStore):
question = st.text_area("## Ask a question")
button = st.button("Ask")
count_button = st.button("Count Tokens", type='secondary')
if button:
if model.startswith("gpt"):
logger.info('Using OpenAI model %s', model)
@ -33,3 +42,6 @@ def chat_with_doc(model, vector_store: SupabaseVectorStore):
result = qa({"question": question})
logger.info('Result: %s', result)
st.write(result["answer"])
if count_button:
st.write(count_tokens(question, model))