Git Product home page Git Product logo

Comments (3)

simonw avatar simonw commented on May 23, 2024

Relevant code:

llm/llm/cli.py

Lines 227 to 228 in 82c1923

def get_log_db_path():
return os.path.expanduser("~/.llm/log.db")

llm/llm/cli.py

Lines 109 to 118 in 82c1923

@cli.command()
def init_db():
"Ensure ~/.llm/log.db SQLite database exists"
path = get_log_db_path()
if os.path.exists(path):
return
# Ensure directory exists
os.makedirs(os.path.dirname(path), exist_ok=True)
db = sqlite_utils.Database(path)
db.vacuum()

I'm also going to remove all use of os.path and replace that with pathlib.

from llm.

simonw avatar simonw commented on May 23, 2024

Cheated on that refactor:

llm --system "Refactor to use pathlib instead of os.path" < llm/cli.py

Here's the diff from what it produced:

diff --git a/llm/cli.py b/llm/cli.py
index 6562fa1..b5fc8dc 100644
--- a/llm/cli.py
+++ b/llm/cli.py
@@ -4,12 +4,11 @@ import datetime
 import json
 from .migrations import migrate
 import openai
-import os
 import pathlib
-from platformdirs import user_data_dir
 import sqlite_utils
 import sys
 import warnings
+from platformdirs import user_data_dir
 
 warnings.simplefilter("ignore", ResourceWarning)
 
@@ -110,10 +109,10 @@ def prompt(prompt, system, gpt4, model, no_stream, no_log, _continue, chat_id, k
 def init_db():
     "Ensure ~/.llm/log.db SQLite database exists"
     path = get_log_db_path()
-    if os.path.exists(path):
+    if path.exists():
         return
     # Ensure directory exists
-    os.makedirs(os.path.dirname(path), exist_ok=True)
+    path.parent.mkdir(parents=True, exist_ok=True)
     db = sqlite_utils.Database(path)
     db.vacuum()
 
@@ -130,11 +129,7 @@ def path():
 
 
 def keys_path():
-    return os.environ.get("LLM_KEYS_PATH") or os.path.join(user_dir(), "keys.json")
-
-
-def user_dir():
-    return user_data_dir("io.datasette.llm", "Datasette")
+    return os.environ.get("LLM_KEYS_PATH") or pathlib.Path(user_data_dir("io.datasette.llm", "Datasette")) / "keys.json"
 
 
 @keys.command(name="set")
@@ -178,8 +173,8 @@ def set_(name, value):
 )
 @click.option("-t", "--truncate", is_flag=True, help="Truncate long strings in output")
 def logs(count, path, truncate):
-    path = path or get_log_db_path()
-    if not os.path.exists(path):
+    path = pathlib.Path(path or get_log_db_path())
+    if not path.exists():
         raise click.ClickException("No log database found at {}".format(path))
     db = sqlite_utils.Database(path)
     migrate(db)
@@ -225,14 +220,14 @@ def load_keys():
 
 
 def get_log_db_path():
-    return os.path.expanduser("~/.llm/log.db")
+    return pathlib.Path("~/.llm/log.db").expanduser()
 
 
 def log(no_log, system, prompt, response, model, chat_id=None):
     if no_log:
         return
     log_path = get_log_db_path()
-    if not os.path.exists(log_path):
+    if not log_path.exists():
         return
     db = sqlite_utils.Database(log_path)
     migrate(db)
@@ -252,7 +247,7 @@ def get_history(chat_id):
     if chat_id is None:
         return None, []
     log_path = get_log_db_path()
-    if not os.path.exists(log_path):
+    if not log_path.exists():
         raise click.ClickException(
             "This feature requires logging. Run `llm init-db` to create ~/.llm/log.db"
         )

It shouldn't have removed import os but other than that it looks good.

from llm.

simonw avatar simonw commented on May 23, 2024

This documentation is out-of-date:

If a SQLite database file exists in `~/.llm/log.db` then the tool will log all prompts and responses to it.

llm/docs/logging.md

Lines 37 to 39 in 76dbbed

You can also use [Datasette](https://datasette.io/) to browse your logs like this:
datasette ~/.llm/log.db

I think I need a llm logs path command - except logs isn't setup for sub-commands at the moment.

Options:

  • llm logs --path - can work right now, but is inconsistent with llm keys path
  • llm logs path - weird because llm logs is its own command right now. Maybe use another default command group?

from llm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.