-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_llama.py
210 lines (167 loc) · 6.41 KB
/
main_llama.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
import openai
import os
from dotenv import load_dotenv
from llama_index import (
SimpleDirectoryReader,
VectorStoreIndex,
ServiceContext,
StorageContext,
PromptHelper,
load_index_from_storage,
get_response_synthesizer,
set_global_service_context,
)
from llama_index.embeddings import HuggingFaceEmbedding
from llama_index.llms import OpenAI
from llama_index.text_splitter import SentenceSplitter
# from llama_index.extractors import (
# #SummaryExtractor,
# #QuestionsAnsweredExtractor,
# TitleExtractor,
# #KeywordExtractor,
# )
from llama_index.llms import HuggingFaceInferenceAPI
import torch
from llama_index.llms import Ollama
from llama_index.retrievers import VectorIndexRetriever
from llama_index.query_engine import RetrieverQueryEngine
from llama_index.postprocessor import SimilarityPostprocessor
import nest_asyncio
from llama_index.node_parser import SimpleNodeParser
# Initialize variables
documents_dir = "data/statements_txt_files"
llm_gpt3_name = "gpt-3.5-turbo-0613"
llm_llama2_name = "llama2"
llm_hf_name = "berkeley-nest/Starling-LM-7B-alpha"
llm_response_max_tokens = 1024
llm_temp = 0
# chunk_size = 256
chunk_overlap = 0
paragraph_separator = "\n\n"
# system_prompt = "Hello, I am a financial analyst. My expertise is answering questions about financial statements."
save_index = False
load_index = False
# custom_title_prompt = """\
# Context: {context_str}. Give a title that summarizes all of \
# the unique entities, titles or themes found in the context. Your answer should consist of nothing else but this title. Title: """
# nested asynchronous requests
nest_asyncio.apply()
# # Load environment variables from .env file
# load_dotenv()
# # Hugging Face Token
# # HF_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
# # Get the OpenAI API key
# openai_api_key = os.getenv("OPENAI_API_KEY")
# # Set the OpenAI API key
# openai.api_key = openai_api_key
# # LLMs
# llm_gpt3 = OpenAI(model=llm_gpt3_name, temperature=llm_temp, max_tokens=llm_response_max_tokens)
llm_llama2 = Ollama(
model=llm_llama2_name, temperature=llm_temp, max_tokens=llm_response_max_tokens
)
# Read the documents from the directory
reader = SimpleDirectoryReader(input_dir=documents_dir, filename_as_id=True)
documents = reader.load_data()
# Parse nodes
node_parser = SimpleNodeParser.from_defaults()
nodes = node_parser.get_nodes_from_documents(documents)
# # Chunking
# text_splitter = SentenceSplitter(
# # chunk_size=chunk_size,
# chunk_overlap=chunk_overlap,
# paragraph_separator=paragraph_separator,
# )
# # Meta data extractors
# meta_data_extractors = [
# TitleExtractor(nodes=1, llm=llm_llama2, node_template=custom_title_prompt),
# # QuestionsAnsweredExtractor(questions=3, llm=llm_hf),
# # SummaryExtractor(summaries=["prev", "self"], llm=llm_llama2),
# # KeywordExtractor(keywords=10, llm=llm_hf),
# ]
# Embedding
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
# # Prompt Helper
# prompt_helper = PromptHelper()
# Service Context - a bundle of commonly used resources used during the indexing and querying stage in a LlamaIndex pipeline
service_context = ServiceContext.from_defaults(
llm=llm_llama2,
embed_model=embed_model,
# text_splitter=text_splitter,
# prompt_helper=prompt_helper,
# system_prompt=system_prompt,
# transformations=[text_splitter], # + meta_data_extractors,
)
# Set the global service context
set_global_service_context(service_context)
# Indexing - load from disk or create new index from documents
if load_index:
storage_context = StorageContext.from_defaults(persist_dir="vector_store")
index = load_index_from_storage(storage_context, index_id="vector_index")
else:
index = VectorStoreIndex.from_documents(documents, use_async=True, show_progress=True)
# Save index to disk
if save_index:
index.set_index_id("vector_index")
index.storage_context.persist("./vector_store")
# # See meta data
# index.ref_doc_info
# Configure retriever
retriever = VectorIndexRetriever(
index=index,
similarity_top_k=3,
)
# Configure response synthesizer - when used in a query engine, the response synthesizer is used after nodes are retrieved from a retriever, and after any node-postprocessors are ran.
response_synthesizer = get_response_synthesizer(
response_mode="tree_summarize", # see https://docs.llamaindex.ai/en/stable/module_guides/deploying/query_engine/response_modes.html
structured_answer_filtering=True, # https://docs.llamaindex.ai/en/stable/module_guides/querying/response_synthesizers/root.html
)
# Assemble query engine
query_engine = RetrieverQueryEngine(
retriever=retriever,
response_synthesizer=response_synthesizer,
node_postprocessors=[SimilarityPostprocessor(similarity_cutoff=0.8)],
)
# response = query_engine.query("what is the revenue for UP Fintech?")# and Top Strike")
# print(response)
from llama_index.llama_pack import download_llama_pack
QueryRewritingRetrieverPack = download_llama_pack(
"QueryRewritingRetrieverPack",
"./query_rewriting_pack",
# leave the below commented out (was for testing purposes)
# llama_hub_url="https://raw.githubusercontent.com/run-llama/llama-hub/jerry/add_llama_packs/llama_hub",
)
query_rewriting_pack = QueryRewritingRetrieverPack(
nodes,
chunk_size=256,
vector_similarity_top_k=2,
)
# this will run the full pack
response = query_rewriting_pack.run("Compare revenues for UP Fintech and Top Strike")
print(str(response))
# remotely_run = HuggingFaceInferenceAPI(
# model_name="berkeley-nest/Starling-LM-7B-alpha",
# token=HF_TOKEN,
# url=API_URL,
# )
# message = [ChatMessage(
# role="user", # Role can be 'user' or 'system'
# content="What is the capital of France?" # The actual message content
# )]
# response = remotely_run.chat(message)
# print(response)
# HF_TOKEN = os.getenv("HUGGING_FACE_TOKEN")
# # This is your custom Hugging Face Inference Endpoint URL
# ENDPOINT_URL = "https://ghprpg1pq3gveb5b.us-east-1.aws.endpoints.huggingface.cloud"
# # Initialize the HuggingFaceInferenceAPI with your custom endpoint
# llm_hf = HuggingFaceInferenceAPI(
# model_name=ENDPOINT_URL,
# token=HF_TOKEN,
# # Specify any additional parameters you might need
# )
# message = [ChatMessage(
# role="user", # Role can be 'user' or 'system'
# content="What is the capital of France?" # The actual message content
# )]
# response = llm_hf.chat(message)
# print(response)
# Ollama