2023-02-14 06:06:25 +00:00
|
|
|
"""Test PromptLayer OpenAI API wrapper."""
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
from typing import Generator
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_community.llms.loading import load_llm
|
|
|
|
from langchain_community.llms.promptlayer_openai import PromptLayerOpenAI
|
2023-02-14 06:06:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_promptlayer_openai_call() -> None:
|
|
|
|
"""Test valid call to promptlayer openai."""
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = PromptLayerOpenAI(max_tokens=10) # type: ignore[call-arg]
|
2024-04-24 23:39:23 +00:00
|
|
|
output = llm.invoke("Say foo:")
|
2023-02-14 06:06:25 +00:00
|
|
|
assert isinstance(output, str)
|
|
|
|
|
|
|
|
|
|
|
|
def test_promptlayer_openai_extra_kwargs() -> None:
|
|
|
|
"""Test extra kwargs to promptlayer openai."""
|
|
|
|
# Check that foo is saved in extra_kwargs.
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = PromptLayerOpenAI(foo=3, max_tokens=10) # type: ignore[call-arg]
|
2023-02-14 06:06:25 +00:00
|
|
|
assert llm.max_tokens == 10
|
|
|
|
assert llm.model_kwargs == {"foo": 3}
|
|
|
|
|
|
|
|
# Test that if extra_kwargs are provided, they are added to it.
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = PromptLayerOpenAI(foo=3, model_kwargs={"bar": 2}) # type: ignore[call-arg]
|
2023-02-14 06:06:25 +00:00
|
|
|
assert llm.model_kwargs == {"foo": 3, "bar": 2}
|
|
|
|
|
|
|
|
# Test that if provided twice it errors
|
|
|
|
with pytest.raises(ValueError):
|
2024-05-13 18:55:07 +00:00
|
|
|
PromptLayerOpenAI(foo=3, model_kwargs={"foo": 2}) # type: ignore[call-arg]
|
2023-02-14 06:06:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_promptlayer_openai_stop_valid() -> None:
|
|
|
|
"""Test promptlayer openai stop logic on valid configuration."""
|
|
|
|
query = "write an ordered list of five items"
|
2024-05-13 18:55:07 +00:00
|
|
|
first_llm = PromptLayerOpenAI(stop="3", temperature=0) # type: ignore[call-arg]
|
2024-04-24 23:39:23 +00:00
|
|
|
first_output = first_llm.invoke(query)
|
2024-05-13 18:55:07 +00:00
|
|
|
second_llm = PromptLayerOpenAI(temperature=0) # type: ignore[call-arg]
|
2024-04-24 23:39:23 +00:00
|
|
|
second_output = second_llm.invoke(query, stop=["3"])
|
2023-02-14 06:06:25 +00:00
|
|
|
# Because it stops on new lines, shouldn't return anything
|
|
|
|
assert first_output == second_output
|
|
|
|
|
|
|
|
|
|
|
|
def test_promptlayer_openai_stop_error() -> None:
|
|
|
|
"""Test promptlayer openai stop logic on bad configuration."""
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = PromptLayerOpenAI(stop="3", temperature=0) # type: ignore[call-arg]
|
2023-02-14 06:06:25 +00:00
|
|
|
with pytest.raises(ValueError):
|
2024-04-24 23:39:23 +00:00
|
|
|
llm.invoke("write an ordered list of five items", stop=["\n"])
|
2023-02-14 06:06:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_saving_loading_llm(tmp_path: Path) -> None:
|
|
|
|
"""Test saving/loading an promptlayer OpenAPI LLM."""
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = PromptLayerOpenAI(max_tokens=10) # type: ignore[call-arg]
|
2023-02-14 06:06:25 +00:00
|
|
|
llm.save(file_path=tmp_path / "openai.yaml")
|
|
|
|
loaded_llm = load_llm(tmp_path / "openai.yaml")
|
|
|
|
assert loaded_llm == llm
|
|
|
|
|
|
|
|
|
|
|
|
def test_promptlayer_openai_streaming() -> None:
|
|
|
|
"""Test streaming tokens from promptalyer OpenAI."""
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = PromptLayerOpenAI(max_tokens=10) # type: ignore[call-arg]
|
2023-02-14 06:06:25 +00:00
|
|
|
generator = llm.stream("I'm Pickle Rick")
|
|
|
|
|
|
|
|
assert isinstance(generator, Generator)
|
|
|
|
|
|
|
|
for token in generator:
|
|
|
|
assert isinstance(token["choices"][0]["text"], str)
|