XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 0
UTF-8
import unittest
import json

from g4f.client import Client, AsyncClient, ChatCompletion
from g4f.Provider import Copilot, DDG

DEFAULT_MESSAGES = [
    {"role": "system", "content": 'Response in json, Example: {"success": false}'},
    {"role": "user", "content": "Say success true in json"},
]


class TestProviderIntegration(unittest.TestCase):
    def test_bing(self):
        client = Client(provider=Copilot)
        response = client.chat.completions.create(
            DEFAULT_MESSAGES, "", response_format={"type": "json_object"}
        )
        self.assertIsInstance(response, ChatCompletion)
        if not response.choices or response.choices[0].message is None:
            self.fail("LLM returned empty or filtered response")
        self.assertIn("success", json.loads(response.choices[0].message.content))

    def test_openai(self):
        client = Client(provider=DDG)
        response = client.chat.completions.create(
            DEFAULT_MESSAGES, "", response_format={"type": "json_object"}
        )
        self.assertIsInstance(response, ChatCompletion)
        if not response.choices or response.choices[0].message is None:
            self.fail("LLM returned empty or filtered response")
        self.assertIn("success", json.loads(response.choices[0].message.content))


class TestChatCompletionAsync(unittest.IsolatedAsyncioTestCase):
    async def test_bing(self):
        client = AsyncClient(provider=Copilot)
        response = await client.chat.completions.create(
            DEFAULT_MESSAGES, "", response_format={"type": "json_object"}
        )
        self.assertIsInstance(response, ChatCompletion)
        self.assertIn("success", json.loads(response.choices[0].message.content))

    async def test_openai(self):
        client = AsyncClient(provider=DDG)
        response = await client.chat.completions.create(
            DEFAULT_MESSAGES, "", response_format={"type": "json_object"}
        )
        self.assertIsInstance(response, ChatCompletion)
        self.assertIn("success", json.loads(response.choices[0].message.content))


if __name__ == "__main__":
    unittest.main()