Skip to content

Commit 452cb69

Browse files
committedMay 16, 2023
Created using Colaboratory
1 parent c2d2bff commit 452cb69

File tree

1 file changed

+247
-0
lines changed

1 file changed

+247
-0
lines changed
 

‎lc_milvus_test.ipynb

+247
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"id": "view-in-github",
7+
"colab_type": "text"
8+
},
9+
"source": [
10+
"<a href=\"https://colab.research.google.com/github/jonbaer/googlecolab/blob/master/lc_milvus_test.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"source": [
16+
"Uncomment the pip block to install the necessary libraries\n",
17+
"\n",
18+
"If you don't already have it the .txt file can be found [here](https://github.com/hwchase17/langchain/blob/master/docs/modules/state_of_the_union.txt)"
19+
],
20+
"metadata": {
21+
"id": "1WxCsSVK7Jb1"
22+
}
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": null,
27+
"metadata": {
28+
"id": "gP7MbMLL7H6l"
29+
},
30+
"outputs": [],
31+
"source": [
32+
"# !pip install langchain openai milvus pymilvus python-dotenv tiktoken"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {
39+
"id": "d3iHzMMR7H6m"
40+
},
41+
"outputs": [],
42+
"source": [
43+
"import os\n",
44+
"from dotenv import load_dotenv\n",
45+
"import openai\n",
46+
"load_dotenv()\n",
47+
"openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n",
48+
"# openai.api_key"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": null,
54+
"metadata": {
55+
"id": "uJMMYyPY7H6n"
56+
},
57+
"outputs": [],
58+
"source": [
59+
"from langchain.llms import OpenAI\n",
60+
"davinci = OpenAI(model_name=\"text-davinci-003\")"
61+
]
62+
},
63+
{
64+
"cell_type": "code",
65+
"execution_count": null,
66+
"metadata": {
67+
"id": "xz6xpd0g7H6n"
68+
},
69+
"outputs": [],
70+
"source": [
71+
"from milvus import default_server"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": null,
77+
"metadata": {
78+
"id": "DbVG7eHs7H6n",
79+
"outputId": "1e05df00-744c-4d3a-dbfe-d36cff185990"
80+
},
81+
"outputs": [
82+
{
83+
"name": "stdout",
84+
"output_type": "stream",
85+
"text": [
86+
"\n",
87+
"\n",
88+
" __ _________ _ ____ ______\n",
89+
" / |/ / _/ /| | / / / / / __/\n",
90+
" / /|_/ // // /_| |/ / /_/ /\\ \\\n",
91+
" /_/ /_/___/____/___/\\____/___/ {Lite}\n",
92+
"\n",
93+
" Welcome to use Milvus!\n",
94+
"\n",
95+
" Version: v2.2.8-lite\n",
96+
" Process: 15063\n",
97+
" Started: 2023-05-10 20:25:54\n",
98+
" Config: /Users/yujiantang/.milvus.io/milvus-server/2.2.8/configs/milvus.yaml\n",
99+
" Logs: /Users/yujiantang/.milvus.io/milvus-server/2.2.8/logs\n",
100+
"\n",
101+
" Ctrl+C to exit ...\n"
102+
]
103+
}
104+
],
105+
"source": [
106+
"default_server.start()"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"metadata": {
113+
"id": "OQYgH1o57H6o"
114+
},
115+
"outputs": [],
116+
"source": [
117+
"from langchain.embeddings.openai import OpenAIEmbeddings\n",
118+
"from langchain.text_splitter import CharacterTextSplitter\n",
119+
"from langchain.vectorstores import Milvus"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"metadata": {
126+
"id": "MhmV9z0Y7H6o"
127+
},
128+
"outputs": [],
129+
"source": [
130+
"from langchain.document_loaders import TextLoader\n",
131+
"loader = TextLoader('./state_of_the_union.txt')\n",
132+
"documents = loader.load()\n",
133+
"text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
134+
"docs = text_splitter.split_documents(documents)\n",
135+
"\n",
136+
"embeddings = OpenAIEmbeddings()"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": null,
142+
"metadata": {
143+
"id": "zQWNtaRZ7H6o"
144+
},
145+
"outputs": [],
146+
"source": [
147+
"vector_db = Milvus.from_documents(\n",
148+
" docs,\n",
149+
" embeddings,\n",
150+
" connection_args={\"host\": \"127.0.0.1\", \"port\": default_server.listen_port},\n",
151+
")"
152+
]
153+
},
154+
{
155+
"cell_type": "code",
156+
"execution_count": null,
157+
"metadata": {
158+
"id": "5TmEg5Uw7H6o"
159+
},
160+
"outputs": [],
161+
"source": [
162+
"from langchain.chains import RetrievalQA"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": null,
168+
"metadata": {
169+
"id": "NJqD6T0l7H6o"
170+
},
171+
"outputs": [],
172+
"source": [
173+
"qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type=\"stuff\", retriever=vector_db.as_retriever())"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": null,
179+
"metadata": {
180+
"id": "uCLs1eVI7H6p",
181+
"outputId": "78be5739-b4f1-4554-fe9e-994b434f4e04"
182+
},
183+
"outputs": [
184+
{
185+
"data": {
186+
"text/plain": [
187+
"\" The president said that Ketanji Brown Jackson is one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, from a family of public school educators and police officers, a consensus builder, and has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans.\""
188+
]
189+
},
190+
"execution_count": 17,
191+
"metadata": {},
192+
"output_type": "execute_result"
193+
}
194+
],
195+
"source": [
196+
"query = \"What did the president say about Ketanji Brown Jackson\"\n",
197+
"qa.run(query)"
198+
]
199+
},
200+
{
201+
"cell_type": "code",
202+
"execution_count": null,
203+
"metadata": {
204+
"id": "A8kI4j0M7H6p"
205+
},
206+
"outputs": [],
207+
"source": [
208+
"default_server.stop()"
209+
]
210+
},
211+
{
212+
"cell_type": "code",
213+
"execution_count": null,
214+
"metadata": {
215+
"id": "ILSzh4xM7H6p"
216+
},
217+
"outputs": [],
218+
"source": []
219+
}
220+
],
221+
"metadata": {
222+
"kernelspec": {
223+
"display_name": "hw_milvus",
224+
"language": "python",
225+
"name": "python3"
226+
},
227+
"language_info": {
228+
"codemirror_mode": {
229+
"name": "ipython",
230+
"version": 3
231+
},
232+
"file_extension": ".py",
233+
"mimetype": "text/x-python",
234+
"name": "python",
235+
"nbconvert_exporter": "python",
236+
"pygments_lexer": "ipython3",
237+
"version": "3.10.11"
238+
},
239+
"orig_nbformat": 4,
240+
"colab": {
241+
"provenance": [],
242+
"include_colab_link": true
243+
}
244+
},
245+
"nbformat": 4,
246+
"nbformat_minor": 0
247+
}

0 commit comments

Comments
 (0)