Fix pylance errors
This commit is contained in:
parent
bbc6242b19
commit
d82705bdb7
1 changed files with 9 additions and 7 deletions
16
main.py
16
main.py
|
|
@ -5,6 +5,7 @@ import readline # noqa: F401 — enables arrow-key history in input()
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
from openai.types.chat import ChatCompletionMessageParam
|
||||||
|
|
||||||
|
|
||||||
DB_PATH = Path(__file__).parent / "conversations.db"
|
DB_PATH = Path(__file__).parent / "conversations.db"
|
||||||
|
|
@ -61,10 +62,11 @@ def get_or_create_conversation(conn: sqlite3.Connection, name: str | None, model
|
||||||
(display_name, model),
|
(display_name, model),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
assert cur.lastrowid is not None
|
||||||
return cur.lastrowid
|
return cur.lastrowid
|
||||||
|
|
||||||
|
|
||||||
def load_messages(conn: sqlite3.Connection, conv_id: int) -> list[dict]:
|
def load_messages(conn: sqlite3.Connection, conv_id: int) -> list[ChatCompletionMessageParam]:
|
||||||
rows = conn.execute(
|
rows = conn.execute(
|
||||||
"SELECT role, content FROM messages WHERE conversation_id = ? ORDER BY id",
|
"SELECT role, content FROM messages WHERE conversation_id = ? ORDER BY id",
|
||||||
(conv_id,),
|
(conv_id,),
|
||||||
|
|
@ -127,13 +129,12 @@ def chat_loop(client: OpenAI, conn: sqlite3.Connection, conv_id: int, model: str
|
||||||
for msg in messages:
|
for msg in messages:
|
||||||
if msg["role"] == "system":
|
if msg["role"] == "system":
|
||||||
continue
|
continue
|
||||||
prefix = "you" if msg["role"] == "user" else "gpt"
|
prefix = "you" if msg["role"] == "user" else "assistant"
|
||||||
print(f"\n{prefix}> {msg['content']}")
|
print(f"\n{prefix}> {msg.get('content', '')}")
|
||||||
print()
|
print()
|
||||||
continue
|
continue
|
||||||
|
|
||||||
messages.append({"role": "user", "content": user_input})
|
messages.append({"role": "user", "content": user_input})
|
||||||
save_message(conn, conv_id, "user", user_input)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("gpt> ", end="", flush=True)
|
print("gpt> ", end="", flush=True)
|
||||||
|
|
@ -152,6 +153,7 @@ def chat_loop(client: OpenAI, conn: sqlite3.Connection, conv_id: int, model: str
|
||||||
|
|
||||||
assistant_content = "".join(full_response)
|
assistant_content = "".join(full_response)
|
||||||
messages.append({"role": "assistant", "content": assistant_content})
|
messages.append({"role": "assistant", "content": assistant_content})
|
||||||
|
save_message(conn, conv_id, "user", user_input)
|
||||||
save_message(conn, conv_id, "assistant", assistant_content)
|
save_message(conn, conv_id, "assistant", assistant_content)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -164,7 +166,7 @@ def main():
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="Chat with ChatGPT from the terminal")
|
parser = argparse.ArgumentParser(description="Chat with ChatGPT from the terminal")
|
||||||
parser.add_argument("-n", "--name", help="Conversation name (resumes if exists)")
|
parser.add_argument("-n", "--name", help="Conversation name (resumes if exists)")
|
||||||
parser.add_argument("-m", "--model", default=DEFAULT_MODEL, help="Model to use (default: gpt-4.1)")
|
parser.add_argument("-m", "--model", default=None, help="Model to use (default: gpt-4.1)")
|
||||||
parser.add_argument("-s", "--system", default=DEFAULT_SYSTEM_PROMPT,
|
parser.add_argument("-s", "--system", default=DEFAULT_SYSTEM_PROMPT,
|
||||||
help="System prompt for new conversations")
|
help="System prompt for new conversations")
|
||||||
parser.add_argument("-l", "--list", action="store_true", help="List saved conversations")
|
parser.add_argument("-l", "--list", action="store_true", help="List saved conversations")
|
||||||
|
|
@ -196,9 +198,9 @@ def main():
|
||||||
print(f"Error: No conversation with ID {args.resume}", file=sys.stderr)
|
print(f"Error: No conversation with ID {args.resume}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
conv_id = row[0]
|
conv_id = row[0]
|
||||||
model = args.model if args.model != DEFAULT_MODEL else row[1]
|
model = args.model if args.model is not None else row[1]
|
||||||
else:
|
else:
|
||||||
model = args.model
|
model = args.model or DEFAULT_MODEL
|
||||||
conv_id = get_or_create_conversation(conn, args.name, model)
|
conv_id = get_or_create_conversation(conn, args.name, model)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue