Skip to main content

This package is responsible for calling prompts in a specific format. It uses LangChain and OpenAI API

Project description

PromptCaller

PromptCaller is a Python package for calling prompts in a specific format, using LangChain and the OpenAI API. It enables users to load prompts from a template, render them with contextual data, and make structured requests to the OpenAI API.

Features

  • Load prompts from a .prompt file containing a YAML configuration and a message template.
  • Invoke prompts using LangChain and OpenAI API, with support for structured output.

Installation

To install the package, simply run:

pip install prompt-caller

You will also need an .env file that contains your OpenAI API key:

OPENAI_API_KEY=your_openai_api_key_here

Usage

  1. Define a prompt file:

Create a .prompt file in the prompts directory, e.g., prompts/sample.prompt:

---
model: gpt-4o-mini
temperature: 0.7
max_tokens: 512
output:
  result: "Final result of the expression"
  explanation: "Explanation of the calculation"
---
<system>
You are a helpful assistant.
</system>

<user>
How much is {{expression}}?
</user>

This .prompt file contains:

  • A YAML header for configuring the model and parameters.
  • A template body using Jinja2 to inject the context (like {{ expression }}).
  • Messages structured in a JSX-like format (<system>, <user>).
  1. Load and call a prompt:
from prompt_caller import PromptCaller

ai = PromptCaller()

response = ai.call("sample", {"expression": "3+8/9"})

print(response)

In this example:

  • The expression value 3+8/9 is injected into the user message.
  • The model will respond with both the result of the expression and an explanation, as specified in the output section of the prompt.
  1. Using the agent feature:

The agent method allows you to enhance the prompt's functionality by integrating external tools. Here’s an example where we evaluate a mathematical expression using Python’s eval in a safe execution environment:

from prompt_caller import PromptCaller

ai = PromptCaller()

def evaluate_expression(expression: str):
      """
      Evaluate a math expression using eval.
      """
      safe_globals = {"__builtins__": None}
      return eval(expression, safe_globals, {})

response = ai.agent(
      "sample-agent", {"expression": "3+8/9"}, tools=[evaluate_expression]
)

print(response)

In this example:

  • The agent method is used to process the prompt while integrating external tools.
  • The evaluate_expression function evaluates the mathematical expression securely.
  • The response includes the processed result based on the prompt and tool execution.

How It Works

  1. _loadPrompt: Loads the prompt file, splits the YAML header from the body, and parses them.
  2. _renderTemplate: Uses the Jinja2 template engine to render the body with the provided context.
  3. _parseJSXBody: Parses the message body written in JSX-like tags to extract system and user messages.
  4. call: Invokes the OpenAI API with the parsed configuration and messages, and handles structured output via dynamic Pydantic models.

Build and Upload

To build the distribution and upload it to a package repository like PyPI, follow these steps:

  1. Build the distribution:

    Run the following command to create both source (sdist) and wheel (bdist_wheel) distributions:

    python setup.py sdist bdist_wheel
    

    This will generate the distribution files in the dist/ directory.

  2. Upload to PyPI using Twine:

    Use twine to securely upload the distribution to PyPI:

    twine upload dist/*
    

    Ensure you have configured your PyPI credentials before running this command. You can find more information on configuring credentials in the Twine documentation.

License

This project is licensed under the Apache License 2.0. You may use, modify, and distribute this software as long as you provide proper attribution and include the full text of the license in any distributed copies or derivative works.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prompt_caller-0.2.0.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prompt_caller-0.2.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file prompt_caller-0.2.0.tar.gz.

File metadata

  • Download URL: prompt_caller-0.2.0.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for prompt_caller-0.2.0.tar.gz
Algorithm Hash digest
SHA256 cf9fa181b36db57ce7db77df905c5e3c5fc44963039a89d4091aca39e915b288
MD5 390761550b4c7645c205ff5e3b0d7b37
BLAKE2b-256 443af2275c6951e9182ae65d572c497a12c37c036cb6556256debba1bba8725a

See more details on using hashes here.

File details

Details for the file prompt_caller-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: prompt_caller-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for prompt_caller-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8f08f6bf4d9eb029103adb20667827909b65a1266d2b1841bc95a9973f68fb57
MD5 881741d76f1dc0299ad561087fe31163
BLAKE2b-256 312a1d2c3d4801c9bfb6712cd80c9dbf8b1a8ab2fda11a7ca8b4ec93a89803f3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page