Skip to content

Commit

Permalink
Lint using Ruff, refs #78
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jul 10, 2023
1 parent 2b50797 commit 5a8cd10
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ jobs:
- name: Run mypy
run: |
mypy llm
- name: Run ruff
run: |
ruff .
1 change: 1 addition & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
68 changes: 33 additions & 35 deletions llm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 1 addition & 2 deletions llm/default_plugins/openai_models.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion llm/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
line-length = 160
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def get_long_description():
"cogapp",
"mypy",
"black",
"ruff",
"types-PyYAML",
"types-requests",
]
Expand Down
1 change: 0 additions & 1 deletion tests/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import click
import importlib
from llm import cli, hookimpl, plugins
import pytest


def test_register_commands():
Expand Down

0 comments on commit 5a8cd10

Please sign in to comment.