From 83484c0a5658b023bcef930aee5099a4fc059cb4 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 20 Apr 2024 15:41:49 +0200 Subject: Add workers and use_colors options to api --- etc/examples/api.py | 19 +++++++++++++++++++ etc/examples/ecosia.py | 18 ++++++++++++++++++ etc/examples/openaichat.py | 23 +++++++++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 etc/examples/api.py create mode 100644 etc/examples/ecosia.py create mode 100644 etc/examples/openaichat.py (limited to 'etc') diff --git a/etc/examples/api.py b/etc/examples/api.py new file mode 100644 index 00000000..d4d03a77 --- /dev/null +++ b/etc/examples/api.py @@ -0,0 +1,19 @@ +import requests +import json +url = "http://localhost:1337/v1/chat/completions" +body = { + "model": "", + "provider": "MetaAI", + "stream": True, + "messages": [ + {"role": "assistant", "content": "What can you do? Who are you?"} + ] +} +lines = requests.post(url, json=body, stream=True).iter_lines() +for line in lines: + if line.startswith(b"data: "): + try: + print(json.loads(line[6:]).get("choices", [{"delta": {}}])[0]["delta"].get("content", ""), end="") + except json.JSONDecodeError: + pass +print() \ No newline at end of file diff --git a/etc/examples/ecosia.py b/etc/examples/ecosia.py new file mode 100644 index 00000000..5a2ae520 --- /dev/null +++ b/etc/examples/ecosia.py @@ -0,0 +1,18 @@ +import asyncio +import g4f +from g4f.client import AsyncClient + +async def main(): + client = AsyncClient( + provider=g4f.Provider.Ecosia, + ) + async for chunk in client.chat.completions.create( + [{"role": "user", "content": "happy dogs on work. write some lines"}], + g4f.models.default, + stream=True, + green=True, + ): + print(chunk.choices[0].delta.content or "", end="") + print(f"\nwith {chunk.model}") + +asyncio.run(main()) \ No newline at end of file diff --git a/etc/examples/openaichat.py b/etc/examples/openaichat.py new file mode 100644 index 00000000..291daa2c --- /dev/null +++ b/etc/examples/openaichat.py @@ -0,0 +1,23 @@ +from g4f.client import Client +from g4f.Provider import OpenaiChat, RetryProvider + +# compatible countries: https://pastebin.com/UK0gT9cn +client = Client( + proxies = { + 'http': 'http://username:password@host:port', # MUST BE WORKING OPENAI COUNTRY PROXY ex: USA + 'https': 'http://username:password@host:port' # MUST BE WORKING OPENAI COUNTRY PROXY ex: USA + }, + provider = RetryProvider([OpenaiChat], + single_provider_retry=True, max_retries=5) +) + +messages = [ + {'role': 'user', 'content': 'Hello'} +] + +response = client.chat.completions.create(model='gpt-3.5-turbo', + messages=messages, + stream=True) + +for message in response: + print(message.choices[0].delta.content or "") \ No newline at end of file -- cgit v1.2.3 From db00480153283a3bf76e6398fd7240e943cf15bb Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 20 Apr 2024 20:11:09 +0200 Subject: Disable Bing integration test --- etc/unittest/integration.py | 1 + 1 file changed, 1 insertion(+) (limited to 'etc') diff --git a/etc/unittest/integration.py b/etc/unittest/integration.py index 379f36b6..1f7fb000 100644 --- a/etc/unittest/integration.py +++ b/etc/unittest/integration.py @@ -19,6 +19,7 @@ class TestProviderIntegration(unittest.TestCase): self.skipTest("nest_asyncio is not installed") def test_bing(self): + self.skipTest("Not working") client = Client(provider=Bing) response = client.chat.completions.create(DEFAULT_MESSAGES, "", response_format={"type": "json_object"}) self.assertIsInstance(response, ChatCompletion) -- cgit v1.2.3