-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
llm openai models command, closes #70
- Loading branch information
Showing
3 changed files
with
112 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import List, Dict | ||
|
||
|
||
def dicts_to_table_string( | ||
headings: List[str], dicts: List[Dict[str, str]] | ||
) -> List[str]: | ||
max_lengths = [len(h) for h in headings] | ||
|
||
# Compute maximum length for each column | ||
for d in dicts: | ||
for i, h in enumerate(headings): | ||
if h in d and len(str(d[h])) > max_lengths[i]: | ||
max_lengths[i] = len(str(d[h])) | ||
|
||
# Generate formatted table strings | ||
res = [] | ||
res.append(" ".join(h.ljust(max_lengths[i]) for i, h in enumerate(headings))) | ||
|
||
for d in dicts: | ||
row = [] | ||
for i, h in enumerate(headings): | ||
row.append(str(d.get(h, "")).ljust(max_lengths[i])) | ||
res.append(" ".join(row)) | ||
|
||
return res |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from click.testing import CliRunner | ||
from llm.cli import cli | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def mocked_models(requests_mock): | ||
requests_mock.get( | ||
"https://api.openai.com/v1/models", | ||
json={ | ||
"data": [ | ||
{ | ||
"id": "ada:2020-05-03", | ||
"object": "model", | ||
"created": 1588537600, | ||
"owned_by": "openai", | ||
}, | ||
{ | ||
"id": "babbage:2020-05-03", | ||
"object": "model", | ||
"created": 1588537600, | ||
"owned_by": "openai", | ||
}, | ||
] | ||
}, | ||
headers={"Content-Type": "application/json"}, | ||
) | ||
return requests_mock | ||
|
||
|
||
def test_openai_models(mocked_models): | ||
runner = CliRunner() | ||
result = runner.invoke(cli, ["openai", "models", "--key", "x"]) | ||
assert result.exit_code == 0 | ||
assert result.output == ( | ||
"id owned_by created \n" | ||
"ada:2020-05-03 openai 2020-05-03T13:26:40\n" | ||
"babbage:2020-05-03 openai 2020-05-03T13:26:40\n" | ||
) |