llama_index icon indicating copy to clipboard operation
llama_index copied to clipboard

[Bug]: DashScope API Key Cannot Be Retrieved from Environment Variables

Open cpuoverload opened this issue 1 year ago • 1 comments

Bug Description

The DashScope integration in llama-index-llms-dashscope does not retrieve the API key from environment variables. Even if DASHSCOPE_API_KEY is set in the environment, the library requires the api_key to be explicitly passed when initializing DashScope.

Version

0.12.19

Steps to Reproduce

  1. Create .env file and set the environment variable:
export DASHSCOPE_API_KEY="your_api_key_here"
  1. Initialize DashScope without specifying api_key
from dotenv import load_dotenv
from llama_index.llms.dashscope import DashScope, DashScopeGenerationModels

load_dotenv(".env")

llm = DashScope(model_name=DashScopeGenerationModels.QWEN_PLUS)
response = llm.complete("Who are you?")
print(response.text)
  1. Run the python file

Relevant Logs/Tracbacks

Traceback (most recent call last):
  File "/xxx/test_dashscope.py", line 5, in <module>
    llm = DashScope(model_name=DashScopeGenerationModels.QWEN_PLUS)
  File "/xxx/miniforge3/envs/ragas/lib/python3.10/site-packages/llama_index/llms/dashscope/base.py", line 209, in __init__
    super().__init__(
  File "/xxx/miniforge3/envs/ragas/lib/python3.10/site-packages/llama_index/core/llms/custom.py", line 30, in __init__
    super().__init__(*args, **kwargs)
  File "/xxx/miniforge3/envs/ragas/lib/python3.10/site-packages/pydantic/main.py", line 214, in __init__
    validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for DashScope
api_key
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.10/v/string_type

cpuoverload avatar Feb 26 '25 15:02 cpuoverload

Hey @cpuoverload! 👋 I'm here to help you with any bugs, questions, or contributions you have for llama-index-llms-dashscope. Let's squash those bugs together!

The issue you're encountering with the DashScope integration not retrieving the API key from environment variables might be due to the way the environment variable is being accessed. The code checks for the DASHSCOPE_API_KEY using os.getenv("DASHSCOPE_API_KEY", None), and if it returns None, a ValueError is raised [1].

To resolve this, ensure that the DASHSCOPE_API_KEY environment variable is correctly set and not empty. You can verify this by running echo $DASHSCOPE_API_KEY in your terminal to confirm its value. Additionally, make sure that the environment variable is set in the same context where your Python script is executed.

If the issue persists, you can explicitly set the environment variable in your Python script before initializing DashScope:

import os
os.environ["DASHSCOPE_API_KEY"] = "your_api_key_here"

This ensures that the API key is available to the DashScope integration when it is initialized.

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Bug Report | Other

dosubot[bot] avatar Feb 26 '25 15:02 dosubot[bot]