llama-index

Форк
0
/
network_query_engine.ipynb 
184 строки · 5.3 Кб
1
{
2
 "cells": [
3
  {
4
   "cell_type": "markdown",
5
   "id": "27beb3c1-4d05-4645-85e5-60c28bdaaae2",
6
   "metadata": {},
7
   "source": [
8
    "# NetworkQueryEngine Demo"
9
   ]
10
  },
11
  {
12
   "cell_type": "code",
13
   "execution_count": null,
14
   "id": "ff743622-6887-4121-8049-44110b366f06",
15
   "metadata": {},
16
   "outputs": [],
17
   "source": [
18
    "import os\n",
19
    "from llama_index.networks import (\n",
20
    "    NetworkQueryEngine,\n",
21
    "    ContributorClient,\n",
22
    ")\n",
23
    "from llama_index.llms.openai import OpenAI"
24
   ]
25
  },
26
  {
27
   "cell_type": "code",
28
   "execution_count": null,
29
   "id": "ee0673fd-2bcc-4a45-8caa-a2714377ac5c",
30
   "metadata": {},
31
   "outputs": [],
32
   "source": [
33
    "import nest_asyncio\n",
34
    "\n",
35
    "nest_asyncio.apply()"
36
   ]
37
  },
38
  {
39
   "cell_type": "code",
40
   "execution_count": null,
41
   "id": "c1c19958-cb5d-49b4-af8a-76265f108286",
42
   "metadata": {},
43
   "outputs": [],
44
   "source": [
45
    "# setup ContributorClients to connect to ContributorServices\n",
46
    "contributors = [\n",
47
    "    ContributorClient.from_config_file(\n",
48
    "        env_file=f\"./client-env-files/.env.contributor_{ix}.client\"\n",
49
    "    )\n",
50
    "    for ix in range(1, 4)\n",
51
    "]"
52
   ]
53
  },
54
  {
55
   "cell_type": "code",
56
   "execution_count": null,
57
   "id": "ce075783-b9bc-4dbb-b982-e6c2c47a8a40",
58
   "metadata": {},
59
   "outputs": [],
60
   "source": [
61
    "# build NetworkRAG\n",
62
    "api_key = os.environ.get(\"OPENAI_API_KEY\")\n",
63
    "llm = OpenAI(api_key=api_key)\n",
64
    "network_query_engine = NetworkQueryEngine.from_args(contributors=contributors, llm=llm)"
65
   ]
66
  },
67
  {
68
   "cell_type": "code",
69
   "execution_count": null,
70
   "id": "05350567-a369-4274-9574-cecb194f1d7d",
71
   "metadata": {},
72
   "outputs": [],
73
   "source": [
74
    "# knowledge from contributor 1\n",
75
    "response = await network_query_engine.aquery(\"Who is Paul?\")"
76
   ]
77
  },
78
  {
79
   "cell_type": "code",
80
   "execution_count": null,
81
   "id": "9ccb68f2-2c65-4074-bb7f-5c411ab8ef5c",
82
   "metadata": {},
83
   "outputs": [
84
    {
85
     "data": {
86
      "text/plain": [
87
       "'Paul is a person who has been involved in various ventures such as painting, starting companies like Viaweb and Y Combinator, and creating a new dialect of Lisp called Arc. He also began publishing essays online, realizing the potential of the web as a platform for reaching a wider audience.'"
88
      ]
89
     },
90
     "execution_count": null,
91
     "metadata": {},
92
     "output_type": "execute_result"
93
    }
94
   ],
95
   "source": [
96
    "response.response"
97
   ]
98
  },
99
  {
100
   "cell_type": "code",
101
   "execution_count": null,
102
   "id": "7f28db53-a96d-431a-aee9-bf16b236f850",
103
   "metadata": {},
104
   "outputs": [],
105
   "source": [
106
    "# knowledge from contributor 2\n",
107
    "response = await network_query_engine.aquery(\"What is DoRA?\")"
108
   ]
109
  },
110
  {
111
   "cell_type": "code",
112
   "execution_count": null,
113
   "id": "03144a51-21ec-4667-858e-81baaea97507",
114
   "metadata": {},
115
   "outputs": [
116
    {
117
     "data": {
118
      "text/plain": [
119
       "'DoRA is a method that enhances the learning capacity of LoRA by introducing incremental directional updates and adapting to different LoRA variants. It can be combined with other LoRA variants like VeRA, which reduces trainable parameters significantly while maintaining accuracy. DoRA consistently outperforms LoRA across various rank settings for commonsense reasoning tasks, demonstrating its ability to achieve better accuracy with fewer trainable parameters.'"
120
      ]
121
     },
122
     "execution_count": null,
123
     "metadata": {},
124
     "output_type": "execute_result"
125
    }
126
   ],
127
   "source": [
128
    "response.response"
129
   ]
130
  },
131
  {
132
   "cell_type": "code",
133
   "execution_count": null,
134
   "id": "bd5849c6-6ebf-4f69-bbd2-d874e2ee80d9",
135
   "metadata": {},
136
   "outputs": [],
137
   "source": [
138
    "# knowledge from contributor 3\n",
139
    "response = await network_query_engine.aquery(\"Summarize the history of New York City\")"
140
   ]
141
  },
142
  {
143
   "cell_type": "code",
144
   "execution_count": null,
145
   "id": "de285ade-451f-407d-a308-685da6b5fcb4",
146
   "metadata": {},
147
   "outputs": [
148
    {
149
     "data": {
150
      "text/plain": [
151
       "\"New York City's history began with European exploration in the early 16th century, leading to Dutch settlement and the founding of New Amsterdam in the early 17th century. It played a crucial role in the American Revolution and briefly served as the national capital. The city's strategic location as a major seaport and transportation hub, along with waves of European immigrants in the mid-19th century, fueled its industrial growth. The consolidation of the five boroughs in 1898 marked a significant milestone. Over the years, New York City has evolved into a global center for finance, culture, and commerce, known for its diverse population, iconic landmarks, and significant role in historical events.\""
152
      ]
153
     },
154
     "execution_count": null,
155
     "metadata": {},
156
     "output_type": "execute_result"
157
    }
158
   ],
159
   "source": [
160
    "response.response"
161
   ]
162
  }
163
 ],
164
 "metadata": {
165
  "kernelspec": {
166
   "display_name": "networks-demo",
167
   "language": "python",
168
   "name": "networks-demo"
169
  },
170
  "language_info": {
171
   "codemirror_mode": {
172
    "name": "ipython",
173
    "version": 3
174
   },
175
   "file_extension": ".py",
176
   "mimetype": "text/x-python",
177
   "name": "python",
178
   "nbconvert_exporter": "python",
179
   "pygments_lexer": "ipython3"
180
  }
181
 },
182
 "nbformat": 4,
183
 "nbformat_minor": 5
184
}
185

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.