Skip to content

Commit

Permalink
remove openai_api_key as global env
Browse files Browse the repository at this point in the history
  • Loading branch information
LuizFrra committed Feb 7, 2024
1 parent 812546d commit 0bbe5e9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
11 changes: 4 additions & 7 deletions libs/community/tests/unit_tests/chat_models/test_openai.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Test OpenAI Chat API wrapper."""
import json
import os
from typing import Any
from unittest.mock import MagicMock, patch

Expand All @@ -15,14 +14,12 @@
from langchain_community.adapters.openai import convert_dict_to_message
from langchain_community.chat_models.openai import ChatOpenAI

os.environ["OPENAI_API_KEY"] = "foo"


@pytest.mark.requires("openai")
def test_openai_model_param() -> None:
llm = ChatOpenAI(model="foo")
llm = ChatOpenAI(model="foo", openai_api_key="foo")
assert llm.model_name == "foo"
llm = ChatOpenAI(model_name="foo")
llm = ChatOpenAI(model_name="foo", openai_api_key="foo")
assert llm.model_name == "foo"


Expand Down Expand Up @@ -84,7 +81,7 @@ def mock_completion() -> dict:

@pytest.mark.requires("openai")
def test_openai_predict(mock_completion: dict) -> None:
llm = ChatOpenAI()
llm = ChatOpenAI(openai_api_key="foo")
mock_client = MagicMock()
completed = False

Expand All @@ -106,7 +103,7 @@ def mock_create(*args: Any, **kwargs: Any) -> Any:

@pytest.mark.requires("openai")
async def test_openai_apredict(mock_completion: dict) -> None:
llm = ChatOpenAI()
llm = ChatOpenAI(openai_api_key="foo")
mock_client = MagicMock()
completed = False

Expand Down
6 changes: 1 addition & 5 deletions libs/community/tests/unit_tests/embeddings/test_openai.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import os

import pytest

from langchain_community.embeddings.openai import OpenAIEmbeddings

os.environ["OPENAI_API_KEY"] = "foo"


@pytest.mark.requires("openai")
def test_openai_invalid_model_kwargs() -> None:
Expand All @@ -16,5 +12,5 @@ def test_openai_invalid_model_kwargs() -> None:
@pytest.mark.requires("openai")
def test_openai_incorrect_field() -> None:
with pytest.warns(match="not default parameter"):
llm = OpenAIEmbeddings(foo="bar")
llm = OpenAIEmbeddings(foo="bar", openai_api_key="foo")
assert llm.model_kwargs == {"foo": "bar"}
12 changes: 4 additions & 8 deletions libs/community/tests/unit_tests/llms/test_openai.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import os

import pytest

from langchain_community.llms.openai import OpenAI
from langchain_community.utils.openai import is_openai_v1

os.environ["OPENAI_API_KEY"] = "foo"


def _openai_v1_installed() -> bool:
try:
Expand All @@ -17,15 +13,15 @@ def _openai_v1_installed() -> bool:

@pytest.mark.requires("openai")
def test_openai_model_param() -> None:
llm = OpenAI(model="foo")
llm = OpenAI(model="foo", openai_api_key="foo")
assert llm.model_name == "foo"
llm = OpenAI(model_name="foo")
llm = OpenAI(model_name="foo", openai_api_key="foo")
assert llm.model_name == "foo"


@pytest.mark.requires("openai")
def test_openai_model_kwargs() -> None:
llm = OpenAI(model_kwargs={"foo": "bar"})
llm = OpenAI(model_kwargs={"foo": "bar"}, openai_api_key="foo")
assert llm.model_kwargs == {"foo": "bar"}


Expand All @@ -42,7 +38,7 @@ def test_openai_invalid_model_kwargs() -> None:
@pytest.mark.requires("openai")
def test_openai_incorrect_field() -> None:
with pytest.warns(match="not default parameter"):
llm = OpenAI(foo="bar")
llm = OpenAI(foo="bar", openai_api_key="foo")
assert llm.model_kwargs == {"foo": "bar"}


Expand Down

0 comments on commit 0bbe5e9

Please sign in to comment.