Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 17419

OpenAI from Langchain requires "openai_api_key" even though it is loaded

$
0
0

this is my code:

import osfrom dotenv import load_dotenv,find_dotenvload_dotenv(find_dotenv())print(os.environ.get("OPEN_AI_KEY"))from langchain.llms import OpenAIllm=OpenAI(model_name="text-davinci-003",temperature=0.7,max_tokens=512)print(llm)

when I execute above code I get this error

ValidationError: 1 validation error for OpenAI__root__  Did not find openai_api_key, please add an environment variable `OPENAI_API_KEY` which contains it, or pass  `openai_api_key` as a named parameter. (type=value_error)

docs say

If you'd prefer not to set an environment variable you can pass thekey in directly via the openai_api_key named parameter when initiatingthe OpenAI LLM class:

But already set it and it prints correctly

enter image description here

When I set the llm by passing named param:

llm=OpenAI(openai_api_key="PASSINGCORRECTKEY", model_name="text-davinci-003",temperature=0.7,max_tokens=512)llm("Tell me a joke")

then I get this error:

raise ValueError("Argument `prompt` is expected to be a string. Instead found "            f"{type(prompt)}. If you want to run the LLM on multiple prompts, use ""`generate` instead."         )

UPDATE

env variable initially was set as OPEN_AI_KEY since I copied and pasted from one of my other project which calls chat/completions api. I changed the env to OPENAI_API_KEY not I get this error:

AuthenticationError: Incorrect API key provided: org-Wz3J****************2XK6. You can find your API key at https://platform.openai.com/account/api-keys.

But same api key works when i call "https://api.openai.com/v1/chat/completions" endpoint


Viewing all articles
Browse latest Browse all 17419

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>