Repository: Giskard-AI/giskard-oss
Issue: [GSK-1364] Error when running Langchain
Root cause: Documentation Deficiency

 I want to run my dataset with Langchain using Giskard, I did as this documentation(https://docs.giskard.ai/en/latest/reference/notebooks/llm_comment_generation.html) just replace with my dataset and my openai key but i got this error. Please someone help me to handle this  issue!
 
```
 2023-06-26 15:08:29,898 pid:15616 MainThread giskard.scanner.logger WARNING  LLM support is in alpha version — 🔥 things may break ! Please report any issues to https://github.com/giskard-AI/giskard/issues.
🔎 Running scan…
2023-06-26 15:08:29,903 pid:15616 MainThread giskard.scanner.logger INFO     Running detectors: ['TextPerturbationDetector', 'EthicalBiasDetector', 'LLMToxicityDetector']
Running detector TextPerturbationDetector…2023-06-26 15:08:29,907 pid:15616 MainThread giskard.scanner.logger INFO     TextPerturbationDetector: Running with transformations=['Transform to uppercase', 'Transform to lowercase', 'Transform to title case', 'Add typos', 'Punctuation Removal'] threshold=None output_sensitivity=None num_samples=None
2023-06-26 15:08:30,462 pid:15616 MainThread openai       INFO     error_code=invalid_api_key error_message='Incorrect API key provided: mf39wE1W************************************scCS. You can find your API key at https://platform.openai.com/account/api-keys.' error_param=None error_type=invalid_request_error message='OpenAI API error received' stream_error=False
2023-06-26 15:08:30,466 pid:15616 MainThread giskard.scanner.logger ERROR    Detector TextPerturbationDetector failed with error: Incorrect API key provided: mf39wE1W************************************scCS. You can find your API key at https://platform.openai.com/account/api-keys.
 0 issues detected. (Took 0:00:00.568313)
Running detector EthicalBiasDetector…2023-06-26 15:08:30,484 pid:15616 MainThread giskard.scanner.logger INFO     EthicalBiasDetector: Running with transformations=['Switch Gender', 'Switch Religion', 'Switch countries from high- to low-income and vice versa'] threshold=None output_sensitivity=None num_samples=None
2023-06-26 15:08:30,884 pid:15616 MainThread openai       INFO     error_code=invalid_api_key error_message='Incorrect API key provided: mf39wE1W************************************scCS. You can find your API key at https://platform.openai.com/account/api-keys.' error_param=None error_type=invalid_request_error message='OpenAI API error received' stream_error=False
2023-06-26 15:08:30,886 pid:15616 MainThread giskard.scanner.logger ERROR    Detector EthicalBiasDetector failed with error: Incorrect API key provided: mf39wE1W************************************scCS. You can find your API key at https://platform.openai.com/account/api-keys.
 0 issues detected. (Took 0:00:00.405398)
Running detector LLMToxicityDetector…2023-06-26 15:08:31,418 pid:15616 MainThread openai       INFO     error_code=invalid_api_key error_message='Incorrect API key provided: mf39wE1W************************************scCS. You can find your API key at https://platform.openai.com/account/api-keys.' error_param=None error_type=invalid_request_error message='OpenAI API error received' stream_error=False
2023-06-26 15:08:31,421 pid:15616 MainThread giskard.scanner.logger ERROR    Detector LLMToxicityDetector failed with error: Incorrect API key provided: mf39wE1W************************************scCS. You can find your API key at https://platform.openai.com/account/api-keys.
 0 issues detected. (Took 0:00:00.535858)
Scan completed: no issues found. (Took 0:00:01.544884)
```
<sub>[GSK-1364](https://linear.app/giskard/issue/GSK-1364/error-when-running-langchain)</sub>

Hi @refik90 , 

Thanks for pointing it out, there's typo in our documentation, you can create an llm using this line of code instead:
```
OPENAI_API_KEY=...
llm = OpenAI(openai_api_key=OPENAI_API_KEY, request_timeout=15, max_retries=50, temperature=0)
```

or even better store the API key in the environment variable `OPENAI_API_KEY` (in ~/.bashrc or ~/.zshrc for example):
```
export OPENAI_API_KEY=...
```

and then do the following 

```
import os
llm = OpenAI(openai_api_key=os.environ["OPENAI_API_KEY"], request_timeout=15, max_retries=50, temperature=0)
```

> Hi @refik90 ,
> 
> Thanks for pointing it out, there's typo in our documentation, you can create an llm using this line of code instead:
> 
> ```
> OPENAI_API_KEY=...
> llm = OpenAI(openai_api_key=OPENAI_API_KEY, request_timeout=15, max_retries=50, temperature=0)
> ```
> 
> or even better store the API key in the environment variable `OPENAI_API_KEY` (in ~/.bashrc or ~/.zshrc for example):
> 
> ```
> export OPENAI_API_KEY=...
> ```
> 
> and then do the following
> 
> ```
> import os
> llm = OpenAI(openai_api_key=os.environ["OPENAI_API_KEY"], request_timeout=15, max_retries=50, temperature=0)
> ```

in the code where i need to add this ?
> or even better store the API key in the environment variable `OPENAI_API_KEY` (in ~/.bashrc or ~/.zshrc for example):
> 
> ```
> export OPENAI_API_KEY=...


In addition, from my experiences, the envs are not likely to be serialized and sent to the worker.
If you would like to upload the model and test it in a worker, you may also need to provide the OpenAI API key in the envs of the worker.

> ([https://docs.giskard.ai/en/latest/](https://docs.giskard.ai/en/latest/reference/notebooks/llm_comment_generation.html)

I have openAI Api key and i added on this line OPENAI_API_KEY=...
llm = OpenAI(openai_api_key=OPENAI_API_KEY, request_timeout=15, max_retries=50, temperature=0)

But still is not working

> in the code where i need to add this ?

If you're using mac/linux you can edit ~/.zshrc or ~/.bashrc (depending on which shell you use) by running the following:

```bash
# in case you use zsh:
echo "export OPENAI_API_KEY=..." >> ~/.zshrc

# in case you use bash
echo "export OPENAI_API_KEY=..." >> ~/.bashrc
```

Then restart your terminal and you'll have OPENAI_API_KEY environment variable available when running

```
import os
os.environ["OPENAI_API_KEY"]
```


> > in the code where i need to add this ?
> 
> If you're using mac/linux you can edit ~/.zshrc or ~/.bashrc (depending on which shell you use) by running the following:
> 
> ```shell
> # in case you use zsh:
> echo "export OPENAI_API_KEY=..." >> ~/.zshrc
> 
> # in case you use bash
> echo "export OPENAI_API_KEY=..." >> ~/.bashrc
> ```
> 
> Then restart your terminal and you'll have OPENAI_API_KEY environment variable available when running
> 
> ```
> import os
> os.environ["OPENAI_API_KEY"]
> ```

i am using windows and my jupyter notebook is working with anaconda prompt, how is for windows?

In this case have you tried specifying an opanai API key directly as the langchain documentation suggests:

[https://python.langchain.com/docs/get_started/quickstart#environment-setup](https://python.langchain.com/docs/get_started/quickstart#environment-setup)

e.g.

```
from langchain.llms import OpenAI

llm = OpenAI(openai_api_key="...")
```

<!-- From Andrey Avtomonov on Linear -->