chroma-core / chroma

the AI-native open-source embedding database
https://www.trychroma.com/
Apache License 2.0
15.24k stars 1.28k forks source link

[Bug]: Global Overwrite of OpenAI API Key During Text Embedding Execution #2979

Open watanabeycr7 opened 2 weeks ago

watanabeycr7 commented 2 weeks ago

What happened?

When the OpenAIEmbeddingFunction() is executed, the api_key provided as an argument overwrites the openai.api_key that is referenced globally throughout the application. This behavior prevents the application from being able to differentiate between the API key used for embeddings and the API key used for regular chat operations.

Here's a snippet of the affected code:

app.logger.info(f"openai.api_key: {openai.api_key[-4:]}")
app.logger.info(f"ANOHTER_OPENAI_API_KEY: {ANOTHER_OPENAI_API_KEY[-4:]}")

embedding_function = embedding_functions.OpenAIEmbeddingFunction(
    api_key=ANOTHER_OPENAI_API_KEY, model_name=EMBEDDING_MODEL
)

app.logger.info(f"openai.api_key: {openai.api_key[-4:]}")

Versions

chroma v0.5.5 flask v2.3.2 python 3.11.6

Relevant log output

2024-10-21 17:34:25 [2024-10-21 08:34:25,464] INFO in internal_chatbot_documents_controller: openai.api_key: pTV4
2024-10-21 17:34:25 [2024-10-21 08:34:25,464] INFO in internal_chatbot_documents_controller: ANOHTER_OPENAI_API_KEY: -EsA
2024-10-21 17:34:25 [2024-10-21 08:34:25,483] INFO in internal_chatbot_documents_controller: openai.api_key: -EsA
watanabeycr7 commented 2 weeks ago

I've identified the issue and have a possible solution in mind. I'll begin working on a PR to address this.