$ show JamesHWade/gpttools
gpttools is an R package that extends RStudio with large language model (LLM) capabilities for R package development. It provides a suite of RStudio addins for tasks such as generating roxygen documentation, suggesting unit tests, converting scripts to functions, and a Copilot-style ghost text writer. The package also includes a retrieval-augmented generation (RAG) system that scrapes documentation, creates embeddings, and indexes them for context-aware chat. It supports multiple AI providers (OpenAI, Anthropic, Perplexity, Cohere, Ollama, Azure OpenAI, Google) via a modular streaming handler pattern, and offers both cloud-based and local (sentence-transformers, Ollama) embedding options. The architecture is a mix of R functions, Shiny apps (settings, retrieval chat, data documentation), and Python integration via reticulate for local models. Key entry points include the addins, the chat() function, chat_with_context() for RAG, and the crawl() function for building indexes. External dependencies include the gptstudio package, various LLM APIs, and a Python environment for local embeddings. While primarily an application, it incorporates data engineering patterns for scraping, chunking, embedding, and indexing documentation.
The LLM identified 3 files worth highlighting for reuse:
Implements a create_handler() factory that abstracts streaming across multiple LLM providers (OpenAI, Anthropic, Perplexity, Cohere, Ollama, Azure OpenAI). Each provider returns a handler function with a callback for processing streamed tokens. This pattern is isolated, self-contained, and can be extracted to add new providers or reused in other R projects requiring streaming API calls.
Provides a complete pipeline for chunking text with overlap (chunk_with_overlap()), adding embeddings via OpenAI or local sentence-transformers (add_embeddings()), and retrieving top matches (get_top_matches()). The functions are parameterized, independent of the rest of the package, and can be reused in any R project that needs text chunking and embedding-based similarity search.
Manages chat history persistence using Parquet files. Functions save_user_history() and read_history() handle reading/writing with a consistent schema. The implementation is self-contained, uses arrow for Parquet I/O, and can be extracted for any R application needing lightweight, append-only history storage.