### Install Promplate with OpenAI support using pip Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Installs the Promplate library with the necessary dependencies for OpenAI integration. This is the first step to using Promplate with OpenAI models. ```sh pip install promplate[openai] ``` -------------------------------- ### Create a template with explicit context Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Creates a template and explicitly provides context variables, such as the 'time' module. This allows for more controlled rendering. ```python import time from promplate import Template greet = Template("Greet me. It is {{ time.asctime() }} now.", {"time": time}) ``` -------------------------------- ### Make a simple LLM call with ChatComplete Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Demonstrates a basic LLM call using the initialized ChatComplete client. It sends a simple prompt and expects a text response. ```python complete("hi", model="gpt-3.5-turbo-0125") ``` -------------------------------- ### Make an LLM call with TextComplete and parameters Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Demonstrates calling the TextComplete client with specific parameters like temperature and max_tokens. This allows fine-tuning the LLM's response. ```python complete("1 + 1 = ", temperature=0, max_tokens=1) ``` -------------------------------- ### Install Promplate Source: https://github.com/promplate/py-docs/blob/main/docs/index.md Installs the Promplate package using pip. For OpenAI integration, install with the 'openai' extra. This is a prerequisite for using Promplate with OpenAI models. ```sh pip install promplate #!sh pip install promplate[openai] ``` -------------------------------- ### Initialize and use OpenAI TextComplete LLM client Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Initializes a TextComplete client for making text-based LLM calls, typically for instruct models. Requires an API key and a specific instruct model. ```python from promplate.llm.openai import TextComplete complete = TextComplete(api_key="...").bind(model="gpt-3.5-turbo-instruct") ``` -------------------------------- ### Create a simple template with dynamic data Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Creates a Promplate Template object that includes dynamic data placeholders (e.g., using `{{ }}`). This template can be rendered with specific context. ```python from promplate import Template greet = Template("Greet me. It is {{ time.asctime() }} now.") ``` -------------------------------- ### Render a template with explicit context Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Renders the template using the context provided during its creation. No additional parameters are needed if all required variables are in the context. ```python greet.render() ``` -------------------------------- ### Initialize OpenAI ChatComplete LLM client Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Initializes a ChatComplete client for making chat-based LLM calls. Requires an OpenAI API key. This client wraps OpenAI's SDK for convenience. ```python from promplate.llm.openai import ChatComplete complete = ChatComplete(api_key="...") ``` -------------------------------- ### Initialize and use OpenAI ChatGenerate for streaming responses Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Initializes a ChatGenerate client for streaming LLM responses token by token. This is useful for real-time updates and interactive applications. ```python from promplate.llm.openai import ChatGenerate generate = ChatGenerate(api_key="...").bind(model="gpt-3.5-turbo-0125") ``` -------------------------------- ### Initialize ChatComplete with a free proxy base URL Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Initializes the ChatComplete client using a provided free proxy service as the base URL. Useful if you don't have an OpenAI API key. ```python from promplate.llm.openai import ChatComplete complete = ChatComplete(base_url="https://promplate.dev", api_key="").bind(model="gpt-3.5-turbo-0125") ``` -------------------------------- ### Render a template with local variables Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Renders the template by providing local variables as context. The expression inside `{{ }}` will be evaluated using these variables. ```python import time greet.render(locals()) ``` -------------------------------- ### Create a Prompt Chain with Promplate and OpenAI Source: https://github.com/promplate/py-docs/blob/main/docs/index.md Demonstrates creating a prompt chain using Promplate's Node and integrating with OpenAI's ChatComplete. It involves reading Jinja2 templates, chaining them, and binding an LLM model. Requires 'openai' to be installed. ```python from promplate.llm.openai import ChatComplete from promplate import Node reply = Node.read("reply.j2") translate = Node.read("translate.j2") translate.run_config["temperature"] = 0 chain = reply + translate complete = ChatComplete().bind(model="gpt-3.5-turbo") context = {"lang": "chinese"} ``` -------------------------------- ### Initialize and Render a Promplate Node Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Demonstrates how to create a 'Node' instance from a template string and render it. The 'Node' constructor takes a template string and optional local variables. The `.render()` method processes the template, substituting placeholders like `{{ time.asctime() }}`. ```python from promplate import Node greet = Node("Greet me. It is {{ time.asctime() }} now.", locals()) print(greet.render()) ``` -------------------------------- ### Stream an LLM response using ChatGenerate Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Iterates over the ChatGenerate client to print the LLM's response as it's generated. This showcases the streaming capability. ```python for i in generate("Explain why 1 + 1 = 2"): print(i, end="", flush=True) ``` -------------------------------- ### MkDocs Build and Deployment Commands Source: https://context7.com/promplate/py-docs/llms.txt Provides essential shell commands for managing the MkDocs documentation build process. This includes installing dependencies, running a development server with live reloading, building the static site, performing optimization steps, and deploying to GitHub Pages. ```bash # Install dependencies pip install mkdocs mkdocs-material mkdocstrings-python promplate # Development server with live reload mkdocs serve --dirtyreload # Build static site mkdocs build # Full production build with optimization mkdocs build && python -m scripts.patch && python -m scripts.minify # Output directory: ./site/ # Deploy to GitHub Pages: mkdocs gh-deploy ``` -------------------------------- ### OpenAI Text Completion (Instruct Models) in Python Source: https://context7.com/promplate/py-docs/llms.txt Utilizes OpenAI's instruct models for text completion tasks. Shows initialization, simple completions, and controlling generation with parameters like temperature and max_tokens. Includes an example with constrained output. ```python from promplate.llm.openai import TextComplete # Initialize text completion client complete = TextComplete(api_key="sk-...").bind(model="gpt-3.5-turbo-instruct") # Simple completion response = complete("I am") print(response) # Output: ' just incredibly proud of the team, and their creation of a brand new ship makes' # Control generation with parameters result = complete("1 + 1 = ", temperature=0, max_tokens=1) print(result) # Output: '2' # More complex prompt with constrained output calculation = complete( "The capital of France is", temperature=0.3, max_tokens=10 ) print(calculation) # Output: ' Paris' ``` -------------------------------- ### Bind a model to ChatComplete for easier calls Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Binds a specific model (e.g., gpt-3.5-turbo-0125) to the ChatComplete client. This allows for simpler calls without repeatedly specifying the model. ```python complete = ChatComplete(api_key="...").bind(model="gpt-3.5-turbo-0125") ``` -------------------------------- ### Stream LLM Responses in Python Source: https://context7.com/promplate/py-docs/llms.txt Enables real-time streaming of generated tokens from LLM responses for improved user experience. This example demonstrates initializing a streaming generator and iterating through tokens. ```python from promplate.llm.openai import ChatGenerate # Initialize streaming generator generate = ChatGenerate(api_key="sk-...").bind(model="gpt-3.5-turbo-0125") # Stream response token by token for token in generate("Explain why 1 + 1 = 2"): print(token, end="", flush=True) # Output (streamed gradually): ``` -------------------------------- ### OpenAI Chat Completion in Python Source: https://context7.com/promplate/py-docs/llms.txt Integrates with OpenAI's chat models for text completion. Demonstrates initializing the client, making simple calls, and binding model parameters for repeated use. Includes an example using a free proxy. ```python from promplate.llm.openai import ChatComplete # Initialize with API key complete = ChatComplete(api_key="sk-...") # Make a simple completion call response = complete("hi", model="gpt-3.5-turbo-0125") print(response) # Output: 'Hello! How can I assist you today?' # Bind model parameter for repeated use complete = ChatComplete(api_key="sk-...").bind(model="gpt-3.5-turbo-0125") # Now call without model parameter response = complete("hi") print(response) # Output: 'Hello! How can I assist you today?' # Use free proxy for testing (no API key required) complete = ChatComplete( base_url="https://promplate.dev", api_key="" ).bind(model="gpt-3.5-turbo-0125") result = complete("hi") print(result) # Output: 'Hello! How can I assist you today?' ``` -------------------------------- ### Chain Promplate Nodes for Sequential Execution Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Shows how to combine two 'Node' instances into a chain using the addition operator. This creates a pipeline where the output of the first node (`greet`) becomes the input (`__result__`) for the second node (`translate`). The `.invoke()` method executes the chain, and `.result` accesses the final output. ```python translate = Node('translate \"{{ __result__ }}\" into {{ target_language }}') chain = greet + translate # this represents the pipeline of "greeting in another language" print(chain.invoke({"target_language": "zh_CN"}, complete).result) ``` -------------------------------- ### Python: Basic LLM Response Collection Source: https://context7.com/promplate/py-docs/llms.txt Demonstrates how to collect a full response from a stream using the generate function in Python. It shows a simple example of calling 'What is Python?' and printing the response. ```python # Collect full response from stream response = "".join(generate("What is Python?")) print(response) # Output: 'Python is a high-level, interpreted programming language...' ``` -------------------------------- ### PDM Script Configuration for MkDocs Source: https://context7.com/promplate/py-docs/llms.txt Shows how to configure PDM (Python Development Master) scripts for building and managing MkDocs documentation. It defines tasks for development, minification, patching, and a composite build process, along with examples of how to run these scripts. ```python # pyproject.toml build scripts # [tool.pdm.scripts] # dev = "mkdocs serve --dirtyreload" # minify = { call = "scripts.minify:main" } # patch = { call = "scripts.patch:main" } # gen = { composite = ["mkdocs build", "patch", "minify"] } # Run with PDM # pdm run dev # Start development server ``` -------------------------------- ### File-Based Chat Templates with Chat Markup (Promplate) Source: https://context7.com/promplate/py-docs/llms.txt Shows how to use separate template files for chat prompts with Promplate in Python, leveraging the special chat markup syntax for better organization. It includes an example of a template file structure. ```python from promplate import Node from promplate.llm.openai import ChatComplete # Template file: reply.j2 # {# import time #} # # <|system|> # current time: {{ time.localtime() }} # # <|user|> # Say happy new year to me in no more than 5 words. # Note that you must include the year in the message. ``` -------------------------------- ### Register Callback to Process Node Output Source: https://github.com/promplate/py-docs/blob/main/docs/quick-start.md Illustrates how to register a callback function to be executed after a node finishes processing using the `@node.end_process` decorator. The callback function receives the execution context and can access the result and other context variables, allowing for actions like logging or data transformation. ```python @greet.end_process def log_greet_result(context): print(context.result) print(context["target_language"]) # Assuming 'chain' and 'complete' are defined from previous examples # print(chain.invoke({"target_language": "zh_CN"}, complete).result) ``` -------------------------------- ### Python: Callback Registration for Post-Processing with Promplate Source: https://context7.com/promplate/py-docs/llms.txt Explains how to register callbacks for pre-processing and post-processing LLM responses within a Promplate chain in Python. It demonstrates using decorator syntax to log results, modify context, and shows an example of a pre-process callback modifying input. ```python from promplate import Node from promplate.llm.openai import ChatComplete # Initialize components complete = ChatComplete(api_key="sk-...").bind(model="gpt-3.5-turbo-0125") greet = Node("Greet me in English") translate = Node('translate """{{ __result__ }}""" into {{ target_language }}') # Register callback using decorator syntax @greet.end_process def log_greet_result(context): print(f"Greeting result: {context.result}") print(f"Target language: {context['target_language']}") # Callbacks can modify context context["logged"] = True # Create chain chain = greet + translate # Execute chain - callback will be triggered after greet node result = chain.invoke({"target_language": "zh_CN"}, complete) # Console output during execution: # Greeting result: Good morning! # zh_CN print(result.result) # Output: '早上好!' print(result["logged"]) # Output: True # Pre-process callback example @translate.pre_process def modify_input(context): print(f"About to translate: {context['__result__']}") # Can modify context before node execution context["target_language"] = context["target_language"].upper() # Multiple callbacks can be registered on the same node ``` -------------------------------- ### Invoke Prompt Chain and Get Result Source: https://github.com/promplate/py-docs/blob/main/docs/index.md Executes a previously defined prompt chain ('chain') with specified context and an LLM completion configuration. It retrieves the final result from the invoked chain. This operation requires the 'chain', 'context', and 'complete' variables to be defined beforehand. ```python chain.invoke({"lang": "chinese"}, complete).result ``` -------------------------------- ### Read Prompts from Files (Python) Source: https://github.com/promplate/py-docs/blob/main/docs/index.md Demonstrates how to read prompt templates from local files synchronously and asynchronously using Promplate. Supports Jinja2 and Markdown file formats. ```python from promplate import Template foo = Template.read("path/to/some-template.j2") // synchronous bar = await Template.aread("path/to/some-prompt.md") // asynchronous ``` -------------------------------- ### Fetch Prompts from HTTP URLs (Python) Source: https://github.com/promplate/py-docs/blob/main/docs/index.md Illustrates fetching prompt templates from remote HTTP URLs synchronously and asynchronously using Promplate. Allows for dynamic loading of templates from web resources. ```python from promplate import Template foo = Template.fetch("https://your-domain.com/path/to/some-template.j2") // synchronous bar = await Template.afetch("https://your-domain.com/path/to/some-prompt.md") // asynchronous ``` -------------------------------- ### Create and Render Dynamic Templates in Python Source: https://context7.com/promplate/py-docs/llms.txt Demonstrates how to create dynamic prompt templates using Jinja2-like syntax with Python expression interpolation. It shows rendering templates with local context and pre-bound context variables. ```python import time from promplate import Template # Create template with dynamic expressions greet = Template("Greet me. It is {{ time.asctime() }} now.") # Render with local context rendered = greet.render(locals()) print(rendered) # Output: 'Greet me. It is Sun Oct 1 03:56:02 2023 now.' # Create template with pre-bound context greet_with_context = Template( "Greet me. It is {{ time.asctime() }} now.", {"time": time} ) result = greet_with_context.render() print(result) # Output: 'Greet me. It is Sun Oct 1 03:56:02 2023 now.' # Translation template using context variables translate = Template('Translate this into {{ lang }}: """{{ text }}"""') output = translate.render({"lang": "Spanish", "text": "Hello world"}) print(output) # Output: 'Translate this into Spanish: """Hello world"""' ``` -------------------------------- ### Python: Node-Based Prompt Chaining with Promplate Source: https://context7.com/promplate/py-docs/llms.txt Illustrates creating and chaining multiple prompt templates (Nodes) for complex tasks using Promplate in Python. It shows how to initialize an LLM client, define nodes, chain them, and invoke the chain with context. Intermediate results can be accessed via ChainContext. ```python import time from promplate import Node from promplate.llm.openai import ChatComplete # Initialize LLM client complete = ChatComplete(api_key="sk-...").bind(model="gpt-3.5-turbo-0125") # Create first node for greeting greet = Node("Greet me. It is {{ time.asctime() }} now.", locals()) # Create second node that uses result from first node # __result__ is automatically injected with output from previous node translate = Node('translate """{{ __result__ }}""" into {{ target_language }}') # Chain nodes together with + operator chain = greet + translate # Execute chain with context and LLM result = chain.invoke({"target_language": "zh_CN"}, complete) print(result.result) # Output: '早上好!' # Access intermediate results through ChainContext print(result["target_language"]) # Output: 'zh_CN' # Single node execution simple_node = Node("Say hello in {{ language }}") rendered = simple_node.render({"language": "French"}) print(rendered) # Output: 'Say hello in French' ``` -------------------------------- ### Load Templates from Files and URLs in Python Source: https://context7.com/promplate/py-docs/llms.txt Shows how to load prompt templates from local files (synchronously and asynchronously) and remote HTTP URLs. Template names are automatically extracted from filenames. ```python from promplate import Template # Synchronous file reading foo = Template.read("path/to/some-template.j2") print(foo) # Output: