-
Notifications
You must be signed in to change notification settings - Fork 15.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit-tests back by mocking build_client
- Loading branch information
1 parent
0b8a0aa
commit 3e45658
Showing
4 changed files
with
227 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
libs/community/tests/unit_tests/chat_models/test_outlines.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import pytest | ||
from pydantic import BaseModel, Field | ||
|
||
from langchain_community.chat_models.outlines import ChatOutlines | ||
|
||
|
||
def test_chat_outlines_initialization(monkeypatch): | ||
monkeypatch.setattr(ChatOutlines, "build_client", lambda self: self) | ||
|
||
chat = ChatOutlines( | ||
model="microsoft/Phi-3-mini-4k-instruct", | ||
max_tokens=42, | ||
stop=["\n"], | ||
) | ||
assert chat.model == "microsoft/Phi-3-mini-4k-instruct" | ||
assert chat.max_tokens == 42 | ||
assert chat.backend == "transformers" | ||
assert chat.stop == ["\n"] | ||
|
||
|
||
def test_chat_outlines_backend_llamacpp(monkeypatch): | ||
monkeypatch.setattr(ChatOutlines, "build_client", lambda self: self) | ||
chat = ChatOutlines( | ||
model="TheBloke/Llama-2-7B-Chat-GGUF/llama-2-7b-chat.Q4_K_M.gguf", | ||
backend="llamacpp", | ||
) | ||
assert chat.backend == "llamacpp" | ||
|
||
|
||
def test_chat_outlines_backend_vllm(monkeypatch): | ||
monkeypatch.setattr(ChatOutlines, "build_client", lambda self: self) | ||
chat = ChatOutlines(model="microsoft/Phi-3-mini-4k-instruct", backend="vllm") | ||
assert chat.backend == "vllm" | ||
|
||
|
||
def test_chat_outlines_backend_mlxlm(monkeypatch): | ||
monkeypatch.setattr(ChatOutlines, "build_client", lambda self: self) | ||
chat = ChatOutlines(model="microsoft/Phi-3-mini-4k-instruct", backend="mlxlm") | ||
assert chat.backend == "mlxlm" | ||
|
||
|
||
def test_chat_outlines_with_regex(monkeypatch): | ||
monkeypatch.setattr(ChatOutlines, "build_client", lambda self: self) | ||
regex = r"\d{3}-\d{3}-\d{4}" | ||
chat = ChatOutlines(model="microsoft/Phi-3-mini-4k-instruct", regex=regex) | ||
assert chat.regex == regex | ||
|
||
|
||
def test_chat_outlines_with_type_constraints(monkeypatch): | ||
monkeypatch.setattr(ChatOutlines, "build_client", lambda self: self) | ||
chat = ChatOutlines(model="microsoft/Phi-3-mini-4k-instruct", type_constraints=int) | ||
assert chat.type_constraints == int # noqa | ||
|
||
|
||
def test_chat_outlines_with_json_schema(monkeypatch): | ||
monkeypatch.setattr(ChatOutlines, "build_client", lambda self: self) | ||
|
||
class TestSchema(BaseModel): | ||
name: str = Field(description="A person's name") | ||
age: int = Field(description="A person's age") | ||
|
||
chat = ChatOutlines( | ||
model="microsoft/Phi-3-mini-4k-instruct", json_schema=TestSchema | ||
) | ||
assert chat.json_schema == TestSchema | ||
|
||
|
||
def test_chat_outlines_with_grammar(monkeypatch): | ||
monkeypatch.setattr(ChatOutlines, "build_client", lambda self: self) | ||
|
||
grammar = """ | ||
?start: expression | ||
?expression: term (("+" | "-") term)* | ||
?term: factor (("*" | "/") factor)* | ||
?factor: NUMBER | "-" factor | "(" expression ")" | ||
%import common.NUMBER | ||
""" | ||
chat = ChatOutlines(model="microsoft/Phi-3-mini-4k-instruct", grammar=grammar) | ||
assert chat.grammar == grammar | ||
|
||
|
||
def test_raise_for_multiple_output_constraints(monkeypatch): | ||
monkeypatch.setattr(ChatOutlines, "build_client", lambda self: self) | ||
|
||
with pytest.raises(ValueError): | ||
ChatOutlines( | ||
model="microsoft/Phi-3-mini-4k-instruct", | ||
type_constraints=int, | ||
regex=r"\d{3}-\d{3}-\d{4}", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import pytest | ||
|
||
from langchain_community.llms.outlines import Outlines | ||
|
||
|
||
def test_outlines_initialization(monkeypatch): | ||
monkeypatch.setattr(Outlines, "build_client", lambda self: self) | ||
|
||
llm = Outlines( | ||
model="microsoft/Phi-3-mini-4k-instruct", | ||
max_tokens=42, | ||
stop=["\n"], | ||
) | ||
assert llm.model == "microsoft/Phi-3-mini-4k-instruct" | ||
assert llm.max_tokens == 42 | ||
assert llm.backend == "transformers" | ||
assert llm.stop == ["\n"] | ||
|
||
|
||
def test_outlines_backend_llamacpp(monkeypatch): | ||
monkeypatch.setattr(Outlines, "build_client", lambda self: self) | ||
llm = Outlines( | ||
model="TheBloke/Llama-2-7B-Chat-GGUF/llama-2-7b-chat.Q4_K_M.gguf", | ||
backend="llamacpp", | ||
) | ||
assert llm.backend == "llamacpp" | ||
|
||
|
||
def test_outlines_backend_vllm(monkeypatch): | ||
monkeypatch.setattr(Outlines, "build_client", lambda self: self) | ||
llm = Outlines(model="microsoft/Phi-3-mini-4k-instruct", backend="vllm") | ||
assert llm.backend == "vllm" | ||
|
||
|
||
def test_outlines_backend_mlxlm(monkeypatch): | ||
monkeypatch.setattr(Outlines, "build_client", lambda self: self) | ||
llm = Outlines(model="microsoft/Phi-3-mini-4k-instruct", backend="mlxlm") | ||
assert llm.backend == "mlxlm" | ||
|
||
|
||
def test_outlines_with_regex(monkeypatch): | ||
monkeypatch.setattr(Outlines, "build_client", lambda self: self) | ||
regex = r"\d{3}-\d{3}-\d{4}" | ||
llm = Outlines(model="microsoft/Phi-3-mini-4k-instruct", regex=regex) | ||
assert llm.regex == regex | ||
|
||
|
||
def test_outlines_with_type_constraints(monkeypatch): | ||
monkeypatch.setattr(Outlines, "build_client", lambda self: self) | ||
llm = Outlines(model="microsoft/Phi-3-mini-4k-instruct", type_constraints=int) | ||
assert llm.type_constraints == int # noqa | ||
|
||
|
||
def test_outlines_with_json_schema(monkeypatch): | ||
monkeypatch.setattr(Outlines, "build_client", lambda self: self) | ||
from pydantic import BaseModel, Field | ||
|
||
class TestSchema(BaseModel): | ||
name: str = Field(description="A person's name") | ||
age: int = Field(description="A person's age") | ||
|
||
llm = Outlines(model="microsoft/Phi-3-mini-4k-instruct", json_schema=TestSchema) | ||
assert llm.json_schema == TestSchema | ||
|
||
|
||
def test_outlines_with_grammar(monkeypatch): | ||
monkeypatch.setattr(Outlines, "build_client", lambda self: self) | ||
grammar = """ | ||
?start: expression | ||
?expression: term (("+" | "-") term)* | ||
?term: factor (("*" | "/") factor)* | ||
?factor: NUMBER | "-" factor | "(" expression ")" | ||
%import common.NUMBER | ||
""" | ||
llm = Outlines(model="microsoft/Phi-3-mini-4k-instruct", grammar=grammar) | ||
assert llm.grammar == grammar | ||
|
||
|
||
def test_raise_for_multiple_output_constraints(monkeypatch): | ||
monkeypatch.setattr(Outlines, "build_client", lambda self: self) | ||
with pytest.raises(ValueError): | ||
Outlines( | ||
model="microsoft/Phi-3-mini-4k-instruct", | ||
type_constraints=int, | ||
regex=r"\d{3}-\d{3}-\d{4}", | ||
) | ||
Outlines( | ||
model="microsoft/Phi-3-mini-4k-instruct", | ||
type_constraints=int, | ||
regex=r"\d{3}-\d{3}-\d{4}", | ||
) |