API Reference#

Here is the API reference for gptme.

core#

Some of the core classes and functions in gptme.

Message#

A message in the conversation.

class gptme.message.Message#

A message in the assistant conversation.

role#

The role of the message sender (system, user, or assistant).

content#

The content of the message.

pinned#

Whether this message should be pinned to the top of the chat, and never context-trimmed.

hide#

Whether this message should be hidden from the chat output (but still be sent to the assistant).

quiet#

Whether this message should be printed on execution (will still print on resume, unlike hide). This is not persisted to the log file.

timestamp#

The timestamp of the message.

files#

Files attached to the message, could e.g. be images for vision.

__init__(role: ~typing.Literal['system', 'user', 'assistant'], content: str, pinned: bool = False, hide: bool = False, quiet: bool = False, timestamp: ~datetime.datetime = <factory>, files: list[~pathlib.Path] = <factory>) None#
classmethod from_toml(toml: str) Self#

Converts a TOML string to a message.

The string can be a single [[message]].

get_codeblocks() list[Codeblock]#

Get all codeblocks from the message content.

replace(**kwargs) Self#

Replace attributes of the message.

to_dict(keys=None, openai=False, anthropic=False) dict#

Return a dict representation of the message, serializable to JSON.

to_toml() str#

Converts a message to a TOML string, for easy editing by hand in editor to then be parsed back.

Codeblock#

A codeblock in a message, possibly executable by tools.

class gptme.codeblock.Codeblock#

Codeblock(lang: str, content: str, path: str | None = None, start: int | None = None)

__init__(lang: str, content: str, path: str | None = None, start: int | None = None) None#
classmethod from_xml(content: str) Codeblock#

Example

<codeblock lang=”python” path=”example.py”> print(“Hello, world!”) </codeblock>

LogManager#

Holds the current conversation as a list of messages, saves and loads the conversation to and from files, supports branching, etc.

class gptme.logmanager.Conversation#

Conversation(name: str, path: str, created: float, modified: float, messages: int, branches: int)

__init__(name: str, path: str, created: float, modified: float, messages: int, branches: int) None#
class gptme.logmanager.LogManager#

Manages a conversation log.

__init__(log: list[Message] | None = None, logdir: str | Path | None = None, branch: str | None = None, show_hidden=False)#
append(msg: Message) None#

Appends a message to the log, writes the log, prints the message.

branch(name: str) None#

Switches to a branch.

diff(branch: str) str | None#

Prints the diff between the current branch and another branch.

edit(new_log: list[Message]) None#

Edits the log.

fork(name: str) None#

Copy the conversation folder to a new name.

classmethod load(logdir: str | Path, initial_msgs: list[Message] | None = None, branch: str = 'main', create: bool = False, **kwargs) LogManager#

Loads a conversation log.

prepare_messages() list[Message]#

Prepares the log into messages before sending it to the LLM.

rename(name: str, keep_date=False) None#

Rename the conversation. Renames the folder containing the conversation and its branches.

If keep_date is True, we will keep the date part of conversation folder name (“2021-08-01-some-name”) If you want to keep the old log, use fork()

to_dict(branches=False) dict#

Returns a dict representation of the log.

undo(n: int = 1, quiet=False) None#

Removes the last message from the log.

write(branches=True) None#

Writes to the conversation log.

gptme.logmanager.get_conversations() Generator[Conversation, None, None]#

Returns all conversations, excluding ones used for testing, evals, etc.

gptme.logmanager.get_user_conversations() Generator[Conversation, None, None]#

Returns all user conversations, excluding ones used for testing, evals, etc.

prompts#

See Prompts for more information.

tools#

Supporting classes and functions for creating and using tools.

class gptme.tools.ToolSpec#

Tool specification. Defines a tool that can be used by the agent.

Parameters:
  • name – The name of the tool.

  • desc – A description of the tool.

  • instructions – Instructions on how to use the tool.

  • examples – Example usage of the tool.

  • functions – Functions registered in the IPython REPL.

  • init – An optional function that is called when the tool is first loaded.

  • execute – An optional function that is called when the tool executes a block.

  • block_types – A list of block types that the tool will execute.

  • available – Whether the tool is available for use.

__init__(name: str, desc: str, instructions: str = '', examples: str = '', functions: list[~collections.abc.Callable] | None = None, init: ~collections.abc.Callable[[], ~gptme.tools.base.ToolSpec] | None = None, execute: ~gptme.tools.base.ExecuteFunc | None = None, block_types: list[str] = <factory>, available: bool = True) None#
get_doc(doc: str | None = None) str#

Returns an updated docstring with examples.

class gptme.tools.ToolUse#

ToolUse(tool: str, args: list[str], content: str, start: int | None = None)

__init__(tool: str, args: list[str], content: str, start: int | None = None) None#
execute(ask: bool) Generator[Message, None, None]#

Executes a tool-use tag and returns the output.

classmethod iter_from_content(content: str) Generator[ToolUse, None, None]#

Returns all ToolUse in a message, markdown or XML, in order.

gptme.tools.execute_msg(msg: Message, ask: bool) Generator[Message, None, None]#

Uses any tools called in a message and returns the response.

server#

See Server for more information.

Server for gptme.

gptme.server.create_app() Flask#

Create the Flask app.