-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
90 additions
and
23 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
import textwrap | ||
from unittest.mock import MagicMock | ||
|
||
import openai | ||
import openai.resources | ||
import pytest | ||
|
||
|
||
def split_with_delimiter(string, delim): | ||
result = [] | ||
last_split = 0 | ||
for index, character in enumerate(string): | ||
if character == delim: | ||
result.append(string[last_split:index + 1]) | ||
last_split = index + 1 | ||
if last_split != len(string): | ||
result.append(string[last_split:]) | ||
return result | ||
|
||
|
||
@pytest.fixture | ||
def ollama_config(set_config): | ||
config = textwrap.dedent("""\ | ||
[shelloracle] | ||
provider = "Ollama" | ||
[provider.Ollama] | ||
host = "localhost" | ||
port = 11434 | ||
model = "dolphin-mistral" | ||
""") | ||
set_config(config) | ||
def mock_asyncopenai(monkeypatch): | ||
class AsyncChatCompletionIterator: | ||
def __init__(self, answer: str): | ||
self.answer_index = 0 | ||
self.answer_deltas = split_with_delimiter(answer, " ") | ||
|
||
def __aiter__(self): | ||
return self | ||
|
||
async def __anext__(self): | ||
if self.answer_index < len(self.answer_deltas): | ||
answer_chunk = self.answer_deltas[self.answer_index] | ||
self.answer_index += 1 | ||
chunk = MagicMock() | ||
chunk.delta.content = answer_chunk | ||
answer = MagicMock() | ||
answer.choices = [chunk] | ||
return answer | ||
else: | ||
raise StopAsyncIteration | ||
|
||
async def mock_acreate(*args, **kwargs): | ||
return AsyncChatCompletionIterator("cat test.py") | ||
|
||
monkeypatch.setattr(openai.resources.chat.AsyncCompletions, "create", mock_acreate) |
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 |
---|---|---|
@@ -1,10 +1,28 @@ | ||
import pytest | ||
|
||
from shelloracle.providers.localai import LocalAI | ||
|
||
class TestLocalAI: | ||
|
||
class TestOpenAI: | ||
@pytest.fixture | ||
def localai_config(self, set_config): | ||
config = {'shelloracle': {'provider': 'LocalAI'}, | ||
'provider': {'LocalAI': {'host': 'localhost', 'port': 8080, 'model': 'gpt-3.5-turbo'}}} | ||
config = {'shelloracle': {'provider': 'LocalAI'}, 'provider': { | ||
'LocalAI': {'host': 'localhost', 'port': 8080, 'model': 'mistral-openorca'}}} | ||
set_config(config) | ||
|
||
@pytest.fixture | ||
def localai_instance(self, localai_config): | ||
return LocalAI() | ||
|
||
def test_name(self): | ||
assert LocalAI.name == "LocalAI" | ||
|
||
def test_model(self, localai_instance): | ||
assert localai_instance.model == "mistral-openorca" | ||
|
||
return set_config(config) | ||
@pytest.mark.asyncio | ||
async def test_generate(self, mock_asyncopenai, localai_instance): | ||
result = "" | ||
async for response in localai_instance.generate(""): | ||
result += response | ||
assert result == "cat test.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
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