11{
2- "nbformat" : 4 ,
3- "nbformat_minor" : 0 ,
4- "metadata" : {
5- "colab" : {
6- "provenance" : [],
7- "gpuType" : " T4"
8- },
9- "kernelspec" : {
10- "name" : " python3" ,
11- "display_name" : " Python 3"
12- },
13- "language_info" : {
14- "name" : " python"
15- },
16- "accelerator" : " GPU"
2+ "cells" : [
3+ {
4+ "cell_type" : " markdown" ,
5+ "metadata" : {
6+ "id" : " 6sr3Gk8hNDaF"
7+ },
8+ "source" : []
179 },
18- "cells" : [
19- {
20- "cell_type" : " markdown" ,
21- "source" : [],
22- "metadata" : {
23- "id" : " 6sr3Gk8hNDaF"
24- }
25- },
26- {
27- "cell_type" : " markdown" ,
28- "source" : [
29- " # If you are using Colab for free, we highly recommend you activate the T4 GPU\n " ,
30- " # hardware accelerator. Our models are designed to run with at least 16GB\n " ,
31- " # of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed\n " ,
32- " # to the ~13GB Colab gives automatically.\n " ,
33- " # To activate T4:\n " ,
34- " # 1. click on the \" Runtime\" tab\n " ,
35- " # 2. click on \" Change runtime type\"\n " ,
36- " # 3. select T4 GPU under Hardware Accelerator\n " ,
37- " # NOTE: there is a weekly usage limit on using T4 for free"
38- ],
39- "metadata" : {
40- "id" : " eQk5Dv3jyheg"
41- }
42- },
43- {
44- "cell_type" : " code" ,
45- "source" : [
46- " !pip install llmware"
47- ],
48- "metadata" : {
49- "id" : " td7dVt4fD_j6"
50- },
51- "execution_count" : null ,
52- "outputs" : []
53- },
54- {
55- "cell_type" : " code" ,
56- "source" : [
57- " !pip install --upgrade huggingface_hub\n "
58- ],
59- "metadata" : {
60- "id" : " IY8ysw9FElZR"
61- },
62- "execution_count" : null ,
63- "outputs" : []
64- },
65- {
66- "cell_type" : " code" ,
67- "execution_count" : 8 ,
68- "metadata" : {
69- "id" : " AeR-LvVXD27i"
70- },
71- "outputs" : [],
72- "source" : [
73- " \n " ,
74- " \"\"\" This example shows a complex multi-part research analysis. In this example, we will:\n " ,
75- " \n " ,
76- " 1. Build a \" research\" library.\n " ,
77- " 2. Query the research library to identify topics of interest.\n " ,
78- " 3. Create an agent with several analytical tools: sentiment, emotions, topic, entities analysis\n " ,
79- " 4. Pass the results of our query to the agent to conduct multifaceted analysis.\n " ,
80- " 5. Apply a top-level filter ('sentiment') on the results from the query\n " ,
81- " 6. For any of the passages with negative sentiment, we will run a follow-up set of analyses.\n " ,
82- " 7. Finally, we will assemble the follow-up analysis into a list of detailed reports.\n " ,
83- " \"\"\"\n " ,
84- " \n " ,
85- " import os\n " ,
86- " import shutil\n " ,
87- " \n " ,
88- " from llmware.agents import LLMfx\n " ,
89- " from llmware.library import Library\n " ,
90- " from llmware.retrieval import Query\n " ,
91- " from llmware.configs import LLMWareConfig\n " ,
92- " from llmware.setup import Setup\n " ,
93- " \n " ,
94- " \n " ,
95- " \n " ,
96- " \n "
97- ]
98- },
99- {
100- "cell_type" : " code" ,
101- "source" : [
102- " def multistep_analysis():\n " ,
103- " \n " ,
104- " \"\"\" In this example, our objective is to research Microsoft history and rivalry in the 1980s with IBM. \"\"\"\n " ,
105- " \n " ,
106- " # step 1 - assemble source documents and create library\n " ,
107- " \n " ,
108- " print(\" update: Starting example - agent-multistep-analysis\" )\n " ,
109- " \n " ,
110- " # note: lines 38-49 attempt to automatically pull sample document into local path\n " ,
111- " # depending upon permissions in your environment, you may need to set up directly\n " ,
112- " # if you pull down the samples files with Setup().load_sample_files(), in the Books folder,\n " ,
113- " # you will find the source: \" Bill-Gates-Biography.pdf\"\n " ,
114- " # if you have pulled sample documents in the past, then to update to latest: set over_write=True\n " ,
115- " \n " ,
116- " print(\" update: Loading sample files\" )\n " ,
117- " \n " ,
118- " sample_files_path = Setup().load_sample_files(over_write=False)\n " ,
119- " bill_gates_bio = \" Bill-Gates-Biography.pdf\"\n " ,
120- " path_to_bill_gates_bio = os.path.join(sample_files_path, \" Books\" , bill_gates_bio)\n " ,
121- " \n " ,
122- " microsoft_folder = os.path.join(LLMWareConfig().get_tmp_path(), \" example_microsoft\" )\n " ,
123- " \n " ,
124- " print(\" update: attempting to create source input folder at path: \" , microsoft_folder)\n " ,
125- " \n " ,
126- " if not os.path.exists(microsoft_folder):\n " ,
127- " os.mkdir(microsoft_folder)\n " ,
128- " os.chmod(microsoft_folder, 0o777)\n " ,
129- " shutil.copy(path_to_bill_gates_bio,os.path.join(microsoft_folder, bill_gates_bio))\n " ,
130- " \n " ,
131- " # create library\n " ,
132- " print(\" update: creating library and parsing source document\" )\n " ,
133- " \n " ,
134- " LLMWareConfig().set_active_db(\" sqlite\" )\n " ,
135- " my_lib = Library().create_new_library(\" microsoft_history_0210_1\" )\n " ,
136- " my_lib.add_files(microsoft_folder)\n " ,
137- " \n " ,
138- " # run our first query - \" ibm\"\n " ,
139- " query = \" ibm\"\n " ,
140- " search_results = Query(my_lib).text_query(query)\n " ,
141- " print(f\" update: executing query to filter to key passages - {query} - results found - {len(search_results)}\" )\n " ,
142- " \n " ,
143- " # create an agent and load several tools that we will be using\n " ,
144- " agent = LLMfx()\n " ,
145- " agent.load_tool_list([\" sentiment\" , \" emotions\" , \" topic\" , \" tags\" , \" ner\" , \" answer\" ])\n " ,
146- " \n " ,
147- " # load the search results into the agent's work queue\n " ,
148- " agent.load_work(search_results)\n " ,
149- " \n " ,
150- " while True:\n " ,
151- " \n " ,
152- " agent.sentiment()\n " ,
153- " \n " ,
154- " if not agent.increment_work_iteration():\n " ,
155- " break\n " ,
156- " \n " ,
157- " # analyze sections where the sentiment on ibm was negative\n " ,
158- " follow_up_list = agent.follow_up_list(key=\" sentiment\" , value=\" negative\" )\n " ,
159- " \n " ,
160- " for job_index in follow_up_list:\n " ,
161- " \n " ,
162- " # follow-up 'deep dive' on selected text that references ibm negatively\n " ,
163- " agent.set_work_iteration(job_index)\n " ,
164- " agent.exec_multitool_function_call([\" tags\" , \" emotions\" , \" topics\" , \" ner\" ])\n " ,
165- " agent.answer(\" What is a brief summary?\" , key=\" summary\" )\n " ,
166- " \n " ,
167- " my_report = agent.show_report(follow_up_list)\n " ,
168- " \n " ,
169- " activity_summary = agent.activity_summary()\n " ,
170- " \n " ,
171- " for entries in my_report:\n " ,
172- " print(\" my report entries: \" , entries)\n " ,
173- " \n " ,
174- " return my_report"
175- ],
176- "metadata" : {
177- "id" : " qG3lAj2nM372"
178- },
179- "execution_count" : 9 ,
180- "outputs" : []
181- },
182- {
183- "cell_type" : " code" ,
184- "source" : [
185- " \n " ,
186- " if __name__ == \" __main__\" :\n " ,
187- " \n " ,
188- " multistep_analysis()\n " ,
189- " \n "
190- ],
191- "metadata" : {
192- "id" : " NDKDVuEoM9rL"
193- },
194- "execution_count" : null ,
195- "outputs" : []
196- }
197- ]
198- }
10+ {
11+ "cell_type" : " markdown" ,
12+ "metadata" : {
13+ "id" : " eQk5Dv3jyheg"
14+ },
15+ "source" : [
16+ " # If you are using Colab for free, we highly recommend you activate the T4 GPU\n " ,
17+ " # hardware accelerator. Our models are designed to run with at least 16GB\n " ,
18+ " # of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed\n " ,
19+ " # to the ~13GB Colab gives automatically.\n " ,
20+ " # To activate T4:\n " ,
21+ " # 1. click on the \" Runtime\" tab\n " ,
22+ " # 2. click on \" Change runtime type\"\n " ,
23+ " # 3. select T4 GPU under Hardware Accelerator\n " ,
24+ " # NOTE: there is a weekly usage limit on using T4 for free"
25+ ]
26+ },
27+ {
28+ "cell_type" : " code" ,
29+ "execution_count" : null ,
30+ "metadata" : {
31+ "id" : " td7dVt4fD_j6"
32+ },
33+ "outputs" : [],
34+ "source" : [
35+ " !pip install llmware"
36+ ]
37+ },
38+ {
39+ "cell_type" : " code" ,
40+ "execution_count" : null ,
41+ "metadata" : {
42+ "id" : " IY8ysw9FElZR"
43+ },
44+ "outputs" : [],
45+ "source" : [
46+ " !pip install --upgrade huggingface_hub\n "
47+ ]
48+ },
49+ {
50+ "cell_type" : " code" ,
51+ "execution_count" : 8 ,
52+ "metadata" : {
53+ "id" : " AeR-LvVXD27i"
54+ },
55+ "outputs" : [],
56+ "source" : [
57+ " \n " ,
58+ " \"\"\" This example shows a complex multi-part research analysis. In this example, we will:\n " ,
59+ " \n " ,
60+ " 1. Build a \" research\" library.\n " ,
61+ " 2. Query the research library to identify topics of interest.\n " ,
62+ " 3. Create an agent with several analytical tools: sentiment, emotions, topic, entities analysis\n " ,
63+ " 4. Pass the results of our query to the agent to conduct multifaceted analysis.\n " ,
64+ " 5. Apply a top-level filter ('sentiment') on the results from the query\n " ,
65+ " 6. For any of the passages with negative sentiment, we will run a follow-up set of analyses.\n " ,
66+ " 7. Finally, we will assemble the follow-up analysis into a list of detailed reports.\n " ,
67+ " \"\"\"\n " ,
68+ " \n " ,
69+ " import os\n " ,
70+ " import shutil\n " ,
71+ " \n " ,
72+ " from llmware.agents import LLMfx\n " ,
73+ " from llmware.library import Library\n " ,
74+ " from llmware.retrieval import Query\n " ,
75+ " from llmware.configs import LLMWareConfig\n " ,
76+ " from llmware.setup import Setup\n " ,
77+ " \n " ,
78+ " \n " ,
79+ " \n " ,
80+ " \n "
81+ ]
82+ },
83+ {
84+ "cell_type" : " code" ,
85+ "execution_count" : 9 ,
86+ "metadata" : {
87+ "id" : " qG3lAj2nM372"
88+ },
89+ "outputs" : [],
90+ "source" : [
91+ " def multistep_analysis():\n " ,
92+ " \n " ,
93+ " \"\"\" In this example, our objective is to research Microsoft history and rivalry in the 1980s with IBM. \"\"\"\n " ,
94+ " \n " ,
95+ " # step 1 - assemble source documents and create library\n " ,
96+ " \n " ,
97+ " print(\" update: Starting example - agent-multistep-analysis\" )\n " ,
98+ " \n " ,
99+ " # note: lines 38-49 attempt to automatically pull sample document into local path\n " ,
100+ " # depending upon permissions in your environment, you may need to set up directly\n " ,
101+ " # if you pull down the samples files with Setup().load_sample_files(), in the Books folder,\n " ,
102+ " # you will find the source: \" Bill-Gates-Biography.pdf\"\n " ,
103+ " # if you have pulled sample documents in the past, then to update to latest: set over_write=True\n " ,
104+ " \n " ,
105+ " print(\" update: Loading sample files\" )\n " ,
106+ " \n " ,
107+ " sample_files_path = Setup().load_sample_files(over_write=False)\n " ,
108+ " bill_gates_bio = \" Bill-Gates-Biography.pdf\"\n " ,
109+ " path_to_bill_gates_bio = os.path.join(sample_files_path, \" Books\" , bill_gates_bio)\n " ,
110+ " \n " ,
111+ " microsoft_folder = os.path.join(LLMWareConfig().get_tmp_path(), \" example_microsoft\" )\n " ,
112+ " \n " ,
113+ " print(\" update: attempting to create source input folder at path: \" , microsoft_folder)\n " ,
114+ " \n " ,
115+ " if not os.path.exists(microsoft_folder):\n " ,
116+ " os.mkdir(microsoft_folder)\n " ,
117+ " os.chmod(microsoft_folder, 0o777)\n " ,
118+ " shutil.copy(path_to_bill_gates_bio,os.path.join(microsoft_folder, bill_gates_bio))\n " ,
119+ " \n " ,
120+ " # create library\n " ,
121+ " print(\" update: creating library and parsing source document\" )\n " ,
122+ " \n " ,
123+ " LLMWareConfig().set_active_db(\" sqlite\" )\n " ,
124+ " my_lib = Library().create_new_library(\" microsoft_history_0210_1\" )\n " ,
125+ " my_lib.add_files(microsoft_folder)\n " ,
126+ " \n " ,
127+ " # run our first query - \" ibm\"\n " ,
128+ " query = \" ibm\"\n " ,
129+ " search_results = Query(my_lib).text_query(query)\n " ,
130+ " print(f\" update: executing query to filter to key passages - {query} - results found - {len(search_results)}\" )\n " ,
131+ " \n " ,
132+ " # create an agent and load several tools that we will be using\n " ,
133+ " agent = LLMfx()\n " ,
134+ " agent.load_tool_list([\" sentiment\" , \" emotions\" , \" topic\" , \" tags\" , \" ner\" , \" answer\" ])\n " ,
135+ " \n " ,
136+ " # load the search results into the agent's work queue\n " ,
137+ " agent.load_work(search_results)\n " ,
138+ " \n " ,
139+ " while True:\n " ,
140+ " \n " ,
141+ " agent.sentiment()\n " ,
142+ " \n " ,
143+ " if not agent.increment_work_iteration():\n " ,
144+ " break\n " ,
145+ " \n " ,
146+ " # analyze sections where the sentiment on ibm was negative\n " ,
147+ " follow_up_list = agent.follow_up_list(key=\" sentiment\" , value=\" negative\" )\n " ,
148+ " \n " ,
149+ " for job_index in follow_up_list:\n " ,
150+ " \n " ,
151+ " # follow-up 'deep dive' on selected text that references ibm negatively\n " ,
152+ " agent.set_work_iteration(job_index)\n " ,
153+ " agent.exec_multitool_function_call([\" tags\" , \" emotions\" , \" topics\" , \" ner\" ])\n " ,
154+ " agent.answer(\" What is a brief summary?\" , key=\" summary\" )\n " ,
155+ " \n " ,
156+ " my_report = agent.show_report(follow_up_list)\n " ,
157+ " \n " ,
158+ " activity_summary = agent.activity_summary()\n " ,
159+ " \n " ,
160+ " for entries in my_report:\n " ,
161+ " print(\" my report entries: \" , entries)\n " ,
162+ " \n " ,
163+ " return my_report"
164+ ]
165+ },
166+ {
167+ "cell_type" : " code" ,
168+ "execution_count" : null ,
169+ "metadata" : {
170+ "id" : " NDKDVuEoM9rL"
171+ },
172+ "outputs" : [],
173+ "source" : [
174+ " \n " ,
175+ " if __name__ == \" __main__\" :\n " ,
176+ " \n " ,
177+ " multistep_analysis()\n " ,
178+ " \n "
179+ ]
180+ }
181+ ],
182+ "metadata" : {
183+ "accelerator" : " GPU" ,
184+ "colab" : {
185+ "gpuType" : " T4" ,
186+ "provenance" : []
187+ },
188+ "kernelspec" : {
189+ "display_name" : " Python 3" ,
190+ "name" : " python3"
191+ },
192+ "language_info" : {
193+ "name" : " python"
194+ }
195+ },
196+ "nbformat" : 4 ,
197+ "nbformat_minor" : 0
198+ }
0 commit comments