BaranziniLab / KG_RAG

Empower Large Language Models (LLM) using Knowledge Graph based Retrieval-Augmented Generation (KG-RAG) for knowledge intensive tasks
Apache License 2.0
624 stars 84 forks source link

issues with key format #14

Closed cswangxiaowei closed 6 months ago

cswangxiaowei commented 8 months ago

May I know if my format for the API_key is correct? I use API_KEY=XXXXXX, and put the key directly in /workspaces/KG_RAG/.gpt_config.env. However, after finishing setup and building the vectorDB, I cannot call OPENAI. P.S. My key is correct, and I try to generate a new key, but it is not working.

The error is: (kg_rag) @cswangxiaowei ➜ /workspaces/KG_RAG (main) $ python -m kg_rag.rag_based_generation.GPT.text_generation -g "gpt-4"

Enter your question : who are you? Retrieving context from SPOKE graph... Calling OpenAI... Calling OpenAI... Calling OpenAI... Calling OpenAI... Calling OpenAI... Traceback (most recent call last): File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 382, in call result = fn(*args, *kwargs) File "/workspaces/KG_RAG/kg_rag/utility.py", line 183, in fetch_GPT_response response = openai.ChatCompletion.create( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 25, in create return super().create(args, **kwargs) File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 151, in create ) = cls.__prepare_create_request( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 108, in prepare_create_request requestor = api_requestor.APIRequestor( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_requestor.py", line 139, in init__ self.api_key = key or util.default_api_key() File "/home/codespace/.local/lib/python3.10/site-packages/openai/util.py", line 186, in default_api_key raise openai.error.AuthenticationError( openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/opt/conda/envs/kg_rag/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "/opt/conda/envs/kg_rag/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "/workspaces/KG_RAG/kg_rag/rag_based_generation/GPT/text_generation.py", line 56, in main() File "/workspaces/KG_RAG/kg_rag/rag_based_generation/GPT/text_generation.py", line 44, in main context = retrieve_context(question, vectorstore, embedding_function_for_context_retrieval, node_context_df, CONTEXT_VOLUME, QUESTION_VS_CONTEXT_SIMILARITY_PERCENTILE_THRESHOLD, QUESTION_VS_CONTEXT_MINIMUM_SIMILARITY) File "/workspaces/KG_RAG/kg_rag/utility.py", line 248, in retrieve_context entities = disease_entity_extractor_v2(question) File "/workspaces/KG_RAG/kg_rag/utility.py", line 232, in disease_entity_extractor_v2 resp = get_GPT_response(prompt_updated, system_prompts["DISEASE_ENTITY_EXTRACTION"], chat_model_id, chat_deployment_id, temperature=0) File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 655, in call return self._cached_call(args, kwargs)[0] File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 598, in _cached_call out, metadata = self.call(*args, kwargs) File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 856, in call output = self.func(*args, *kwargs) File "/workspaces/KG_RAG/kg_rag/utility.py", line 203, in get_GPT_response return fetch_GPT_response(instruction, system_prompt, chat_model_id, chat_deployment_id, temperature) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 289, in wrapped_f return self(f, args, kw) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 379, in call do = self.iter(retry_state=retry_state) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 326, in iter raise retry_exc from fut.exception() tenacity.RetryError: RetryError[<Future at 0x7f5c931e94b0 state=finished raised AuthenticationError>]

karthiksoman commented 8 months ago

Hi @cswangxiaowei Here the issue is that your API_KEY is not accessible for the openai library to authenticate. This is because your .gpt_config.env file is in /workspaces/KG_RAG folder. There are two solutions for this:

Solution 1: Move .gpt_config.env to your $HOME folder and retry running it.

Solution 2: Keep .gpt_config.env where it currently is, but update this line of config.yaml with the correct path of your .gpt_config.env file.

On a different note: Since the objective of KG-RAG is to address biomedical queries, I would strongly suggest to try some disease related queries to test it (instead of non-biomedical queries like Who are you?. For such queries you can use direct GPT calls) An example query can be (feel free to change it as per your interest): What are the genes associated with multiple sclerosis?

Let me know if this issue gets resolved for you.

cswangxiaowei commented 8 months ago

Thank you for the reply! However, I choose solution 2 and try to ask: What are the genes associated with multiple sclerosis? but receive the error again. Besides, I try to use GPT4 api in other respos and it works. The error is: (kg_rag) @cswangxiaowei ➜ /workspaces/KG_RAG (main) $ python -m kg_rag.rag_based_generation.GPT.text_generation -g "gpt-4"

Enter your question : What are the genes associated with multiple sclerosis? Retrieving context from SPOKE graph... Here is the KG-RAG based answer:

Calling OpenAI... Calling OpenAI... Calling OpenAI... Calling OpenAI... Calling OpenAI... Traceback (most recent call last): File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 382, in call result = fn(*args, *kwargs) File "/workspaces/KG_RAG/kg_rag/utility.py", line 183, in fetch_GPT_response response = openai.ChatCompletion.create( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 25, in create return super().create(args, **kwargs) File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_resources/abstract/engine_apiresource.py", line 155, in create response, , api_key = requestor.request( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_requestor.py", line 299, in request resp, got_stream = self._interpret_response(result, stream) File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_requestor.py", line 710, in _interpret_response self._interpret_response_line( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_requestor.py", line 775, in _interpret_response_line raise self.handle_error_response( openai.error.InvalidRequestError: Invalid URL (POST /v1/engines/gpt-4/chat/completions)

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/opt/conda/envs/kg_rag/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "/opt/conda/envs/kg_rag/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "/workspaces/KG_RAG/kg_rag/rag_based_generation/GPT/text_generation.py", line 56, in main() File "/workspaces/KG_RAG/kg_rag/rag_based_generation/GPT/text_generation.py", line 48, in main output = get_GPT_response(enriched_prompt, SYSTEM_PROMPT, CHAT_MODEL_ID, CHAT_DEPLOYMENT_ID, temperature=TEMPERATURE) File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 655, in call return self._cached_call(args, kwargs)[0] File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 598, in _cached_call out, metadata = self.call(*args, kwargs) File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 856, in call output = self.func(*args, *kwargs) File "/workspaces/KG_RAG/kg_rag/utility.py", line 203, in get_GPT_response return fetch_GPT_response(instruction, system_prompt, chat_model_id, chat_deployment_id, temperature) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 289, in wrapped_f return self(f, args, kw) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 379, in call do = self.iter(retry_state=retry_state) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 326, in iter raise retry_exc from fut.exception() tenacity.RetryError: RetryError[<Future at 0x7fae2c858e20 state=finished raised InvalidRequestError>]

karthiksoman commented 8 months ago

Can you please double check the path of your .gpt_config.env is the same as that provided in the config.yaml file?

Also, here is the template of .gpt_config.env file: API_KEY= API_VERSION=<API version, this is totally optional> RESOURCE_ENDPOINT=<API resource endpoint, this is totally optional>

Davy-Chendy commented 7 months ago

Thank you for the reply! However, I choose solution 2 and try to ask: What are the genes associated with multiple sclerosis? but receive the error again. Besides, I try to use GPT4 api in other respos and it works. The error is: (kg_rag) @cswangxiaowei ➜ /workspaces/KG_RAG (main) $ python -m kg_rag.rag_based_generation.GPT.text_generation -g "gpt-4"

Enter your question : What are the genes associated with multiple sclerosis? Retrieving context from SPOKE graph... Here is the KG-RAG based answer:

Calling OpenAI... Calling OpenAI... Calling OpenAI... Calling OpenAI... Calling OpenAI... Traceback (most recent call last): File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 382, in call result = fn(*args, *kwargs) File "/workspaces/KG_RAG/kg_rag/utility.py", line 183, in fetch_GPT_response response = openai.ChatCompletion.create( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 25, in create return super().create(args, **kwargs) File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_resources/abstract/engine_apiresource.py", line 155, in create response, , api_key = requestor.request( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_requestor.py", line 299, in request resp, got_stream = self._interpret_response(result, stream) File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_requestor.py", line 710, in _interpret_response self._interpret_response_line( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_requestor.py", line 775, in _interpret_response_line raise self.handle_error_response( openai.error.InvalidRequestError: Invalid URL (POST /v1/engines/gpt-4/chat/completions)

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/opt/conda/envs/kg_rag/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "/opt/conda/envs/kg_rag/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "/workspaces/KG_RAG/kg_rag/rag_based_generation/GPT/text_generation.py", line 56, in main() File "/workspaces/KG_RAG/kg_rag/rag_based_generation/GPT/text_generation.py", line 48, in main output = get_GPT_response(enriched_prompt, SYSTEM_PROMPT, CHAT_MODEL_ID, CHAT_DEPLOYMENT_ID, temperature=TEMPERATURE) File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 655, in call return self._cached_call(args, kwargs)[0] File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 598, in _cached_call out, metadata = self.call(*args, kwargs) File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 856, in call output = self.func(*args, kwargs) File "/workspaces/KG_RAG/kg_rag/utility.py", line 203, in get_GPT_response return fetch_GPT_response(instruction, system_prompt, chat_model_id, chat_deployment_id, temperature) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init*.py", line 289, in wrapped_f return self(f, args, kw) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 379, in call do = self.iter(retry_state=retry_state) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 326, in iter raise retry_exc from fut.exception() tenacity.RetryError: RetryError[<Future at 0x7fae2c858e20 state=finished raised InvalidRequestError>]

I have the same problem as you. Did you solve it?

Davy-Chendy commented 7 months ago

Thank you for the reply! However, I choose solution 2 and try to ask: What are the genes associated with multiple sclerosis? but receive the error again. Besides, I try to use GPT4 api in other respos and it works. The error is: (kg_rag) @cswangxiaowei ➜ /workspaces/KG_RAG (main) $ python -m kg_rag.rag_based_generation.GPT.text_generation -g "gpt-4"

Enter your question : What are the genes associated with multiple sclerosis? Retrieving context from SPOKE graph... Here is the KG-RAG based answer:

Calling OpenAI... Calling OpenAI... Calling OpenAI... Calling OpenAI... Calling OpenAI... Traceback (most recent call last): File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 382, in call result = fn(*args, *kwargs) File "/workspaces/KG_RAG/kg_rag/utility.py", line 183, in fetch_GPT_response response = openai.ChatCompletion.create( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_resources/chat_completion.py", line 25, in create return super().create(args, **kwargs) File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_resources/abstract/engine_apiresource.py", line 155, in create response, , api_key = requestor.request( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_requestor.py", line 299, in request resp, got_stream = self._interpret_response(result, stream) File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_requestor.py", line 710, in _interpret_response self._interpret_response_line( File "/home/codespace/.local/lib/python3.10/site-packages/openai/api_requestor.py", line 775, in _interpret_response_line raise self.handle_error_response( openai.error.InvalidRequestError: Invalid URL (POST /v1/engines/gpt-4/chat/completions)

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/opt/conda/envs/kg_rag/lib/python3.10/runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "/opt/conda/envs/kg_rag/lib/python3.10/runpy.py", line 86, in _run_code exec(code, run_globals) File "/workspaces/KG_RAG/kg_rag/rag_based_generation/GPT/text_generation.py", line 56, in main() File "/workspaces/KG_RAG/kg_rag/rag_based_generation/GPT/text_generation.py", line 48, in main output = get_GPT_response(enriched_prompt, SYSTEM_PROMPT, CHAT_MODEL_ID, CHAT_DEPLOYMENT_ID, temperature=TEMPERATURE) File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 655, in call return self._cached_call(args, kwargs)[0] File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 598, in _cached_call out, metadata = self.call(*args, kwargs) File "/home/codespace/.local/lib/python3.10/site-packages/joblib/memory.py", line 856, in call output = self.func(*args, kwargs) File "/workspaces/KG_RAG/kg_rag/utility.py", line 203, in get_GPT_response return fetch_GPT_response(instruction, system_prompt, chat_model_id, chat_deployment_id, temperature) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init*.py", line 289, in wrapped_f return self(f, args, kw) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 379, in call do = self.iter(retry_state=retry_state) File "/home/codespace/.local/lib/python3.10/site-packages/tenacity/init.py", line 326, in iter raise retry_exc from fut.exception() tenacity.RetryError: RetryError[<Future at 0x7fae2c858e20 state=finished raised InvalidRequestError>]

I upgraded the openai to the latest version using pip install --upgrade openaiand modified the fetch_GPT_response function as follows:

client = openai.OpenAI(api_key="your api key")

@retry(wait=wait_random_exponential(min=10, max=30), stop=stop_after_attempt(5))
def fetch_GPT_response(instruction, system_prompt, chat_model_id, chat_deployment_id, temperature=0):
    print('Calling OpenAI...')
    response = client.chat.completions.create(
        model=chat_model_id,
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": instruction}
        ],
        temperature=temperature,
    )
    return response.choices[0].message.content.strip()

This modification was effective.

karthiksoman commented 7 months ago

Thanks @Davy-Chendy for the fix! May I know which openai version you used for this? Also, does the existing fetch_GPT_response function is compatible with the openai version that you used?

Davy-Chendy commented 6 months ago

Thanks @Davy-Chendy for the fix! May I know which openai version you used for this? Also, does the existing fetch_GPT_response function is compatible with the openai version that you used?

My current openai is the latest version currently updated (2024-3-2) using the command pip install --upgrade openai, which is 1.13.3. The existingfetch_GPT_responsefunction is not compatible with the current openai version because the usage of some functions in the new version of the openai library has changed. For details, please refer to my reply above, or openai's official documentation.

karthiksoman commented 6 months ago

Thanks @Davy-Chendy And you are able to get KG-RAG based response using the latest openai version?

Davy-Chendy commented 6 months ago

Thanks @Davy-Chendy And you are able to get KG-RAG based response using the latest openai version?

yep

karthiksoman commented 6 months ago

Awesome! Then I am closing this issue since it is resolved!