mirror of
https://github.com/hwchase17/langchain.git
synced 2025-05-19 22:11:41 +00:00
langchain: support api key argument with OpenAI moderation chain (#29140)
**Description:** Makes it possible to instantiate `OpenAIModerationChain` with an `openai_api_key` argument only and no `OPENAI_API_KEY` environment variable defined. **Issue:** https://github.com/langchain-ai/langchain/issues/25176 **Dependencies:** `openai` --------- Co-authored-by: ccurme <chester.curme@gmail.com>
This commit is contained in:
parent
335ca3a606
commit
e156b372fb
@ -67,8 +67,8 @@ class OpenAIModerationChain(Chain):
|
||||
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(
|
||||
|
@ -2,6 +2,7 @@ import json
|
||||
|
||||
import pytest
|
||||
|
||||
from langchain.chains import OpenAIModerationChain
|
||||
from langchain.chains.openai_functions.openapi import get_openapi_chain
|
||||
|
||||
api_spec = {
|
||||
@ -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…
Reference in New Issue
Block a user