File size: 5,406 Bytes
cfd3735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "3651e424",
   "metadata": {},
   "source": [
    "# Getting Started\n",
    "\n",
    "This section contains everything related to prompts. A prompt is the value passed into the Language Model. This value can either be a string (for LLMs) or a list of messages (for Chat Models).\n",
    "\n",
    "The data types of these prompts are rather simple, but their construction is anything but. Value props of LangChain here include:\n",
    "\n",
    "- A standard interface for string prompts and message prompts\n",
    "- A standard (to get started) interface for string prompt templates and message prompt templates\n",
    "- Example Selectors: methods for inserting examples into the prompt for the language model to follow\n",
    "- OutputParsers: methods for inserting instructions into the prompt as the format in which the language model should output information, as well as methods for then parsing that string output into a format.\n",
    "\n",
    "We have in depth documentation for specific types of string prompts, specific types of chat prompts, example selectors, and output parsers.\n",
    "\n",
    "Here, we cover a quick-start for a standard interface for getting started with simple prompts."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff34414d",
   "metadata": {},
   "source": [
    "## PromptTemplates\n",
    "\n",
    "PromptTemplates are responsible for constructing a prompt value. These PromptTemplates can do things like formatting, example selection, and more. At a high level, these are basically objects that expose a `format_prompt` method for constructing a prompt. Under the hood, ANYTHING can happen."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7ce42639",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.prompts import PromptTemplate, ChatPromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5a178697",
   "metadata": {},
   "outputs": [],
   "source": [
    "string_prompt = PromptTemplate.from_template(\"tell me a joke about {subject}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f4ef6d6b",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat_prompt = ChatPromptTemplate.from_template(\"tell me a joke about {subject}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5f16c8f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "string_prompt_value = string_prompt.format_prompt(subject=\"soccer\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "863755ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "chat_prompt_value = chat_prompt.format_prompt(subject=\"soccer\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b3d8511",
   "metadata": {},
   "source": [
    "## `to_string`\n",
    "\n",
    "This is what is called when passing to an LLM (which expects raw text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1964a8a0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'tell me a joke about soccer'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "string_prompt_value.to_string()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bf6c94e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Human: tell me a joke about soccer'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_prompt_value.to_string()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0825af8",
   "metadata": {},
   "source": [
    "## `to_messages`\n",
    "\n",
    "This is what is called when passing to ChatModel (which expects a list of messages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "e4da46f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='tell me a joke about soccer', additional_kwargs={}, example=False)]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "string_prompt_value.to_messages()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "eae84b88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='tell me a joke about soccer', additional_kwargs={}, example=False)]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chat_prompt_value.to_messages()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a34fa440",
   "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.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}