Include one or more context files in chat session

This commit is contained in:
Paul Harrison 2023-08-22 17:08:41 +01:00
parent 7cbde55aac
commit 0ad1634cf6
3 changed files with 36 additions and 4 deletions

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "llm-chat" name = "llm-chat"
version = "0.1.0" version = "0.2.0"
description = "A general CLI interface for large language models." description = "A general CLI interface for large language models."
authors = ["Paul Harrison <paul@harrison.sh>"] authors = ["Paul Harrison <paul@harrison.sh>"]
readme = "README.md" readme = "README.md"

View File

@ -77,6 +77,6 @@ class Chat:
return message return message
def get_chat(settings: OpenAISettings | None = None) -> ChatProtocol: def get_chat(settings: OpenAISettings | None = None, context: list[Message] = []) -> ChatProtocol:
"""Get a chat object.""" """Get a chat object."""
return Chat(settings=settings) return Chat(settings=settings, context=context)

View File

@ -1,3 +1,4 @@
from pathlib import Path
from typing import Annotated, Any, Optional from typing import Annotated, Any, Optional
import typer import typer
@ -6,6 +7,7 @@ from rich.console import Console
from rich.markdown import Markdown from rich.markdown import Markdown
from llm_chat.chat import get_chat from llm_chat.chat import get_chat
from llm_chat.models import Message, Role
from llm_chat.settings import DEFAULT_MODEL, DEFAULT_TEMPERATURE, Model, OpenAISettings from llm_chat.settings import DEFAULT_MODEL, DEFAULT_TEMPERATURE, Model, OpenAISettings
app = typer.Typer() app = typer.Typer()
@ -39,6 +41,20 @@ def read_user_input(session: PromptSession[Any]) -> str:
return prompt return prompt
def load_context(path: Path) -> Message:
"""Load context text from file."""
if not path.exists():
raise typer.BadParameter(f"File {path} does not exist.")
if not path.is_file():
raise typer.BadParameter(f"Path {path} is not a file.")
with path.open() as f:
content = f.read()
return Message(role=Role.SYSTEM, content=content)
@app.command() @app.command()
def chat( def chat(
api_key: Annotated[ api_key: Annotated[
@ -63,6 +79,19 @@ def chat(
..., "--temperature", "-t", help="Model temperature (i.e. creativeness)." ..., "--temperature", "-t", help="Model temperature (i.e. creativeness)."
), ),
] = DEFAULT_TEMPERATURE, ] = DEFAULT_TEMPERATURE,
context: Annotated[
Optional[list[Path]],
typer.Option(
...,
"--context",
"-c",
help="Path to a file containing context text.",
exists=True,
file_okay=True,
dir_okay=False,
readable=True,
),
] = [],
) -> None: ) -> None:
"""Start a chat session.""" """Start a chat session."""
# TODO: Add option to load context from file. # TODO: Add option to load context from file.
@ -71,8 +100,11 @@ def chat(
settings = OpenAISettings(api_key=api_key, model=model, temperature=temperature) settings = OpenAISettings(api_key=api_key, model=model, temperature=temperature)
else: else:
settings = OpenAISettings(model=model, temperature=temperature) settings = OpenAISettings(model=model, temperature=temperature)
context_messages = [load_context(path) for path in context]
current_chat = get_chat(settings) current_chat = get_chat(settings=settings, context=context_messages)
# current_chat = get_chat(settings=settings)
console = get_console() console = get_console()
session = get_session() session = get_session()