| Title: | Unified Interface for Large Language Model Interactions |
|---|---|
| Description: | Provides a unified interface for interacting with Large Language Models (LLMs) through various providers including OpenAI <https://platform.openai.com/docs/api-reference>, Ollama <https://ollama.com/>, and other OpenAI-compatible APIs. Features include automatic connection testing, max_tokens limit auto-adjustment, structured JSON responses with schema validation, interactive JSON schema generation, prompt templating, and comprehensive diagnostics. |
| Authors: | Zaoqu Liu [aut, cre] (ORCID: <https://orcid.org/0000-0002-0452-742X>) |
| Maintainer: | Zaoqu Liu <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.0.0 |
| Built: | 2026-05-27 06:56:43 UTC |
| Source: | https://github.com/Zaoqu-Liu/llmhelper |
This function constructs a structured prompt string by injecting user-supplied parameters into a predefined template. It leverages the glue package to replace named placeholders in the template with actual values, enabling dynamic prompt creation for LLM workflows.
build_prompt(template, ...)build_prompt(template, ...)
template |
A character string containing the prompt template. Placeholders
should be wrapped in |
... |
Named arguments matching placeholders in |
The build_prompt() function uses glue::glue_data() internally. Placeholders
in template (e.g., {filename}, {threshold}) are resolved by passing a named
list of parameters via .... You can include any number of placeholders in
the template, as long as the corresponding argument is supplied when calling
this function.
A single character string with all {placeholder} fields in template
replaced by the corresponding values from ....
Zaoqu Liu; Email: [email protected]
## Not run: # Define a template with placeholders prompt_template <- " Perform the following analysis on dataset at '{filepath}': 1. Load data from '{filepath}' 2. Normalize using method '{norm_method}' 3. Save results to '{output_dir}' IMPORTANT: Use package::function notation for all function calls." # Build the prompt by supplying named arguments filled_prompt <- build_prompt( template = prompt_template, filepath = "/path/to/data.csv", norm_method = "quantile", output_dir = "/path/to/output/" ) cat(filled_prompt) ## End(Not run)## Not run: # Define a template with placeholders prompt_template <- " Perform the following analysis on dataset at '{filepath}': 1. Load data from '{filepath}' 2. Normalize using method '{norm_method}' 3. Save results to '{output_dir}' IMPORTANT: Use package::function notation for all function calls." # Build the prompt by supplying named arguments filled_prompt <- build_prompt( template = prompt_template, filepath = "/path/to/data.csv", norm_method = "quantile", output_dir = "/path/to/output/" ) cat(filled_prompt) ## End(Not run)
This function provides detailed diagnostics for LLM connection issues, helping identify problems at different levels of the stack.
diagnose_llm_connection(base_url, api_key, model, test_tidyprompt = TRUE)diagnose_llm_connection(base_url, api_key, model, test_tidyprompt = TRUE)
base_url |
The API base URL |
api_key |
The API key |
model |
The model name |
test_tidyprompt |
Whether to test tidyprompt compatibility |
A list (invisible) containing test results with elements:
connectivity: Logical indicating if basic network connectivity passed
endpoint: Logical indicating if API endpoint is accessible
auth: Logical indicating if authentication and model test passed
tidyprompt: Logical indicating tidyprompt compatibility (if tested)
Extract only the schema part from generated result
extract_schema_only(schema_result)extract_schema_only(schema_result)
schema_result |
Result from generate_json_schema |
Just the schema portion for use with tidyprompt
This function creates an interactive system to generate JSON schemas based on user descriptions. It supports multi-turn conversations until the user is satisfied with the generated schema.
generate_json_schema( description, llm_client, max_iterations = 5, interactive = TRUE, verbose = TRUE )generate_json_schema( description, llm_client, max_iterations = 5, interactive = TRUE, verbose = TRUE )
description |
Initial description of the desired JSON structure |
llm_client |
The LLM provider object (from llm_openai or llm_ollama) |
max_iterations |
Maximum number of refinement iterations (default: 5) |
interactive |
Whether to run in interactive mode (default: TRUE) |
verbose |
Whether to show detailed conversation logs (default: TRUE) |
A list containing the final JSON schema and conversation history
Zaoqu Liu; Email: [email protected]
This function sends a prompt to a Language Learning Model (LLM) and returns either a text response or a JSON-structured response based on the provided parameters. It handles retries, validation, and response formatting automatically.
get_llm_response( prompt, llm_client, max_retries = 5, max_words = NULL, max_characters = NULL, json_schema = NULL, schema_strict = FALSE, schema_type = "auto", verbose = NULL, stream = NULL, clean_chat_history = TRUE, return_mode = c("only_response", "full") )get_llm_response( prompt, llm_client, max_retries = 5, max_words = NULL, max_characters = NULL, json_schema = NULL, schema_strict = FALSE, schema_type = "auto", verbose = NULL, stream = NULL, clean_chat_history = TRUE, return_mode = c("only_response", "full") )
prompt |
A character string or tidyprompt object containing the prompt to send to the LLM. This is the main input that the LLM will respond to. |
llm_client |
An LLM provider object created by functions like |
max_retries |
Integer. Maximum number of retry attempts if the LLM fails to provide a valid response (default: 5). The function will retry if:
|
max_words |
Integer or NULL. Maximum number of words allowed in the response (default: NULL, no limit). Only applies when json_schema is NULL (text responses). If specified, responses exceeding this limit will trigger a retry. Example: max_words = 50 limits response to 50 words or fewer. |
max_characters |
Integer or NULL. Maximum number of characters allowed in the response (default: NULL, no limit). Only applies when json_schema is NULL (text responses). If specified, responses exceeding this limit will trigger a retry. Example: max_characters = 280 limits response to Twitter-like length. |
json_schema |
List or NULL. JSON schema specification for structured responses (default: NULL for text responses). When provided, the LLM will be forced to return a valid JSON object matching the schema. The schema should be a list representing a JSON schema structure with:
|
schema_strict |
Logical. Whether to enforce strict schema validation (default: FALSE). When TRUE:
|
schema_type |
Character. Method for enforcing JSON response format (default: 'auto'). Options:
|
verbose |
Logical or NULL. Whether to print detailed interaction logs to console (default: NULL, uses LLM client's setting). When TRUE:
|
stream |
Logical or NULL. Whether to stream the response in real-time (default: NULL, uses LLM client's setting). When TRUE:
|
clean_chat_history |
Logical. Whether to clean chat history between retries (default: TRUE). When TRUE:
|
return_mode |
Character. What information to return (default: "only_response"). Options:
|
This function serves as a unified interface for getting responses from LLMs with
automatic handling of different response formats and validation. It internally uses
the tidyprompt package's answer_as_text() or answer_as_json() functions
depending on whether a JSON schema is provided.
Text Mode (json_schema = NULL):
Uses answer_as_text() with optional word/character limits
Returns plain text responses
Validates response length constraints
JSON Mode (json_schema provided):
Uses answer_as_json() with schema validation
Forces structured JSON responses
Validates against provided schema
Returns parsed R objects (lists)
Error Handling: The function automatically retries on various failure conditions including validation errors, JSON parsing errors, and network issues.
Depends on return_mode parameter:
If return_mode = "only_response": Character string (text mode) or parsed list (JSON mode)
If return_mode = "full": Named list with response and metadata
NULL if all retry attempts fail
Zaoqu Liu; Email: [email protected]
## Not run: # Basic text response client <- llm_ollama() response <- get_llm_response("What is R?", client) # Text response with word limit short_response <- get_llm_response( "Explain machine learning", client, max_words = 50 ) # JSON response with schema schema <- list( name = "person_info", schema = list( type = "object", properties = list( name = list(type = "string"), age = list(type = "integer") ), required = c("name", "age") ) ) json_response <- get_llm_response( "Create a person with name and age", client, json_schema = schema ) # Full response with metadata full_result <- get_llm_response( "Hello", client, return_mode = "full", verbose = TRUE ) ## End(Not run)## Not run: # Basic text response client <- llm_ollama() response <- get_llm_response("What is R?", client) # Text response with word limit short_response <- get_llm_response( "Explain machine learning", client, max_words = 50 ) # JSON response with schema schema <- list( name = "person_info", schema = list( type = "object", properties = list( name = list(type = "string"), age = list(type = "integer") ), required = c("name", "age") ) ) json_response <- get_llm_response( "Create a person with name and age", client, json_schema = schema ) # Full response with metadata full_result <- get_llm_response( "Hello", client, return_mode = "full", verbose = TRUE ) ## End(Not run)
Get user feedback interactively
get_user_feedback(state, verbose)get_user_feedback(state, verbose)
state |
Current conversation state |
verbose |
Show logs |
Updated conversation state
Returns the left-hand side if it is not NULL, otherwise returns the right-hand side. This is useful for providing default values.
x %||% yx %||% y
x |
A value to check for NULL. |
y |
A default value to return if x is NULL. |
x if not NULL, otherwise y.
NULL %||% "default" "value" %||% "default"NULL %||% "default" "value" %||% "default"
This function creates an Ollama LLM provider with better error handling and follows tidyprompt best practices.
llm_ollama( base_url = "http://localhost:11434/api/chat", model = "qwen2.5:1.5b-instruct", temperature = 0.2, max_tokens = 5000, timeout = 100, stream = TRUE, verbose = TRUE, skip_test = FALSE, auto_download = TRUE, ... )llm_ollama( base_url = "http://localhost:11434/api/chat", model = "qwen2.5:1.5b-instruct", temperature = 0.2, max_tokens = 5000, timeout = 100, stream = TRUE, verbose = TRUE, skip_test = FALSE, auto_download = TRUE, ... )
base_url |
The base URL for the Ollama API |
model |
The model name to use |
temperature |
The temperature parameter for response randomness |
max_tokens |
Maximum number of tokens in response |
timeout |
Request timeout in seconds |
stream |
Whether to use streaming responses |
verbose |
Whether to show verbose output |
skip_test |
Whether to skip the availability test |
auto_download |
Whether to automatically download missing models |
... |
Additional parameters to pass to the model |
A configured LLM provider object
Zaoqu Liu; Email: [email protected]
This function creates an OpenAI-compatible LLM provider with comprehensive error handling and testing capabilities. It automatically handles max_tokens limits by falling back to the model's maximum when exceeded.
llm_provider( base_url = "https://api.openai.com/v1/chat/completions", api_key = NULL, model = "gpt-4o-mini", temperature = 0.2, max_tokens = 5000, timeout = 100, stream = FALSE, verbose = TRUE, skip_test = FALSE, test_mode = c("full", "http_only", "skip"), ... )llm_provider( base_url = "https://api.openai.com/v1/chat/completions", api_key = NULL, model = "gpt-4o-mini", temperature = 0.2, max_tokens = 5000, timeout = 100, stream = FALSE, verbose = TRUE, skip_test = FALSE, test_mode = c("full", "http_only", "skip"), ... )
base_url |
The base URL for the OpenAI-compatible API |
api_key |
The API key for authentication. If NULL, will use LLM_API_KEY env var |
model |
The model name to use |
temperature |
The temperature parameter for response randomness |
max_tokens |
Maximum number of tokens in response (will auto-adjust if exceeds model limit) |
timeout |
Request timeout in seconds |
stream |
Whether to use streaming responses |
verbose |
Whether to show verbose output |
skip_test |
Whether to skip the availability test (useful for problematic providers) |
test_mode |
The testing mode: "full", "http_only", "skip" |
... |
Additional parameters to pass to the model |
A configured LLM provider object
Zaoqu Liu; Email: [email protected]
This function sends a DELETE request to remove a specified model from the Ollama API and returns the updated model list.
ollama_delete_model(.model, .ollama_server = "http://localhost:11434")ollama_delete_model(.model, .ollama_server = "http://localhost:11434")
.model |
The name of the model to delete |
.ollama_server |
The URL of the Ollama server (default: "http://localhost:11434") |
Updated tibble of available models after deletion
Zaoqu Liu; Email: [email protected]
This function sends a request to download a specified model from Ollama's model library with progress tracking.
ollama_download_model(.model, .ollama_server = "http://localhost:11434")ollama_download_model(.model, .ollama_server = "http://localhost:11434")
.model |
The name of the model to download |
.ollama_server |
The URL of the Ollama server (default: "http://localhost:11434") |
No return value, called for side effects (downloads the model to the Ollama server with progress displayed in the console).
Zaoqu Liu; Email: [email protected]
This function retrieves information about available models from the Ollama API and returns it as a tibble with simplified data extraction.
ollama_list_models(.ollama_server = "http://localhost:11434")ollama_list_models(.ollama_server = "http://localhost:11434")
.ollama_server |
The URL of the Ollama server (default: "http://localhost:11434") |
A tibble containing model information, or NULL if no models are found
Zaoqu Liu; Email: [email protected]
This function creates a prompt object with system and user prompts using the tidyprompt package for structured LLM communication.
set_prompt( system = "You are an AI assistant specialized in bioinformatics.", user = "Hi" )set_prompt( system = "You are an AI assistant specialized in bioinformatics.", user = "Hi" )
system |
The system prompt to set context and behavior (default: bioinformatics assistant) |
user |
The user prompt or question |
A prompt object configured with system and user prompts
Zaoqu Liu; Email: [email protected]