Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langchain: support api key argument with OpenAI moderation chain #29140

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions libs/langchain/langchain/chains/moderation.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def validate_environment(cls, values: Dict) -> Any:
if values["openai_pre_1_0"]:
values["client"] = openai.Moderation
else:
values["client"] = openai.OpenAI()
values["async_client"] = openai.AsyncOpenAI()
values["client"] = openai.OpenAI(api_key=openai_api_key)
values["async_client"] = openai.AsyncOpenAI(api_key=openai_api_key)

except ImportError:
raise ImportError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from langchain.chains import OpenAIModerationChain
from langchain.chains.openai_functions.openapi import get_openapi_chain

api_spec = {
Expand Down Expand Up @@ -36,3 +37,13 @@ def test_openai_openapi_chain() -> None:
chain = get_openapi_chain(json.dumps(api_spec), llm)
output = chain.invoke({"query": "Fetch the top two posts."})
assert len(output["response"]) == 2


@pytest.mark.requires("openai")
def test_openai_moderation_chain_instantiation() -> None:
"""Test OpenAIModerationChain."""
api_key = "foo"

moderation = OpenAIModerationChain(openai_api_key=api_key)

assert isinstance(moderation, OpenAIModerationChain)
Loading