summaryrefslogtreecommitdiffstats
path: root/etc
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--etc/examples/api.py13
-rw-r--r--etc/examples/image_api.py6
-rw-r--r--etc/tool/copilot.py6
-rw-r--r--etc/unittest/__main__.py2
-rw-r--r--etc/unittest/backend.py33
-rw-r--r--etc/unittest/client.py76
6 files changed, 91 insertions, 45 deletions
diff --git a/etc/examples/api.py b/etc/examples/api.py
index 1ab9b51b..f8f5d5ec 100644
--- a/etc/examples/api.py
+++ b/etc/examples/api.py
@@ -6,14 +6,19 @@ body = {
"provider": "",
"stream": True,
"messages": [
- {"role": "assistant", "content": "What can you do? Who are you?"}
+ {"role": "user", "content": "What can you do? Who are you?"}
]
}
-lines = requests.post(url, json=body, stream=True).iter_lines()
-for line in lines:
+response = requests.post(url, json=body, stream=True)
+response.raise_for_status()
+for line in response.iter_lines():
if line.startswith(b"data: "):
try:
- print(json.loads(line[6:]).get("choices", [{"delta": {}}])[0]["delta"].get("content", ""), end="")
+ json_data = json.loads(line[6:])
+ if json_data.get("error"):
+ print(json_data)
+ break
+ print(json_data.get("choices", [{"delta": {}}])[0]["delta"].get("content", ""), end="")
except json.JSONDecodeError:
pass
print() \ No newline at end of file
diff --git a/etc/examples/image_api.py b/etc/examples/image_api.py
index dbae22ed..9a438f9b 100644
--- a/etc/examples/image_api.py
+++ b/etc/examples/image_api.py
@@ -1,9 +1,9 @@
import requests
url = "http://localhost:1337/v1/images/generations"
body = {
- "prompt": "heaven for dogs",
- "provider": "OpenaiAccount",
- "response_format": "b64_json",
+ "model": "dall-e",
+ "prompt": "hello world user",
+ #"response_format": "b64_json",
}
data = requests.post(url, json=body, stream=True).json()
print(data) \ No newline at end of file
diff --git a/etc/tool/copilot.py b/etc/tool/copilot.py
index ed1fdf16..817d92da 100644
--- a/etc/tool/copilot.py
+++ b/etc/tool/copilot.py
@@ -219,9 +219,6 @@ def main():
if not pull:
print(f"No PR number found")
exit()
- if pull.get_reviews().totalCount > 0 or pull.get_issue_comments().totalCount > 0:
- print(f"Has already a review")
- exit()
diff = get_diff(pull.diff_url)
except Exception as e:
print(f"Error get details: {e.__class__.__name__}: {e}")
@@ -231,6 +228,9 @@ def main():
except Exception as e:
print(f"Error create review: {e}")
exit(1)
+ if pull.get_reviews().totalCount > 0 or pull.get_issue_comments().totalCount > 0:
+ pull.create_issue_comment(body=review)
+ return
try:
comments = analyze_code(pull, diff)
except Exception as e:
diff --git a/etc/unittest/__main__.py b/etc/unittest/__main__.py
index ee748917..f8a73280 100644
--- a/etc/unittest/__main__.py
+++ b/etc/unittest/__main__.py
@@ -1,10 +1,10 @@
import unittest
+
from .asyncio import *
from .backend import *
from .main import *
from .model import *
from .client import *
-from .client import *
from .include import *
from .integration import *
diff --git a/etc/unittest/backend.py b/etc/unittest/backend.py
index ee6174d5..a2999c5c 100644
--- a/etc/unittest/backend.py
+++ b/etc/unittest/backend.py
@@ -1,15 +1,19 @@
+from __future__ import annotations
+
import unittest
import asyncio
from unittest.mock import MagicMock
-from .mocks import ProviderMock
-import g4f
from g4f.errors import MissingRequirementsError
-
try:
- from g4f.gui.server.backend import Backend_Api, get_error_message
+ from g4f.gui.server.backend import Backend_Api
has_requirements = True
except:
has_requirements = False
+try:
+ from duckduckgo_search.exceptions import DuckDuckGoSearchException
+except ImportError:
+ class DuckDuckGoSearchException:
+ pass
class TestBackendApi(unittest.TestCase):
@@ -31,28 +35,15 @@ class TestBackendApi(unittest.TestCase):
def test_get_providers(self):
response = self.api.get_providers()
- self.assertIsInstance(response, list)
+ self.assertIsInstance(response, dict)
self.assertTrue(len(response) > 0)
def test_search(self):
from g4f.gui.server.internet import search
try:
result = asyncio.run(search("Hello"))
+ except DuckDuckGoSearchException as e:
+ self.skipTest(e)
except MissingRequirementsError:
self.skipTest("search is not installed")
- self.assertEqual(5, len(result))
-
-class TestUtilityFunctions(unittest.TestCase):
-
- def setUp(self):
- if not has_requirements:
- self.skipTest("gui is not installed")
-
- def test_get_error_message(self):
- g4f.debug.last_provider = ProviderMock
- exception = Exception("Message")
- result = get_error_message(exception)
- self.assertEqual("ProviderMock: Exception: Message", result)
-
-if __name__ == '__main__':
- unittest.main() \ No newline at end of file
+ self.assertEqual(5, len(result)) \ No newline at end of file
diff --git a/etc/unittest/client.py b/etc/unittest/client.py
index 54e2091f..97f9f6c8 100644
--- a/etc/unittest/client.py
+++ b/etc/unittest/client.py
@@ -1,6 +1,8 @@
+from __future__ import annotations
+
import unittest
-from g4f.client import Client, ChatCompletion, ChatCompletionChunk
+from g4f.client import Client, AsyncClient, ChatCompletion, ChatCompletionChunk
from .mocks import AsyncGeneratorProviderMock, ModelProviderMock, YieldProviderMock
DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
@@ -8,37 +10,38 @@ DEFAULT_MESSAGES = [{'role': 'user', 'content': 'Hello'}]
class AsyncTestPassModel(unittest.IsolatedAsyncioTestCase):
async def test_response(self):
- client = Client(provider=AsyncGeneratorProviderMock)
- response = await client.chat.completions.async_create(DEFAULT_MESSAGES, "")
+ client = AsyncClient(provider=AsyncGeneratorProviderMock)
+ response = await client.chat.completions.create(DEFAULT_MESSAGES, "")
self.assertIsInstance(response, ChatCompletion)
self.assertEqual("Mock", response.choices[0].message.content)
async def test_pass_model(self):
- client = Client(provider=ModelProviderMock)
- response = await client.chat.completions.async_create(DEFAULT_MESSAGES, "Hello")
+ client = AsyncClient(provider=ModelProviderMock)
+ response = await client.chat.completions.create(DEFAULT_MESSAGES, "Hello")
self.assertIsInstance(response, ChatCompletion)
self.assertEqual("Hello", response.choices[0].message.content)
async def test_max_tokens(self):
- client = Client(provider=YieldProviderMock)
+ client = AsyncClient(provider=YieldProviderMock)
messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
- response = await client.chat.completions.async_create(messages, "Hello", max_tokens=1)
+ response = await client.chat.completions.create(messages, "Hello", max_tokens=1)
self.assertIsInstance(response, ChatCompletion)
self.assertEqual("How ", response.choices[0].message.content)
- response = await client.chat.completions.async_create(messages, "Hello", max_tokens=2)
+ response = await client.chat.completions.create(messages, "Hello", max_tokens=2)
self.assertIsInstance(response, ChatCompletion)
self.assertEqual("How are ", response.choices[0].message.content)
async def test_max_stream(self):
- client = Client(provider=YieldProviderMock)
+ client = AsyncClient(provider=YieldProviderMock)
messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
- response = await client.chat.completions.async_create(messages, "Hello", stream=True)
+ response = client.chat.completions.create(messages, "Hello", stream=True)
async for chunk in response:
+ chunk: ChatCompletionChunk = chunk
self.assertIsInstance(chunk, ChatCompletionChunk)
if chunk.choices[0].delta.content is not None:
self.assertIsInstance(chunk.choices[0].delta.content, str)
messages = [{'role': 'user', 'content': chunk} for chunk in ["You ", "You ", "Other", "?"]]
- response = await client.chat.completions.async_create(messages, "Hello", stream=True, max_tokens=2)
+ response = client.chat.completions.create(messages, "Hello", stream=True, max_tokens=2)
response_list = []
async for chunk in response:
response_list.append(chunk)
@@ -48,11 +51,58 @@ class AsyncTestPassModel(unittest.IsolatedAsyncioTestCase):
self.assertEqual(chunk.choices[0].delta.content, "You ")
async def test_stop(self):
+ client = AsyncClient(provider=YieldProviderMock)
+ messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
+ response = await client.chat.completions.create(messages, "Hello", stop=["and"])
+ self.assertIsInstance(response, ChatCompletion)
+ self.assertEqual("How are you?", response.choices[0].message.content)
+
+class TestPassModel(unittest.TestCase):
+
+ def test_response(self):
+ client = Client(provider=AsyncGeneratorProviderMock)
+ response = client.chat.completions.create(DEFAULT_MESSAGES, "")
+ self.assertIsInstance(response, ChatCompletion)
+ self.assertEqual("Mock", response.choices[0].message.content)
+
+ def test_pass_model(self):
+ client = Client(provider=ModelProviderMock)
+ response = client.chat.completions.create(DEFAULT_MESSAGES, "Hello")
+ self.assertIsInstance(response, ChatCompletion)
+ self.assertEqual("Hello", response.choices[0].message.content)
+
+ def test_max_tokens(self):
+ client = Client(provider=YieldProviderMock)
+ messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
+ response = client.chat.completions.create(messages, "Hello", max_tokens=1)
+ self.assertIsInstance(response, ChatCompletion)
+ self.assertEqual("How ", response.choices[0].message.content)
+ response = client.chat.completions.create(messages, "Hello", max_tokens=2)
+ self.assertIsInstance(response, ChatCompletion)
+ self.assertEqual("How are ", response.choices[0].message.content)
+
+ def test_max_stream(self):
+ client = Client(provider=YieldProviderMock)
+ messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
+ response = client.chat.completions.create(messages, "Hello", stream=True)
+ for chunk in response:
+ self.assertIsInstance(chunk, ChatCompletionChunk)
+ if chunk.choices[0].delta.content is not None:
+ self.assertIsInstance(chunk.choices[0].delta.content, str)
+ messages = [{'role': 'user', 'content': chunk} for chunk in ["You ", "You ", "Other", "?"]]
+ response = client.chat.completions.create(messages, "Hello", stream=True, max_tokens=2)
+ response_list = list(response)
+ self.assertEqual(len(response_list), 3)
+ for chunk in response_list:
+ if chunk.choices[0].delta.content is not None:
+ self.assertEqual(chunk.choices[0].delta.content, "You ")
+
+ def test_stop(self):
client = Client(provider=YieldProviderMock)
messages = [{'role': 'user', 'content': chunk} for chunk in ["How ", "are ", "you", "?"]]
- response = await client.chat.completions.async_create(messages, "Hello", stop=["and"])
+ response = client.chat.completions.create(messages, "Hello", stop=["and"])
self.assertIsInstance(response, ChatCompletion)
self.assertEqual("How are you?", response.choices[0].message.content)
if __name__ == '__main__':
- unittest.main()
+ unittest.main() \ No newline at end of file