Files
wehub-resource-sync 86db9aae8e
Documentation / build (push) Waiting to run
Documentation / deploy (push) Blocked by required conditions
chore: import upstream snapshot with attribution
2026-07-13 13:34:55 +08:00

199 lines
6.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "6sr3Gk8hNDaF"
},
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "eQk5Dv3jyheg"
},
"source": [
"# If you are using Colab for free, we highly recommend you activate the T4 GPU\n",
"# hardware accelerator. Our models are designed to run with at least 16GB\n",
"# of RAM, activating T4 will grant the notebook 16GB of GDDR6 RAM as opposed\n",
"# to the ~13GB Colab gives automatically.\n",
"# To activate T4:\n",
"# 1. click on the \"Runtime\" tab\n",
"# 2. click on \"Change runtime type\"\n",
"# 3. select T4 GPU under Hardware Accelerator\n",
"# NOTE: there is a weekly usage limit on using T4 for free"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "td7dVt4fD_j6"
},
"outputs": [],
"source": [
"!pip install llmware"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "IY8ysw9FElZR"
},
"outputs": [],
"source": [
"!pip install --upgrade huggingface_hub\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "AeR-LvVXD27i"
},
"outputs": [],
"source": [
"\n",
"\"\"\" This example shows a complex multi-part research analysis. In this example, we will:\n",
"\n",
" 1. Build a \"research\" library.\n",
" 2. Query the research library to identify topics of interest.\n",
" 3. Create an agent with several analytical tools: sentiment, emotions, topic, entities analysis\n",
" 4. Pass the results of our query to the agent to conduct multifaceted analysis.\n",
" 5. Apply a top-level filter ('sentiment') on the results from the query\n",
" 6. For any of the passages with negative sentiment, we will run a follow-up set of analyses.\n",
" 7. Finally, we will assemble the follow-up analysis into a list of detailed reports.\n",
"\"\"\"\n",
"\n",
"import os\n",
"import shutil\n",
"\n",
"from llmware.agents import LLMfx\n",
"from llmware.library import Library\n",
"from llmware.retrieval import Query\n",
"from llmware.configs import LLMWareConfig\n",
"from llmware.setup import Setup\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "qG3lAj2nM372"
},
"outputs": [],
"source": [
"def multistep_analysis():\n",
"\n",
" \"\"\" In this example, our objective is to research Microsoft history and rivalry in the 1980s with IBM. \"\"\"\n",
"\n",
" # step 1 - assemble source documents and create library\n",
"\n",
" print(\"update: Starting example - agent-multistep-analysis\")\n",
"\n",
" # note: lines 38-49 attempt to automatically pull sample document into local path\n",
" # depending upon permissions in your environment, you may need to set up directly\n",
" # if you pull down the samples files with Setup().load_sample_files(), in the Books folder,\n",
" # you will find the source: \"Bill-Gates-Biography.pdf\"\n",
" # if you have pulled sample documents in the past, then to update to latest: set over_write=True\n",
"\n",
" print(\"update: Loading sample files\")\n",
"\n",
" sample_files_path = Setup().load_sample_files(over_write=False)\n",
" bill_gates_bio = \"Bill-Gates-Biography.pdf\"\n",
" path_to_bill_gates_bio = os.path.join(sample_files_path, \"Books\", bill_gates_bio)\n",
"\n",
" microsoft_folder = os.path.join(LLMWareConfig().get_tmp_path(), \"example_microsoft\")\n",
"\n",
" print(\"update: attempting to create source input folder at path: \", microsoft_folder)\n",
"\n",
" if not os.path.exists(microsoft_folder):\n",
" os.mkdir(microsoft_folder)\n",
" os.chmod(microsoft_folder, 0o777)\n",
" shutil.copy(path_to_bill_gates_bio,os.path.join(microsoft_folder, bill_gates_bio))\n",
"\n",
" # create library\n",
" print(\"update: creating library and parsing source document\")\n",
"\n",
" LLMWareConfig().set_active_db(\"sqlite\")\n",
" my_lib = Library().create_new_library(\"microsoft_history_0210_1\")\n",
" my_lib.add_files(microsoft_folder)\n",
"\n",
" # run our first query - \"ibm\"\n",
" query = \"ibm\"\n",
" search_results = Query(my_lib).text_query(query)\n",
" print(f\"update: executing query to filter to key passages - {query} - results found - {len(search_results)}\")\n",
"\n",
" # create an agent and load several tools that we will be using\n",
" agent = LLMfx()\n",
" agent.load_tool_list([\"sentiment\", \"emotions\", \"topic\", \"tags\", \"ner\", \"answer\"])\n",
"\n",
" # load the search results into the agent's work queue\n",
" agent.load_work(search_results)\n",
"\n",
" while True:\n",
"\n",
" agent.sentiment()\n",
"\n",
" if not agent.increment_work_iteration():\n",
" break\n",
"\n",
" # analyze sections where the sentiment on ibm was negative\n",
" follow_up_list = agent.follow_up_list(key=\"sentiment\", value=\"negative\")\n",
"\n",
" for job_index in follow_up_list:\n",
"\n",
" # follow-up 'deep dive' on selected text that references ibm negatively\n",
" agent.set_work_iteration(job_index)\n",
" agent.exec_multitool_function_call([\"tags\", \"emotions\", \"topics\", \"ner\"])\n",
" agent.answer(\"What is a brief summary?\", key=\"summary\")\n",
"\n",
" my_report = agent.show_report(follow_up_list)\n",
"\n",
" activity_summary = agent.activity_summary()\n",
"\n",
" for entries in my_report:\n",
" print(\"my report entries: \", entries)\n",
"\n",
" return my_report"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "NDKDVuEoM9rL"
},
"outputs": [],
"source": [
"\n",
"if __name__ == \"__main__\":\n",
"\n",
" multistep_analysis()\n",
"\n"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"gpuType": "T4",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}