Skip to content

Commit 3e6648b

Browse files
committed
Created using Colaboratory
1 parent 22bb9b3 commit 3e6648b

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed

Custom_Chatgpt_App_Langchain.ipynb

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"mount_file_id": "1zRgNVxa_MvfWHSZL1XtHbm3qTsgNZq36",
8+
"authorship_tag": "ABX9TyOJFcxTO8G5Qany7+BJ5/vY",
9+
"include_colab_link": true
10+
},
11+
"kernelspec": {
12+
"name": "python3",
13+
"display_name": "Python 3"
14+
},
15+
"language_info": {
16+
"name": "python"
17+
}
18+
},
19+
"cells": [
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {
23+
"id": "view-in-github",
24+
"colab_type": "text"
25+
},
26+
"source": [
27+
"<a href=\"https://colab.research.google.com/github/kevatsa/Python-programming-exercises/blob/master/Custom_Chatgpt_App_Langchain.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"source": [
33+
"**CUSTOM CHAT GPT APP WITH LANGCHAIN**"
34+
],
35+
"metadata": {
36+
"id": "ia4UcI3Q-2ar"
37+
}
38+
},
39+
{
40+
"cell_type": "code",
41+
"source": [
42+
"pip install langchain"
43+
],
44+
"metadata": {
45+
"id": "xvdf7qBb_ZEJ"
46+
},
47+
"execution_count": null,
48+
"outputs": []
49+
},
50+
{
51+
"cell_type": "code",
52+
"source": [
53+
"pip install openai"
54+
],
55+
"metadata": {
56+
"id": "kppbuXGkANgf"
57+
},
58+
"execution_count": null,
59+
"outputs": []
60+
},
61+
{
62+
"cell_type": "code",
63+
"source": [
64+
"pip install cohere"
65+
],
66+
"metadata": {
67+
"id": "poyvXwZ9AN9Z"
68+
},
69+
"execution_count": null,
70+
"outputs": []
71+
},
72+
{
73+
"cell_type": "code",
74+
"source": [
75+
"pip install tiktoken"
76+
],
77+
"metadata": {
78+
"id": "cGhZrptqAy1z"
79+
},
80+
"execution_count": null,
81+
"outputs": []
82+
},
83+
{
84+
"cell_type": "code",
85+
"source": [
86+
"import os\n",
87+
"os.environ['OPENAI_API_KEY']= \"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\""
88+
],
89+
"metadata": {
90+
"id": "GagnfiP1BJh_"
91+
},
92+
"execution_count": 6,
93+
"outputs": []
94+
},
95+
{
96+
"cell_type": "code",
97+
"source": [
98+
"from langchain.chat_models import ChatOpenAI\n",
99+
"from langchain.schema import SystemMessage\n",
100+
"from langchain.chains import LLMChain\n",
101+
"from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholder\n",
102+
"from langchain.memory import ConversationBufferMemory, FileChatMessageHistory\n",
103+
"\n",
104+
"llm = ChatOpenAI(model_name='gpt-3.5-turbo', temperature=1)\n",
105+
"\n",
106+
"history = FileChatMessageHistory('chat_history.txt')\n",
107+
"\n",
108+
"memory = ConversationBufferMemory(\n",
109+
" memory_key='chat_history',\n",
110+
" chat_memory=history,\n",
111+
" return_messages=True\n",
112+
"\n",
113+
")\n",
114+
"\n",
115+
"prompt = ChatPromptTemplate(\n",
116+
" input_variables=['content'],\n",
117+
" messages=[\n",
118+
" SystemMessage(content='You are chatbot having a conversation with a human.'),\n",
119+
" MessagesPlaceholder(variable_name='chat_history'),\n",
120+
" HumanMessagePromptTemplate.from_template('{content}')\n",
121+
" ]\n",
122+
"\n",
123+
")\n",
124+
"\n",
125+
"chain = LLMChain(\n",
126+
" llm=llm,\n",
127+
" memory=memory,\n",
128+
" prompt=prompt,\n",
129+
" verbose=True\n",
130+
")\n",
131+
"\n",
132+
"while True:\n",
133+
" content = input('Your prompt: ').strip() # Remove leading/trailing whitespaces\n",
134+
" if not content:\n",
135+
" print('Please enter a prompt.')\n",
136+
" continue\n",
137+
"\n",
138+
" if content in ['quit', 'exit', 'bye']:\n",
139+
" print('Goodbye! ')\n",
140+
" break\n",
141+
"\n",
142+
" response = chain.run({'content': content})\n",
143+
" print(response)\n",
144+
" print('-' * 50)\n",
145+
"\n",
146+
"\n"
147+
],
148+
"metadata": {
149+
"colab": {
150+
"base_uri": "https://localhost:8080/"
151+
},
152+
"id": "EOrUGlyyBTcI",
153+
"outputId": "1e7fbaae-2e12-4555-cfa9-d40567e033c1"
154+
},
155+
"execution_count": 7,
156+
"outputs": [
157+
{
158+
"output_type": "stream",
159+
"name": "stdout",
160+
"text": [
161+
"Your prompt: what is the distance of earth from the sun\n",
162+
"\n",
163+
"\n",
164+
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
165+
"Prompt after formatting:\n",
166+
"\u001b[32;1m\u001b[1;3mSystem: You are chatbot having a conversation with a human.\n",
167+
"Human: what is the distance of earth from the sun\u001b[0m\n",
168+
"\n",
169+
"\u001b[1m> Finished chain.\u001b[0m\n",
170+
"The average distance between Earth and the Sun is approximately 93 million miles or about 150 million kilometers. This is known as an astronomical unit (AU) and is used as a standard unit to measure distances within our solar system.\n",
171+
"--------------------------------------------------\n",
172+
"Your prompt: what is the speed of light\n",
173+
"\n",
174+
"\n",
175+
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
176+
"Prompt after formatting:\n",
177+
"\u001b[32;1m\u001b[1;3mSystem: You are chatbot having a conversation with a human.\n",
178+
"Human: what is the distance of earth from the sun\n",
179+
"AI: The average distance between Earth and the Sun is approximately 93 million miles or about 150 million kilometers. This is known as an astronomical unit (AU) and is used as a standard unit to measure distances within our solar system.\n",
180+
"Human: what is the speed of light\u001b[0m\n",
181+
"\n",
182+
"\u001b[1m> Finished chain.\u001b[0m\n",
183+
"The speed of light is approximately 186,282 miles per second (299,792 kilometers per second) in a vacuum. It is considered a universal constant and is denoted by the symbol \"c\" in physics equations. The speed of light is incredibly fast, and it is often used as a reference when discussing the vast distances and timescales in our universe.\n",
184+
"--------------------------------------------------\n",
185+
"Your prompt: how long does it take to reach earth\n",
186+
"\n",
187+
"\n",
188+
"\u001b[1m> Entering new LLMChain chain...\u001b[0m\n",
189+
"Prompt after formatting:\n",
190+
"\u001b[32;1m\u001b[1;3mSystem: You are chatbot having a conversation with a human.\n",
191+
"Human: what is the distance of earth from the sun\n",
192+
"AI: The average distance between Earth and the Sun is approximately 93 million miles or about 150 million kilometers. This is known as an astronomical unit (AU) and is used as a standard unit to measure distances within our solar system.\n",
193+
"Human: what is the speed of light\n",
194+
"AI: The speed of light is approximately 186,282 miles per second (299,792 kilometers per second) in a vacuum. It is considered a universal constant and is denoted by the symbol \"c\" in physics equations. The speed of light is incredibly fast, and it is often used as a reference when discussing the vast distances and timescales in our universe.\n",
195+
"Human: how long does it take to reach earth\u001b[0m\n",
196+
"\n",
197+
"\u001b[1m> Finished chain.\u001b[0m\n",
198+
"To clarify, if you're referring to how long it takes for light from the Sun to reach Earth, it takes approximately 8 minutes and 20 seconds. This is because light travels at the speed of 299,792 kilometers per second, and with the average distance between the Sun and Earth being around 93 million miles or 150 million kilometers, it takes around 8 minutes and 20 seconds for light to travel this distance.\n",
199+
"\n",
200+
"However, if you are referring to how long it takes for something to physically travel from a certain distance to Earth, the time would depend on the speed of the object and the distance it needs to cover.\n",
201+
"--------------------------------------------------\n",
202+
"Your prompt: exit\n",
203+
"Goodbye! \n"
204+
]
205+
}
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"source": [],
211+
"metadata": {
212+
"id": "L2oUtUdABTfg"
213+
},
214+
"execution_count": null,
215+
"outputs": []
216+
},
217+
{
218+
"cell_type": "code",
219+
"source": [],
220+
"metadata": {
221+
"id": "qhDd7NsgBTit"
222+
},
223+
"execution_count": null,
224+
"outputs": []
225+
},
226+
{
227+
"cell_type": "code",
228+
"source": [],
229+
"metadata": {
230+
"id": "C3E_3dAqBTmV"
231+
},
232+
"execution_count": null,
233+
"outputs": []
234+
}
235+
]
236+
}

0 commit comments

Comments
 (0)