forked from pathwaycom/llm-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
271 lines (207 loc) · 7.87 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
Microservice for a context-aware alerting ChatGPT assistant.
This demo is very similar to `contextful` example with an additional real time alerting capability.
In the demo, alerts are sent to the Slack (you need `slack_alert_channel_id` and `slack_alert_token`),
you can either put these env variables in .env file under llm-app directory,
or create env variables in terminal (ie. export in bash)
If you don't have Slack, you can leave them empty and app will print the notifications to
standard output instead.
Upon starting, a REST API endpoint is opened by the app to serve queries about files inside
the input folder `data_dir`.
We can create notifications by sending query to API stating we want to be modified.
Alternatively, the provided Streamlit chat app can be used.
One example would be `Tell me and alert about start date of campaign for Magic Cola`
What happens next?
Each query text is first turned into a vector using OpenAI embedding service,
then relevant documentation pages are found using a Nearest Neighbor index computed
for documents in the corpus. A prompt is build from the relevant documentations pages
and sent to the OpenAI GPT3.5 chat service for processing and answering.
Once you run, Pathway looks for any changes in data sources, and efficiently detects changes
to the relevant documents. When a change is detected, the LLM is asked to answer the query
again, and if the new answer is sufficiently different, an alert is created.
Usage:
In the root of this repository run:
`poetry run ./run_examples.py alerts`
or, if all dependencies are managed manually rather than using poetry
You can either
`python examples/pipelines/alerts/app.py`
or
`python ./run_examples.py alert`
You can also run this example directly in the environment with llm_app installed.
To create alerts:
You can call the REST API:
curl --data '{"user": "user", "query": "How to connect to Kafka in Pathway?"}' http://localhost:8080/ | jq
Or start streamlit UI:
First go to examples/ui directory with `cd llm-app/examples/ui/`
run `streamlit run server.py`
"""
import os
import pathway as pw
from pathway.stdlib.ml.index import KNNIndex
from llm_app import send_slack_alerts
from llm_app.model_wrappers import OpenAIChatGPTModel, OpenAIEmbeddingModel
class DocumentInputSchema(pw.Schema):
doc: str
class QueryInputSchema(pw.Schema):
query: str
user: str
# Helper Functions
@pw.udf
def build_prompt(documents, query):
docs_str = "\n".join(
[f"Doc-({idx}) -> {doc}" for idx, doc in enumerate(documents[::-1])]
)
prompt = f"""Given a set of documents, answer user query. If answer is not in docs, say it can't be inferred.
Docs: {docs_str}
Query: '{query}'
Final Response:"""
return prompt
@pw.udf
def build_prompt_check_for_alert_request_and_extract_query(query: str) -> str:
prompt = f"""Evaluate the user's query and identify if there is a request for notifications on answer alterations:
User Query: '{query}'
Respond with 'Yes' if there is a request for alerts, and 'No' if not,
followed by the query without the alerting request part.
Examples:
"Tell me about windows in Pathway" => "No. Tell me about windows in Pathway"
"Tell me and alert about windows in Pathway" => "Yes. Tell me about windows in Pathway"
"""
return prompt
@pw.udf
def split_answer(answer: str) -> tuple[bool, str]:
alert_enabled = "yes" in answer[:3].lower()
true_query = answer[3:].strip(' ."')
return alert_enabled, true_query
def build_prompt_compare_answers(new: str, old: str) -> str:
prompt = f"""
Are the two following responses deviating?
Answer with Yes or No.
First response: "{old}"
Second response: "{new}"
"""
return prompt
def make_query_id(user, query) -> str:
return str(hash(query + user)) # + str(time.time())
@pw.udf
def construct_notification_message(query: str, response: str) -> str:
return f'New response for question "{query}":\n{response}'
@pw.udf
def construct_message(response, alert_flag, metainfo=None):
if alert_flag:
if metainfo:
response += "\n" + str(metainfo)
return response + "\n\n🔔 Activated"
return response
def decision_to_bool(decision: str) -> bool:
return "yes" in decision.lower()
def run(
*,
data_dir: str = os.environ.get(
"PATHWAY_DATA_DIR", "./examples/data/magic-cola/live/"
),
api_key: str = os.environ.get("OPENAI_API_KEY", ""),
host: str = "0.0.0.0",
port: int = 8080,
embedder_locator: str = "text-embedding-ada-002",
embedding_dimension: int = 1536,
model_locator: str = "gpt-3.5-turbo",
max_tokens: int = 400,
temperature: float = 0.0,
slack_alert_channel_id=os.environ.get("SLACK_ALERT_CHANNEL_ID", ""),
slack_alert_token=os.environ.get("SLACK_ALERT_TOKEN", ""),
**kwargs,
):
# Part I: Build index
embedder = OpenAIEmbeddingModel(api_key=api_key)
documents = pw.io.jsonlines.read(
data_dir,
schema=DocumentInputSchema,
mode="streaming_with_deletions",
autocommit_duration_ms=50,
)
enriched_documents = documents + documents.select(
data=embedder.apply(text=pw.this.doc, locator=embedder_locator)
)
index = KNNIndex(
enriched_documents.data, enriched_documents, n_dimensions=embedding_dimension
)
# Part II: receive queries, detect intent and prepare cleaned query
query, response_writer = pw.io.http.rest_connector(
host=host,
port=port,
schema=QueryInputSchema,
autocommit_duration_ms=50,
delete_completed_queries=False,
)
model = OpenAIChatGPTModel(api_key=api_key)
query += query.select(
prompt=build_prompt_check_for_alert_request_and_extract_query(query.query)
)
query += query.select(
tupled=split_answer(
model.apply(
pw.this.prompt,
locator=model_locator,
temperature=temperature,
max_tokens=100,
)
),
)
query = query.select(
pw.this.user,
alert_enabled=pw.this.tupled[0],
query=pw.this.tupled[1],
)
query += query.select(
data=embedder.apply(text=pw.this.query, locator=embedder_locator),
query_id=pw.apply(make_query_id, pw.this.user, pw.this.query),
)
# Part III: respond to queries
query_context = query + index.get_nearest_items(query.data, k=3).select(
documents_list=pw.this.doc
).with_universe_of(query)
prompt = query_context.select(
pw.this.query_id,
pw.this.query,
pw.this.alert_enabled,
prompt=build_prompt(pw.this.documents_list, pw.this.query),
)
responses = prompt.select(
pw.this.query_id,
pw.this.query,
pw.this.alert_enabled,
response=model.apply(
pw.this.prompt,
locator=model_locator,
temperature=temperature,
max_tokens=max_tokens,
),
)
output = responses.select(
result=construct_message(pw.this.response, pw.this.alert_enabled)
)
response_writer(output)
# Part IV: send alerts about responses which changed significantly.
responses = responses.filter(pw.this.alert_enabled)
def acceptor(new: str, old: str) -> bool:
if new == old:
return False
decision = model(
build_prompt_compare_answers(new, old),
locator=model_locator,
max_tokens=20,
)
return decision_to_bool(decision)
deduplicated_responses = pw.stateful.deduplicate(
responses,
col=responses.response,
acceptor=acceptor,
instance=responses.query_id,
)
alerts = deduplicated_responses.select(
message=construct_notification_message(pw.this.query, pw.this.response)
)
send_slack_alerts(alerts.message, slack_alert_channel_id, slack_alert_token)
pw.run()
if __name__ == "__main__":
run()