diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 0864494f..aeca90c2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -34,3 +34,6 @@ jobs: - name: Run mypy run: | mypy llm + - name: Run ruff + run: | + ruff . diff --git a/Justfile b/Justfile index b8fda092..e8291dac 100644 --- a/Justfile +++ b/Justfile @@ -14,6 +14,7 @@ pipenv run black . --check pipenv run cog --check README.md docs/*.md pipenv run mypy llm + pipenv run ruff . # Rebuild docs with cog @cog: diff --git a/llm/cli.py b/llm/cli.py index d6ee580c..86e4e1b5 100644 --- a/llm/cli.py +++ b/llm/cli.py @@ -11,14 +11,12 @@ get_model_aliases, get_models_with_aliases, ) -import openai import os import pathlib import pydantic from runpy import run_module import shutil import sqlite_utils -from string import Template as StringTemplate import sys import time import warnings @@ -219,39 +217,39 @@ def prompt( return # Original code: - try: - debug = {} - if no_stream: - start = time.time() - response = openai.ChatCompletion.create( - model=model, - messages=messages, - ) - debug["model"] = response.model - debug["usage"] = response.usage - content = response.choices[0].message.content - log(no_log, system, prompt, content, model, chat_id, debug, start) - print(content) - else: - start = time.time() - response = [] - for chunk in openai.ChatCompletion.create( - model=model, - messages=messages, - stream=True, - ): - debug["model"] = chunk.model - content = chunk["choices"][0].get("delta", {}).get("content") - if content is not None: - response.append(content) - print(content, end="") - sys.stdout.flush() - print("") - log(no_log, system, prompt, "".join(response), model, chat_id, debug, start) - except openai.error.AuthenticationError as ex: - raise click.ClickException("{}: {}".format(ex.error.type, ex.error.code)) - except openai.error.OpenAIError as ex: - raise click.ClickException(str(ex)) + # try: + # debug = {} + # if no_stream: + # start = time.time() + # response = openai.ChatCompletion.create( + # model=model, + # messages=messages, + # ) + # debug["model"] = response.model + # debug["usage"] = response.usage + # content = response.choices[0].message.content + # log(no_log, system, prompt, content, model, chat_id, debug, start) + # print(content) + # else: + # start = time.time() + # response = [] + # for chunk in openai.ChatCompletion.create( + # model=model, + # messages=messages, + # stream=True, + # ): + # debug["model"] = chunk.model + # content = chunk["choices"][0].get("delta", {}).get("content") + # if content is not None: + # response.append(content) + # print(content, end="") + # sys.stdout.flush() + # print("") + # log(no_log, system, prompt, "".join(response), model, chat_id, debug, start) + # except openai.error.AuthenticationError as ex: + # raise click.ClickException("{}: {}".format(ex.error.type, ex.error.code)) + # except openai.error.OpenAIError as ex: + # raise click.ClickException(str(ex)) @cli.command() diff --git a/llm/default_plugins/openai_models.py b/llm/default_plugins/openai_models.py index 6e2bac30..8e2035ec 100644 --- a/llm/default_plugins/openai_models.py +++ b/llm/default_plugins/openai_models.py @@ -1,9 +1,8 @@ -from llm import Model, Prompt, OptionsError, Response, hookimpl +from llm import Model, Prompt, Response, hookimpl from llm.errors import NeedsKeyException from llm.utils import dicts_to_table_string import click import datetime -from typing import Optional import openai import requests import json diff --git a/llm/migrations.py b/llm/migrations.py index 865b1357..e3e47fbd 100644 --- a/llm/migrations.py +++ b/llm/migrations.py @@ -34,7 +34,7 @@ def m001_initial(db): # Ensure the original table design exists, so other migrations can run if db["log"].exists(): # It needs to have the chat_id column - if not "chat_id" in db["log"].columns_dict: + if "chat_id" not in db["log"].columns_dict: db["log"].add_column("chat_id") return db["log"].create( diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 00000000..7a3f6a3f --- /dev/null +++ b/ruff.toml @@ -0,0 +1 @@ +line-length = 160 diff --git a/setup.py b/setup.py index 8050e895..71c687bb 100644 --- a/setup.py +++ b/setup.py @@ -48,6 +48,7 @@ def get_long_description(): "cogapp", "mypy", "black", + "ruff", "types-PyYAML", "types-requests", ] diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 37ac3fc0..968325fc 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -2,7 +2,6 @@ import click import importlib from llm import cli, hookimpl, plugins -import pytest def test_register_commands():