langchain/libs/community/tests/unit_tests/llms/test_you.py
Christopher Tee 5171ffc026
community(you): Integrate You.com conversational APIs (#23046)
You.com is releasing two new conversational APIs — Smart and Research. 

This PR:
- integrates those APIs with Langchain, as an LLM
- streaming is supported

If no one reviews your PR within a few days, please @-mention one of
baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17.
2024-07-15 17:46:58 -04:00

38 lines
1.3 KiB
Python

import pytest
import requests_mock
@pytest.mark.parametrize("endpoint", ("smart", "research"))
@pytest.mark.requires("sseclient")
def test_invoke(
endpoint: str, requests_mock: requests_mock.Mocker, monkeypatch: pytest.MonkeyPatch
) -> None:
from langchain_community.llms import You
from langchain_community.llms.you import RESEARCH_ENDPOINT, SMART_ENDPOINT
json = {
"answer": (
"A solar eclipse occurs when the Moon passes between the Sun and Earth, "
"casting a shadow on Earth and ..."
),
"search_results": [
{
"url": "https://en.wikipedia.org/wiki/Solar_eclipse",
"name": "Solar eclipse - Wikipedia",
"snippet": (
"A solar eclipse occurs when the Moon passes "
"between Earth and the Sun, thereby obscuring the view of the Sun "
"from a small part of Earth, totally or partially. "
),
}
],
}
request_endpoint = SMART_ENDPOINT if endpoint == "smart" else RESEARCH_ENDPOINT
requests_mock.post(request_endpoint, json=json)
monkeypatch.setenv("YDC_API_KEY", "...")
llm = You(endpoint=endpoint)
output = llm.invoke("What is a solar eclipse?")
assert output == json["answer"]