continue-custom/server/tests/llm_test.py
Nate Sesti f09150617e
Preview (#541)
* Strong typing (#533)

* refactor: ♻️ get rid of continuedev.src.continuedev structure

* refactor: ♻️ switching back to server folder

* feat: ✨ make config.py imports shorter

* feat: 🔖 publish as pre-release vscode extension

* refactor: ♻️ refactor and add more completion params to ui

* build: 🏗️ download from preview S3

* fix: 🐛 fix paths

* fix: 💚 package:pre-release

* ci: 💚 more time for tests

* fix: 💚 fix build scripts

* fix: 🐛 fix import in run.py

* fix: 🔖 update version to try again

* ci: 💚 Update package.json version [skip ci]

* refactor: 🔥 don't check for old extensions version

* fix: 🐛 small bug fixes

* fix: 🐛 fix config.py import paths

* ci: 💚 Update package.json version [skip ci]

* ci: 💚 platform-specific builds test #1

* feat: 💚 ship with binary

* fix: 💚 fix copy statement to include.exe for windows

* fix: 💚 cd extension before packaging

* chore: 🔊 count tokens generated

* fix: 💚 remove npm_config_arch

* fix: 💚 publish as pre-release!

* chore: 🔖 update version

* perf: 💚 hardcode distro paths

* fix: 🐛 fix yaml syntax error

* chore: 🔖 update version

* fix: 💚 update permissions and version

* feat: 🐛 kill old server if needed

* feat: 💄 update marketplace icon for pre-release

* ci: 💚 Update package.json version [skip ci]

* feat: ✨ auto-reload for config.py

* feat: 🔧 update default config.py imports

* feat: ✨ codelens in config.py

* feat: ✨ select model param count from UI

* ci: 💚 Update package.json version [skip ci]

* feat: ✨ more model options, ollama error handling

* perf: ⚡ don't show server loading immediately

* fix: 🐛 fixing small UI details

* ci: 💚 Update package.json version [skip ci]

* feat: 🚀 headers param on LLM class

* fix: 🐛 fix headers for openai.;y

* feat: ✨ highlight code on cmd+shift+L

* ci: 💚 Update package.json version [skip ci]

* feat: 💄 sticky top bar in gui.tsx

* fix: 🔊 websocket logging and horizontal scrollbar

* ci: 💚 Update package.json version [skip ci]

* feat: ✨ allow AzureOpenAI Service through GGML

* ci: 💚 Update package.json version [skip ci]

* fix: 🐛 fix automigration

* ci: 💚 Update package.json version [skip ci]

* ci: 💚 upload binaries in ci, download apple silicon

* chore: 🔥 remove notes

* fix: 💚 use curl to download binary

* fix: 💚 set permissions on apple silicon binary

* fix: 💚 testing

* fix: 💚 cleanup file

* fix: 💚 fix preview.yaml

* fix: 💚 only upload once per binary

* fix: 💚 install rosetta

* ci: 💚 download binary after tests

* ci: 💚 Update package.json version [skip ci]

* ci: 💚 prepare ci for merge to main

---------

Co-authored-by: GitHub Action <action@github.com>
2023-10-09 18:37:27 -07:00

179 lines
5.1 KiB
Python

import asyncio
import os
from functools import wraps
import pytest
from continuedev.core.main import ChatMessage
from continuedev.libs.llm.anthropic import AnthropicLLM
from continuedev.libs.llm.base import LLM, CompletionOptions
from continuedev.libs.llm.ggml import GGML
from continuedev.libs.llm.openai import OpenAI
from continuedev.libs.llm.together import TogetherLLM
from continuedev.libs.util.count_tokens import DEFAULT_ARGS
from dotenv import load_dotenv
from util.prompts import tokyo_test_pair
load_dotenv()
SPEND_MONEY = True
def start_model(model):
def write_log(msg: str):
pass
asyncio.run(model.start(write_log=write_log, unique_id="test_unique_id"))
def async_test(func):
@wraps(func)
def wrapper(*args, **kwargs):
return asyncio.run(func(*args, **kwargs))
return wrapper
class TestBaseLLM:
model = "gpt-3.5-turbo"
context_length = 4096
system_message = "test_system_message"
def setup_class(cls):
cls.llm = LLM(
model=cls.model,
context_length=cls.context_length,
system_message=cls.system_message,
)
start_model(cls.llm)
def test_llm_is_instance(self):
assert isinstance(self.llm, LLM)
def test_llm_collect_args(self):
options = CompletionOptions(model=self.model)
assert self.llm.collect_args(options) == {
**DEFAULT_ARGS,
"model": self.model,
}
@pytest.mark.skipif(SPEND_MONEY is False, reason="Costs money")
@async_test
async def test_completion(self):
if self.llm.__class__.__name__ == "LLM":
pytest.skip("Skipping abstract LLM")
resp = await self.llm.complete(tokyo_test_pair[0], temperature=0.0)
assert isinstance(resp, str)
assert resp.strip().lower() == tokyo_test_pair[1]
@pytest.mark.skipif(SPEND_MONEY is False, reason="Costs money")
@async_test
async def test_stream_chat(self):
if self.llm.__class__.__name__ == "LLM":
pytest.skip("Skipping abstract LLM")
completion = ""
role = None
async for chunk in self.llm.stream_chat(
messages=[
ChatMessage(
role="user", content=tokyo_test_pair[0], summary=tokyo_test_pair[0]
)
],
temperature=0.0,
):
assert isinstance(chunk, dict)
if "content" in chunk:
completion += chunk["content"]
if "role" in chunk:
role = chunk["role"]
assert role == "assistant"
assert completion.strip().lower() == tokyo_test_pair[1]
@pytest.mark.skipif(SPEND_MONEY is False, reason="Costs money")
@async_test
async def test_stream_complete(self):
if self.llm.__class__.__name__ == "LLM":
pytest.skip("Skipping abstract LLM")
completion = ""
async for chunk in self.llm.stream_complete(
tokyo_test_pair[0], temperature=0.0
):
assert isinstance(chunk, str)
completion += chunk
assert completion.strip().lower() == tokyo_test_pair[1]
class TestOpenAI(TestBaseLLM):
def setup_class(cls):
super().setup_class(cls)
cls.llm = OpenAI(
model=cls.model,
context_length=cls.context_length,
system_message=cls.system_message,
api_key=os.environ["OPENAI_API_KEY"],
# api_base=f"http://localhost:{port}",
)
start_model(cls.llm)
# cls.server = start_openai(port=port)
# def teardown_class(cls):
# cls.server.terminate()
@pytest.mark.asyncio
@pytest.mark.skipif(SPEND_MONEY is False, reason="Costs money")
async def test_completion(self):
resp = await self.llm.complete(
"Output a single word, that being the capital of Japan:"
)
assert isinstance(resp, str)
assert resp.strip().lower() == tokyo_test_pair[1]
class TestGGML(TestBaseLLM):
def setup_class(cls):
super().setup_class(cls)
cls.llm = GGML(
model="gpt-3.5-turbo",
context_length=cls.context_length,
system_message=cls.system_message,
server_url="https://api.openai.com",
api_key=os.environ["OPENAI_API_KEY"],
)
start_model(cls.llm)
@pytest.mark.skipif(True, reason="Together is not working")
class TestTogetherLLM(TestBaseLLM):
def setup_class(cls):
super().setup_class(cls)
cls.llm = TogetherLLM(
api_key=os.environ["TOGETHER_API_KEY"],
)
start_model(cls.llm)
class TestAnthropicLLM(TestBaseLLM):
def setup_class(cls):
super().setup_class(cls)
cls.llm = AnthropicLLM(api_key=os.environ["ANTHROPIC_API_KEY"])
start_model(cls.llm)
def test_llm_collect_args(self):
options = CompletionOptions(model=self.model)
assert self.llm.collect_args(options) == {
"max_tokens_to_sample": DEFAULT_ARGS["max_tokens"],
"temperature": DEFAULT_ARGS["temperature"],
"model": self.model,
}
if __name__ == "__main__":
import pytest
pytest.main()