-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
87 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""A client for ChromaDB.""" | ||
import chromadb | ||
|
||
|
||
class ChromaDB: | ||
"""A class for creating a client for ChromaDB.""" | ||
|
||
def __init__(self, path="tmp/chroma"): | ||
"""Initialize the client.""" | ||
self.client = chromadb.PersistentClient(path=path) | ||
self.client.heartbeat() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"""Create a Chroma collection.""" | ||
from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction | ||
import streamlit as st | ||
|
||
|
||
def create_collection(client, name="pdf-explainer", api_key=None): | ||
"""Create a Chroma collection.""" | ||
try: | ||
embedding_function = OpenAIEmbeddingFunction( | ||
api_key=api_key, model_name="text-embedding-ada-002" | ||
) | ||
collection = client.get_or_create_collection( | ||
name=name, embedding_function=embedding_function | ||
) | ||
return collection | ||
except AttributeError as e: | ||
st.error("An error ocurred while creating the collection." + e.with_traceback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Streamlit GUI messages.""" | ||
import streamlit as st | ||
|
||
|
||
def api_message(api_key): | ||
"""Inform if the api key is set.""" | ||
if api_key is None: | ||
return st.warning("Add your OpenAI API key") | ||
|
||
return st.success("Your API key is setup ") | ||
|
||
|
||
def how_to_create_api_key_message(api_key): | ||
"""Inform how to create an api key.""" | ||
if api_key is None: | ||
return st.write( | ||
"You can find your API key at https://beta.openai.com/account/api-keys" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""OpenAI client creator.""" | ||
import os | ||
from openai import OpenAI | ||
|
||
|
||
def create_client(api_key=None): | ||
"""Create an OpenAI client.""" | ||
if os.getenv("OPENAI_API_KEY"): | ||
api_key = os.getenv("OPENAI_API_KEY") | ||
|
||
client = OpenAI(api_key=api_key) | ||
|
||
return client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from src.openai_client import create_client | ||
from src.collection_creator import create_collection | ||
|
||
|
||
def build(chroma_client, api_key=None): | ||
"""Build the app.""" | ||
client = create_client(api_key=api_key) | ||
collection = create_collection(chroma_client, api_key=api_key) | ||
|
||
return client, collection |