{ "cells": [ { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from langchain.prompts import PromptTemplate\n", "from datetime import datetime" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'lizhen : 18'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prompt_template = PromptTemplate(template=\"{name} : {age}\", input_variables=[\"name\", \"age\"])\n", "partial_prompt_template = prompt_template.partial(name=\"lizhen\")\n", "partial_prompt_template.format(age=\"18\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "foobaz\n" ] } ], "source": [ "prompt = PromptTemplate(template=\"{foo}{bar}\", input_variables=[\"bar\"], partial_variables={\"foo\": \"foo\"})\n", "print(prompt.format(bar=\"baz\"))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'lizhen : 18'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "prompt1 = PromptTemplate(template=\"{name} : {age}\", input_variables=[\"age\"], partial_variables={\"name\": \"lizhen\"})\n", "prompt1.format(age=\"18\")" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "def _get_time():\n", " now = datetime.now()\n", " return now.strftime(\"%Y/%m/%d %H:%M:%S\")" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2023/04/21 17:46:37, 北京气温多少?'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "date_prompt = PromptTemplate(template=\"{date}, {city}气温多少?\",input_variables=[\"date\",\"city\"])\n", "date_partial_prompt = date_prompt.partial(date=_get_time)\n", "date_partial_prompt.format(city=\"北京\")" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'2023/04/21 17:48:50, 天津气温多少?'" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "date_prompt1 = PromptTemplate(template=\"{date}, {city}气温多少?\", input_variables=[\"city\"], partial_variables={\"date\": _get_time})\n", "date_prompt1.format(city=\"天津\")" ] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }