Spaces:
Sleeping
Sleeping
S-MurilloG
commited on
Commit
•
90aaca3
1
Parent(s):
6f7b151
Prueba LangChain
Browse files- CARSE_03_LangChain.ipynb +177 -0
CARSE_03_LangChain.ipynb
ADDED
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 2,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"#! pip install langchain"
|
10 |
+
]
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"cell_type": "code",
|
14 |
+
"execution_count": 3,
|
15 |
+
"metadata": {},
|
16 |
+
"outputs": [],
|
17 |
+
"source": [
|
18 |
+
"from langchain.chains import ConversationChain\n",
|
19 |
+
"from dotenv import load_dotenv\n",
|
20 |
+
"from langchain import OpenAI\n",
|
21 |
+
"import os"
|
22 |
+
]
|
23 |
+
},
|
24 |
+
{
|
25 |
+
"cell_type": "code",
|
26 |
+
"execution_count": 4,
|
27 |
+
"metadata": {},
|
28 |
+
"outputs": [],
|
29 |
+
"source": [
|
30 |
+
"load_dotenv()\n",
|
31 |
+
"API_KEY = os.getenv('OPENAI_KEY')\n",
|
32 |
+
"client = OpenAI()"
|
33 |
+
]
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"cell_type": "markdown",
|
37 |
+
"metadata": {},
|
38 |
+
"source": [
|
39 |
+
"# Conversación con Conversation Buffer Window Memory"
|
40 |
+
]
|
41 |
+
},
|
42 |
+
{
|
43 |
+
"cell_type": "code",
|
44 |
+
"execution_count": 5,
|
45 |
+
"metadata": {},
|
46 |
+
"outputs": [],
|
47 |
+
"source": [
|
48 |
+
"from langchain.chains.conversation.memory import ConversationBufferWindowMemory"
|
49 |
+
]
|
50 |
+
},
|
51 |
+
{
|
52 |
+
"cell_type": "code",
|
53 |
+
"execution_count": 6,
|
54 |
+
"metadata": {},
|
55 |
+
"outputs": [],
|
56 |
+
"source": [
|
57 |
+
"carse = OpenAI(model_name = \"ft:gpt-3.5-turbo-1106:personal:carse:8U71tg31\",\n",
|
58 |
+
" temperature = 0,\n",
|
59 |
+
" model_kwargs={\"stop\": \"\\nHuman:\"})"
|
60 |
+
]
|
61 |
+
},
|
62 |
+
{
|
63 |
+
"cell_type": "code",
|
64 |
+
"execution_count": 7,
|
65 |
+
"metadata": {},
|
66 |
+
"outputs": [],
|
67 |
+
"source": [
|
68 |
+
"window_memory = ConversationBufferWindowMemory(k=5)"
|
69 |
+
]
|
70 |
+
},
|
71 |
+
{
|
72 |
+
"cell_type": "code",
|
73 |
+
"execution_count": 8,
|
74 |
+
"metadata": {},
|
75 |
+
"outputs": [],
|
76 |
+
"source": [
|
77 |
+
"conversation = ConversationChain(\n",
|
78 |
+
" llm = carse,\n",
|
79 |
+
" verbose = True,\n",
|
80 |
+
" memory = window_memory\n",
|
81 |
+
")"
|
82 |
+
]
|
83 |
+
},
|
84 |
+
{
|
85 |
+
"cell_type": "code",
|
86 |
+
"execution_count": 12,
|
87 |
+
"metadata": {},
|
88 |
+
"outputs": [
|
89 |
+
{
|
90 |
+
"name": "stdout",
|
91 |
+
"output_type": "stream",
|
92 |
+
"text": [
|
93 |
+
"\n",
|
94 |
+
"\n",
|
95 |
+
"\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
|
96 |
+
"Prompt after formatting:\n",
|
97 |
+
"\u001b[32;1m\u001b[1;3mThe following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.\n",
|
98 |
+
"\n",
|
99 |
+
"Current conversation:\n",
|
100 |
+
"Human: Hola amor, cómo dormiste hoy?\n",
|
101 |
+
"AI: Hola! Dormí muy bien, gracias. Y tú?\n",
|
102 |
+
"Human: Bien bien, la verdad no me quejo nada\n",
|
103 |
+
"AI: Me alegra escuchar eso. Cómo estuvo tu día ayer?\n",
|
104 |
+
"Human: hace mucho calooor\n",
|
105 |
+
"AI:\u001b[0m\n",
|
106 |
+
"\n",
|
107 |
+
"\u001b[1m> Finished chain.\u001b[0m\n"
|
108 |
+
]
|
109 |
+
},
|
110 |
+
{
|
111 |
+
"data": {
|
112 |
+
"text/plain": [
|
113 |
+
"' Sí, el clima ha estado muy caliente últimamente. Dónde estabas?'"
|
114 |
+
]
|
115 |
+
},
|
116 |
+
"execution_count": 12,
|
117 |
+
"metadata": {},
|
118 |
+
"output_type": "execute_result"
|
119 |
+
}
|
120 |
+
],
|
121 |
+
"source": [
|
122 |
+
"respuesta = input(\"Contesta aquí:\")\n",
|
123 |
+
"conversation.predict(input=respuesta)"
|
124 |
+
]
|
125 |
+
},
|
126 |
+
{
|
127 |
+
"cell_type": "code",
|
128 |
+
"execution_count": 13,
|
129 |
+
"metadata": {},
|
130 |
+
"outputs": [
|
131 |
+
{
|
132 |
+
"name": "stdout",
|
133 |
+
"output_type": "stream",
|
134 |
+
"text": [
|
135 |
+
"Human: Hola amor, cómo dormiste hoy?\n",
|
136 |
+
"AI: Hola! Dormí muy bien, gracias. Y tú?\n",
|
137 |
+
"Human: Bien bien, la verdad no me quejo nada\n",
|
138 |
+
"AI: Me alegra escuchar eso. Cómo estuvo tu día ayer?\n",
|
139 |
+
"Human: hace mucho calooor\n",
|
140 |
+
"AI: Sí, el clima ha estado muy caliente últimamente. Dónde estabas?\n"
|
141 |
+
]
|
142 |
+
}
|
143 |
+
],
|
144 |
+
"source": [
|
145 |
+
"print(conversation.memory.buffer)"
|
146 |
+
]
|
147 |
+
},
|
148 |
+
{
|
149 |
+
"cell_type": "code",
|
150 |
+
"execution_count": null,
|
151 |
+
"metadata": {},
|
152 |
+
"outputs": [],
|
153 |
+
"source": []
|
154 |
+
}
|
155 |
+
],
|
156 |
+
"metadata": {
|
157 |
+
"kernelspec": {
|
158 |
+
"display_name": "base",
|
159 |
+
"language": "python",
|
160 |
+
"name": "python3"
|
161 |
+
},
|
162 |
+
"language_info": {
|
163 |
+
"codemirror_mode": {
|
164 |
+
"name": "ipython",
|
165 |
+
"version": 3
|
166 |
+
},
|
167 |
+
"file_extension": ".py",
|
168 |
+
"mimetype": "text/x-python",
|
169 |
+
"name": "python",
|
170 |
+
"nbconvert_exporter": "python",
|
171 |
+
"pygments_lexer": "ipython3",
|
172 |
+
"version": "3.9.7"
|
173 |
+
}
|
174 |
+
},
|
175 |
+
"nbformat": 4,
|
176 |
+
"nbformat_minor": 2
|
177 |
+
}
|