ANALYSIS TOOL BOX

LLM Integration

SendPromptToChatGPT

Send a templated prompt to an OpenAI chat model via LangChain and return its response.

llmainlplanguage-models

Introduction

Reusable prompts — the same instruction filled in with different data each time — are the backbone of most LLM-powered scripts, but wiring up a template, a client, and a parsed response by hand for every call gets repetitive fast. SendPromptToChatGPT wraps that pattern for OpenAI's chat models: give it a prompt template with {placeholder} variables and a dictionary of values to fill them, and it builds the LangChain prompt, sends it to the specified model, and returns the parsed response text.

Teaching Note

This function provides a lightweight, templated interface for sending one-off prompts to OpenAI's chat models without hand-writing LangChain boilerplate each time. It's most useful for scripts and notebooks that need to run the same prompt shape — with different filled-in values — repeatedly, such as batch summarization, extraction, or classification tasks over a list of inputs.

Parameters

ParameterTypeDefaultDescription
prompt_templaterequiredThe prompt text, containing {variable} placeholders that correspond to the keys in user_input.
user_inputrequiredA dictionary mapping each {variable} placeholder name in prompt_template to the text that should be substituted in.
system_message'You are a helpful assistant.'The system-level instruction describing the assistant's persona or role for this request. Defaults to 'You are a helpful assistant.'
openai_api_keyNoneYour OpenAI API key. Required — if None, the function raises a ValueError with a link to the OpenAI platform.
temperature0.0Sampling temperature controlling response randomness, where 0.0 is most deterministic. Defaults to 0.0.
chat_model_name'gpt-4o-mini'The OpenAI chat model ID to send the prompt to. Defaults to 'gpt-4o-mini'.
maximum_tokens1000Maximum number of tokens the model may generate in its response. Defaults to 1000.
verboseTrueWhether to print status output while the request runs. Defaults to True.

Returns

The model's response as a plain string, parsed from the chat model's output.

Example

python
from analysistoolbox.llm import SendPromptToChatGPT

response = SendPromptToChatGPT(
    prompt_template="Summarize the following customer feedback in one sentence: {feedback}",
    user_input={"feedback": "The onboarding flow was confusing and took too long to get through."},
    openai_api_key="sk-...",
)
print(response)