-
Notifications
You must be signed in to change notification settings - Fork 0
/
kopie_von_llama_cpp_with_llama_index.py
399 lines (337 loc) · 13.5 KB
/
kopie_von_llama_cpp_with_llama_index.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
!pip install langchain llama-index sentence-transformers
!CMAKE_ARGS="-DLLAMA_CUBLAS=on" FORCE_CMAKE=1 pip install llama-cpp-python==0.1.78 numpy==1.23.4 --force-reinstall --upgrade --no-cache-dir --verbose
!cd data
!git clone https://github.com/Darthph0enix7/Swagger_StockInterpretation
from langchain.embeddings import HuggingFaceEmbeddings
from llama_index import (
SimpleDirectoryReader,
VectorStoreIndex,
ServiceContext,
)
from llama_index.llms import LlamaCPP
from llama_index.llms.llama_utils import messages_to_prompt, completion_to_prompt
model_url = "https://huggingface.co/TheBloke/Llama-2-13B-chat-GGML/resolve/main/llama-2-13b-chat.ggmlv3.q4_0.bin"
llm = LlamaCPP(
# You can pass in the URL to a GGML model to download it automatically
model_url=model_url,
# optionally, you can set the path to a pre-downloaded model instead of model_url
model_path=None,
temperature=0.1,
max_new_tokens=256,
# llama2 has a context window of 4096 tokens, but we set it lower to allow for some wiggle room
context_window=3900,
# kwargs to pass to __call__()
generate_kwargs={},
# kwargs to pass to __init__()
# set to at least 1 to use GPU
model_kwargs={"n_gpu_layers": 64},
# transform inputs into Llama2 format
messages_to_prompt=messages_to_prompt,
completion_to_prompt=completion_to_prompt,
verbose=True,
)
from llama_index.llms.llama_utils import messages_to_prompt
from llama_index.prompts.base import ChatPromptTemplate
from llama_index.llms.base import ChatMessage, MessageRole
from langchain.embeddings import HuggingFaceEmbeddings
from llama_index import SimpleDirectoryReader, VectorStoreIndex, ServiceContext
from llama_index.llms import LlamaCPP
# text qa prompt
TEXT_QA_SYSTEM_PROMPT = ChatMessage(
content=(
"You are an expert Q&A system that is trusted around the world.\n"
"Always answer the query using the provided context information, "
"and not prior knowledge.\n"
"Some rules to follow:\n"
"1. Never directly reference the given context in your answer.\n"
"2. Avoid statements like 'Based on the context, ...' or "
"'The context information ...' or anything along "
"those lines."
),
role=MessageRole.SYSTEM,
)
TEXT_QA_PROMPT_TMPL_MSGS = [
TEXT_QA_SYSTEM_PROMPT,
ChatMessage(
content=(
"Context information is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given the context information and not prior knowledge, "
"answer the query.\n"
"Query: {query_str}\n"
"Answer: "
),
role=MessageRole.USER,
),
]
CHAT_TEXT_QA_PROMPT = ChatPromptTemplate(message_templates=TEXT_QA_PROMPT_TMPL_MSGS)
# Tree Summarize
TREE_SUMMARIZE_PROMPT_TMPL_MSGS = [
TEXT_QA_SYSTEM_PROMPT,
ChatMessage(
content=(
"Context information from multiple sources is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given the information from multiple sources and not prior knowledge, "
"answer the query.\n"
"Query: {query_str}\n"
"Answer: "
),
role=MessageRole.USER,
),
]
CHAT_TREE_SUMMARIZE_PROMPT = ChatPromptTemplate(
message_templates=TREE_SUMMARIZE_PROMPT_TMPL_MSGS
)
# Refine Prompt
CHAT_REFINE_PROMPT_TMPL_MSGS = [
ChatMessage(
content=(
"You are an expert Q&A system that stricly operates in two modes"
"when refining existing answers:\n"
"1. **Rewrite** an original answer using the new context.\n"
"2. **Repeat** the original answer if the new context isn't useful.\n"
"Never reference the original answer or context directly in your answer.\n"
"When in doubt, just repeat the original answer."
"New Context: {context_msg}\n"
"Query: {query_str}\n"
"Original Answer: {existing_answer}\n"
"New Answer: "
),
role=MessageRole.USER,
)
]
CHAT_REFINE_PROMPT = ChatPromptTemplate(message_templates=CHAT_REFINE_PROMPT_TMPL_MSGS)
# Table Context Refine Prompt
CHAT_REFINE_TABLE_CONTEXT_TMPL_MSGS = [
ChatMessage(content="{query_str}", role=MessageRole.USER),
ChatMessage(content="{existing_answer}", role=MessageRole.ASSISTANT),
ChatMessage(
content=(
"We have provided a table schema below. "
"---------------------\n"
"{schema}\n"
"---------------------\n"
"We have also provided some context information below. "
"{context_msg}\n"
"---------------------\n"
"Given the context information and the table schema, "
"refine the original answer to better "
"answer the question. "
"If the context isn't useful, return the original answer."
),
role=MessageRole.USER,
),
]
CHAT_REFINE_TABLE_CONTEXT_PROMPT = ChatPromptTemplate(
message_templates=CHAT_REFINE_TABLE_CONTEXT_TMPL_MSGS
)
# [Prompt Template Definitions go here]
# Initialize the LlamaCPP and other services
embed_model = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-mpnet-base-v2"
)
service_context = ServiceContext.from_defaults(
llm=llm,
embed_model=embed_model,
)
# Load documents
documents = SimpleDirectoryReader("/content").load_data()
# Combine the content of all the documents as context
context_data = documents
query_data = "When did the born?"
# Fill in the CHAT_TEXT_QA_PROMPT with the context and query
filled_prompt_msg = CHAT_TEXT_QA_PROMPT.message_templates[1].content.format(
context_str=context_data, query_str=query_data
)
# Convert the filled prompt message to a ChatMessage object before passing it to messages_to_prompt
filled_prompt_message_obj = ChatMessage(content=filled_prompt_msg, role=MessageRole.USER)
filled_prompt = messages_to_prompt([filled_prompt_message_obj])
# Create vector store index
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
# Set up query engine
query_engine = index.as_query_engine()
print(documents)
print(query_engine)
print(index)
print(embed_model)
# Pass the filled-in prompt to the LlamaCPP instance
response = llm.complete(filled_prompt)
print(response.text)
follow_up_question = "An where?"
# Combine original filled prompt, response, and follow-up question
combined_prompt_message = ChatMessage(
content=filled_prompt_msg + "\n" + response.text + "\n" + follow_up_question,
role=MessageRole.USER
)
combined_prompt = messages_to_prompt([combined_prompt_message])
# Get response to the follow-up question
new_response = llm.complete(combined_prompt)
print(new_response.text)
# Setting up prompt templates
from llama_index.llms.llama_utils import messages_to_prompt
from llama_index.prompts.base import ChatPromptTemplate
from llama_index.llms.base import ChatMessage, MessageRole
from langchain.embeddings import HuggingFaceEmbeddings
from llama_index import SimpleDirectoryReader, VectorStoreIndex, ServiceContext
from llama_index.llms import LlamaCPP
import requests
# text qa prompt
TEXT_QA_SYSTEM_PROMPT = ChatMessage(
content=(
"You are an expert Q&A system that is trusted around the world.\n"
"Always answer the query using the provided context information, "
"and not prior knowledge.\n"
"Some rules to follow:\n"
"1. Avoid statements like 'Based on the context, ...' or "
"'The context information ...' or anything along "
"those lines."
),
role=MessageRole.SYSTEM,
)
TEXT_QA_PROMPT_TMPL_MSGS = [
TEXT_QA_SYSTEM_PROMPT,
ChatMessage(
content=(
"Context information is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given the context information and not prior knowledge, "
"answer the query.\n"
"Query: {query_str}\n"
"Answer: "
),
role=MessageRole.USER,
),
]
CHAT_TEXT_QA_PROMPT = ChatPromptTemplate(message_templates=TEXT_QA_PROMPT_TMPL_MSGS)
# Tree Summarize
TREE_SUMMARIZE_PROMPT_TMPL_MSGS = [
TEXT_QA_SYSTEM_PROMPT,
ChatMessage(
content=(
"Context information from multiple sources is below.\n"
"---------------------\n"
"{context_str}\n"
"---------------------\n"
"Given the information from multiple sources and not prior knowledge, "
"answer the query.\n"
"Query: {query_str}\n"
"Answer: "
),
role=MessageRole.USER,
),
]
CHAT_TREE_SUMMARIZE_PROMPT = ChatPromptTemplate(
message_templates=TREE_SUMMARIZE_PROMPT_TMPL_MSGS
)
# Refine Prompt
CHAT_REFINE_PROMPT_TMPL_MSGS = [
ChatMessage(
content=(
"You are an expert Q&A system that stricly operates in two modes"
"when refining existing answers:\n"
"1. **Rewrite** an original answer using the new context.\n"
"2. **Repeat** the original answer if the new context isn't useful.\n"
"Never reference the original answer or context directly in your answer.\n"
"When in doubt, just repeat the original answer."
"New Context: {context_msg}\n"
"Query: {query_str}\n"
"Original Answer: {existing_answer}\n"
"New Answer: "
),
role=MessageRole.USER,
)
]
CHAT_REFINE_PROMPT = ChatPromptTemplate(message_templates=CHAT_REFINE_PROMPT_TMPL_MSGS)
# Table Context Refine Prompt
CHAT_REFINE_TABLE_CONTEXT_TMPL_MSGS = [
ChatMessage(content="{query_str}", role=MessageRole.USER),
ChatMessage(content="{existing_answer}", role=MessageRole.ASSISTANT),
ChatMessage(
content=(
"We have provided a table schema below. "
"---------------------\n"
"{schema}\n"
"---------------------\n"
"We have also provided some context information below. "
"{context_msg}\n"
"---------------------\n"
"Given the context information and the table schema, "
"refine the original answer to better "
"answer the question. "
"If the context isn't useful, return the original answer."
),
role=MessageRole.USER,
),
]
CHAT_REFINE_TABLE_CONTEXT_PROMPT = ChatPromptTemplate(
message_templates=CHAT_REFINE_TABLE_CONTEXT_TMPL_MSGS
)
# Initialize the LlamaCPP and other services
embed_model = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-mpnet-base-v2"
)
service_context = ServiceContext.from_defaults(
llm=llm,
embed_model=embed_model,
)
# Function to fetch stock price from Alpha Vantage
def fetch_stock_price(symbol):
API_KEY = "YOUR_ALPHA_VANTAGE_API_KEY" # Replace with your actual API key
endpoint = f"https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol={symbol}&interval=5min&apikey={API_KEY}"
response = requests.get(endpoint)
data = response.json()
# Extract the latest stock price
latest_time = max(data['Time Series (5min)'])
stock_price = data['Time Series (5min)'][latest_time]['1. open']
return stock_price
# Modified process_query function to handle stock-related questions
def process_query(query):
if "stock price" in query.lower() and "amazon" in query.lower():
stock_price = fetch_stock_price("AMZN")
# Create the context data
context_data = f"The current stock price of Amazon is ${stock_price}."
# Fill in the CHAT_TEXT_QA_PROMPT with the context and query
filled_prompt_msg = CHAT_TEXT_QA_PROMPT.message_templates[1].content.format(
context_str=context_data, query_str=query
)
# Convert the filled prompt message to a ChatMessage object before passing it to messages_to_prompt
filled_prompt_message_obj = ChatMessage(content=filled_prompt_msg, role=MessageRole.USER)
filled_prompt = messages_to_prompt([filled_prompt_message_obj])
# Send the filled-in prompt to the LlamaCPP instance
response = llm.complete(filled_prompt)
return response.text
# Handle other types of questions as previously
else:
documents = SimpleDirectoryReader("/content").load_data()
# Combine the content of all the documents as context
context_data = documents
# Fill in the CHAT_TEXT_QA_PROMPT with the context and query
filled_prompt_msg = CHAT_TEXT_QA_PROMPT.message_templates[1].content.format(
context_str=context_data, query_str=query_data
)
# Convert the filled prompt message to a ChatMessage object before passing it to messages_to_prompt
filled_prompt_message_obj = ChatMessage(content=filled_prompt_msg, role=MessageRole.USER)
filled_prompt = messages_to_prompt([filled_prompt_message_obj])
# Create vector store index
index = VectorStoreIndex.from_documents(documents, service_context=service_context)
# Set up query engine
query_engine = index.as_query_engine()
print(documents)
print(query_engine)
print(index)
print(embed_model)
# Pass the filled-in prompt to the LlamaCPP instance
response = llm.complete(filled_prompt)
print(response.text)
return "I'm not sure how to answer that."
# Sample usage:
query = "What is the stock price of Amazon today and how does it affect the market?"
response = process_query(query)
print(response)