{ "cells": [ { "cell_type": "markdown", "id": "d7467b67", "metadata": {}, "source": [ "# Optimized Prompts\n", "\n", "This example showcases how using the OptimizedPrompt class enables selection of the most relevant examples to include as few-shot examples in the prompt." ] }, { "cell_type": "code", "execution_count": 1, "id": "e9e2b50b", "metadata": {}, "outputs": [], "source": [ "from langchain.chains.react.prompt import EXAMPLES, SUFFIX\n", "from langchain.embeddings.openai import OpenAIEmbeddings\n", "from langchain.example_generator import generate_example, generate_example_from_dynamic_prompt\n", "from langchain.llms.openai import OpenAI\n", "from langchain.prompts.optimized import OptimizedPrompt\n", "from langchain.vectorstores.elastic_vector_search import ElasticVectorSearch\n", "from langchain.vectorstores.faiss_search import FAISS" ] }, { "cell_type": "code", "execution_count": 2, "id": "cb069606", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Question: What is the elevation range for the area that the eastern sector of the\\nColorado orogeny extends into?\\nThought 1: I need to search Colorado orogeny, find the area that the eastern sector\\nof the Colorado orogeny extends into, then find the elevation range of the\\narea.\\nAction 1: Search[Colorado orogeny]\\nObservation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in\\nColorado and surrounding areas.\\nThought 2: It does not mention the eastern sector. So I need to look up eastern\\nsector.\\nAction 2: Lookup[eastern sector]\\nObservation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called\\nthe Central Plains orogeny.\\nThought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I\\nneed to search High Plains and find its elevation range.\\nAction 3: Search[High Plains]\\nObservation 3: High Plains refers to one of two distinct land regions\\nThought 4: I need to instead search High Plains (United States).\\nAction 4: Search[High Plains (United States)]\\nObservation 4: The High Plains are a subregion of the Great Plains. From east to west, the\\nHigh Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130\\nm).[3]\\nThought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer\\nis 1,800 to 7,000 ft.\\nAction 5: Finish[1,800 to 7,000 ft]'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "EXAMPLES[0]" ] }, { "cell_type": "code", "execution_count": 3, "id": "5fda75a4", "metadata": {}, "outputs": [], "source": [ "prompt = OptimizedPrompt.from_examples(\n", " examples=EXAMPLES, \n", " suffix=SUFFIX, \n", " input_variables=[\"input\"],\n", " embeddings=OpenAIEmbeddings(),\n", " vectorstore_cls=FAISS\n", ")" ] }, { "cell_type": "code", "execution_count": 4, "id": "7a601df8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "Question: What is the elevation range for the area that the eastern sector of the\n", "Colorado orogeny extends into?\n", "Thought 1: I need to search Colorado orogeny, find the area that the eastern sector\n", "of the Colorado orogeny extends into, then find the elevation range of the\n", "area.\n", "Action 1: Search[Colorado orogeny]\n", "Observation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in\n", "Colorado and surrounding areas.\n", "Thought 2: It does not mention the eastern sector. So I need to look up eastern\n", "sector.\n", "Action 2: Lookup[eastern sector]\n", "Observation 2: (Result 1 / 1) The eastern sector extends into the High Plains and is called\n", "the Central Plains orogeny.\n", "Thought 3: The eastern sector of Colorado orogeny extends into the High Plains. So I\n", "need to search High Plains and find its elevation range.\n", "Action 3: Search[High Plains]\n", "Observation 3: High Plains refers to one of two distinct land regions\n", "Thought 4: I need to instead search High Plains (United States).\n", "Action 4: Search[High Plains (United States)]\n", "Observation 4: The High Plains are a subregion of the Great Plains. From east to west, the\n", "High Plains rise in elevation from around 1,800 to 7,000 ft (550 to 2,130\n", "m).[3]\n", "Thought 5: High Plains rise in elevation from around 1,800 to 7,000 ft, so the answer\n", "is 1,800 to 7,000 ft.\n", "Action 5: Finish[1,800 to 7,000 ft]\n", "\n", "\n", "\n", "Question: What is the highest mountain peak in Asia?\n" ] } ], "source": [ "print(prompt.format(k=1, input=\"What is the highest mountain peak in Asia?\"))" ] }, { "cell_type": "markdown", "id": "a5dc3525", "metadata": {}, "source": [ "## Requires having ElasticSearch setup" ] }, { "cell_type": "code", "execution_count": null, "id": "bbd92d08", "metadata": {}, "outputs": [], "source": [ "prompt = OptimizedPrompt.from_examples(\n", " examples=EXAMPLES, \n", " suffix=SUFFIX, \n", " input_variables=[\"input\"],\n", " embeddings=OpenAIEmbeddings(),\n", " vectorstore_cls=ElasticVectorSearch,\n", " elasticsearch_url=\"http://localhost:9200\"\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "bd91f408", "metadata": {}, "outputs": [], "source": [ "print(prompt.format(k=1, input=\"What is the highest mountain peak in Asia?\"))" ] }, { "cell_type": "code", "execution_count": null, "id": "716165c2", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.7.6" } }, "nbformat": 4, "nbformat_minor": 5 }