ArunSamespace commited on
Commit
f34fc63
1 Parent(s): ac0c316

Delete model.py

Browse files
Files changed (1) hide show
  1. model.py +0 -207
model.py DELETED
@@ -1,207 +0,0 @@
1
- import os
2
-
3
- os.environ["GOOGLE_API_KEY"] = "AIzaSyAGoYnNPu__70AId7EJS7F_61i69Qmn-wM"
4
- os.environ["OPENAI_API_TYPE"] = "azure"
5
- # os.environ["OPENAI_API_VERSION"] = "2023-07-01-preview"
6
- # # os.environ["OPENAI_API_KEY"] = "5b624f6b71884a488560a86b1fffbf42"
7
- # os.environ["OPENAI_API_KEY"] = "9e337d6696ce4a22a9a1b901e2ebb5fb"
8
- import sys
9
-
10
- sys.path.append('../')
11
-
12
- from langchain.chat_models import AzureChatOpenAI, ChatOpenAI
13
- from langchain.prompts.chat import (ChatPromptTemplate,
14
- HumanMessagePromptTemplate,
15
- SystemMessagePromptTemplate)
16
- from langchain_google_genai import ChatGoogleGenerativeAI
17
- from modules.chat.helpers.embedder import CustomEmbeddings
18
- from modules.chat.helpers.search import SimilaritySearch
19
-
20
- embeddings = CustomEmbeddings(
21
- model="text-embedding-ada-002",
22
- model_url="https://year-embedding-ada-002-aiservices-2136192926.openai.azure.com//openai/deployments/fresh-embedding-ada-002/embeddings?api-version=2023-10-01-preview",
23
- api_key="6eed3006cdd3445cb3f422a7358ce461"
24
- )
25
- vector_store = SimilaritySearch.load_from_disk(
26
- embedding_function=embeddings,
27
- data_dir="../indexs/text-embedding-ada-002/"
28
- # data_dir="../indexs/basic-fno-text-embedding-ada-002/"
29
- )
30
-
31
- class Model:
32
- def __init__(self, model_name: str, **kwargs) -> None:
33
- self.model_name = model_name
34
- self.llm = self.load_llm(model_name=model_name, **kwargs)
35
-
36
- def load_llm(self, model_name: str, **kwargs):
37
- if self.model_name == "gemini-pro":
38
- self.retriever = vector_store.as_retriever(search_kwargs={"k": 2}, search_type="similarity")
39
- return ChatGoogleGenerativeAI(model=model_name, temperature=0, max_tokens=4096)
40
- elif self.model_name == "gpt-3.5-turbo":
41
- self.retriever = vector_store.as_retriever(search_kwargs={"k": 2}, search_type="similarity")
42
- return AzureChatOpenAI(
43
- deployment_name="latest-gpt-35-turbo-16k",
44
- temperature=0,
45
- max_tokens=4096,
46
- # azure_endpoint="https://high-gpt4-32k-0613-aiservices336365459.openai.azure.com/",
47
- openai_api_key="9e337d6696ce4a22a9a1b901e2ebb5fb",
48
- # openai_api_base="https://jan-2024-gpt35-turbo16k-aiservices800630185.openai.azure.com/",
49
- openai_api_base = "https://fresh-gpt35-turbo-aiservices-2112150452.openai.azure.com/",
50
- openai_api_version="2023-07-01-preview"
51
- )
52
- elif self.model_name == "gpt4":
53
- self.retriever = vector_store.as_retriever(search_kwargs={"k": kwargs.get("k", 2)}, search_type="similarity")
54
- return AzureChatOpenAI(
55
- deployment_name="gpt-4-32k",
56
- temperature=0,
57
- max_tokens=4096,
58
- # azure_endpoint="https://high-gpt4-32k-0613-aiservices336365459.openai.azure.com/",
59
- openai_api_key="e91a341abb2f4646ab7b0acd3b9d461e",
60
- openai_api_base="https://jan-2024-gpt4-ai-aiservices-1959882301.openai.azure.com/",
61
- openai_api_version="2023-07-01-preview"
62
- )
63
-
64
- self.retriever = vector_store.as_retriever(search_kwargs={"k": kwargs.get("k", 1)}, search_type="similarity")
65
- return ChatOpenAI(
66
- model=model_name,
67
- openai_api_key="EMPTY",
68
- openai_api_base="http://localhost:8000/v1",
69
- max_tokens=1024,
70
- temperature=0,
71
- model_kwargs={"stop": ["<|im_end|>", "Query:", "Question:"], "top_p": 0.95}
72
- )
73
-
74
-
75
- def run_qa_result(self, query: str):
76
- support_docs = self.retriever.get_relevant_documents(query)
77
- sources = list({d.metadata['source'] for d in support_docs})
78
- context = "\n\n".join([f"{i + 1}. {d.page_content}" for i, d in enumerate(support_docs)])
79
- return context, sources
80
-
81
- def return_prompt(self, system_prompt: str, query: str, context: str):
82
-
83
- # human_template = "Context:\n\n{context}\n\nQuery: {query}"
84
- # human_template = "E-Book:\n\n{context}\n\nQuestion: {query}"
85
-
86
- human_template = "\n\nContext:\n\n{context}\n\nQuestion: {query}"
87
- # human_template = "\n\nBook:\n\n{context}\n\nQuestion: {query}"
88
-
89
- messages = []
90
- if self.model_name in [
91
- "gemini-pro",
92
- "TheBloke/Mistral-7B-Instruct-v0.2-AWQ",
93
- ]:
94
- human_template = system_prompt + "\n\n" + human_template
95
- human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
96
- messages.append(human_message_prompt)
97
- else:
98
- system_message_prompt = SystemMessagePromptTemplate.from_template(system_prompt)
99
- human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)
100
- messages.extend([system_message_prompt, human_message_prompt])
101
-
102
- chat_prompt = ChatPromptTemplate.from_messages(messages)
103
- return chat_prompt.format_prompt(context=context, query=query).to_messages()
104
-
105
- def run(self, system_prompt: str, query: str):
106
- context, sources = self.run_qa_result(query=query)
107
- chat_prompt = self.return_prompt(system_prompt=system_prompt, query=query, context=context)
108
- # text = "".join(resp.content for resp in self.llm.stream(chat_prompt))
109
- # text += "\nSources: \n" + "\n".join([f"{i + 1}. {d}" for i, d in enumerate(sources)])
110
- # return text, sources
111
- for resp in self.llm.stream(chat_prompt):
112
- yield resp.content.replace("$", "₹")
113
-
114
- yield sources
115
- # text = "".join(resp.content for resp in self.llm.stream(chat_prompt))
116
- # text += "\nSources: \n" + "\n".join([f"{i + 1}. {d}" for i, d in enumerate(sources)])
117
- # return text, sources
118
-
119
- def get_sources(query):
120
- results = vector_store.similarity_search_with_relevance_scores(query, k=2)
121
- return [
122
- {
123
- "score": r[-1],
124
- "source": r[0].metadata['source']
125
- }
126
- for r in results
127
- ]
128
-
129
- if __name__ == "__main__":
130
- # model = Model(model_name="phi2")
131
- # model = Model(model_name="gpt-3.5-turbo")
132
- # model = Model(model_name="gemini-pro")
133
- # model = Model(model_name="TheBloke/zephyr-7B-beta-AWQ")
134
- # model = Model(model_name="TheBloke/neural-chat-7B-v3-3-AWQ")
135
- model = Model(model_name="TheBloke/Mistral-7B-Instruct-v0.2-AWQ")
136
- model = Model(model_name="gpt4")
137
- model = Model(model_name="gpt-3.5-turbo")
138
-
139
- # query = "what is reliance?"
140
- # print("results: ", get_sources(query))
141
-
142
- # query = "explain FNO trading?"
143
- # print("results: ", get_sources(query))
144
-
145
- # query="What is FNO trading?"
146
- # query = "Describe ITM, ATM and OTM"
147
- # query = "give formula to calculate intrinsic value in Put and provide an example"
148
- # query = "what is the order of delta, theta, gamma and vega amongst options in a given options chain"
149
- # query = "Explain apple stock and nasdaq"
150
-
151
- # query = "generate a table with long and short in F&O instruments"
152
- # query = "how can we calculate intrinsic value and time value"
153
- # query = "give formula to calculate intrinsic value in Put"
154
-
155
- query = "explain exit from a put trade"
156
- #
157
- # query = "what will be buying cost if I long tesla CE"
158
-
159
-
160
- # system_prompt="""Use the following pieces of context to answer the question in detail. Provide example only if it is in provided context and make sure to use them in rupees.""",
161
-
162
- # system_prompt = """Use the following pieces of context to answer the question in detail. Provide example only if it is in context and make sure to use them in ₹.
163
- # If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
164
-
165
- # system_prompt = """Answer the question using the context. Provide examples only from the context and use only Rupees (₹) in examples. If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
166
-
167
- # system_prompt = """Your task is to answer the question using the given context.
168
-
169
- # Follow the below rules while answering the question:
170
- # - Only create example using the context
171
- # - Use only Rupees '₹' to represent currency.
172
- # - If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
173
-
174
- # system_prompt = """You are an Indian Stock Market Assistant. Your task is to answer the question using the given context. Only create example from the given context and don't use '$'."""
175
-
176
- # query = "what is reliance?"
177
- # query = "what is python?"
178
- query = "what is an apple stock and nasdq"
179
- query = "Generate a tabular format on playing long and short through options"
180
- query = "What is FNO Trading?"
181
-
182
- system_prompt = """Answer the question only from context.
183
- Provide examples only from the context.
184
- If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
185
-
186
- system_prompt = "Answer the question only from the e-book. If it is not sufficient then respond as \"Unknown\""
187
- system_prompt = """Use the following pieces of book to answer the question at the end. \nIf you don't know the answer, please think rationally and answer from the book"""
188
- # system_prompt = """Answer the question using the context. Provide examples only from the context and use only Rupees (₹) in examples. If you don't know the answer, just say 'Please rephrase the question I am unable to answer'"""
189
-
190
- # system_prompt = """Answer the question from the context. Provide examples only from the context. If you don't know the answer, just say 'Please rephrase the question'"""
191
- # system_prompt = """Answer the question from the book. Provide examples only from the book. If you don't know the answer, just say 'Please rephrase the question'"""
192
-
193
- response = model.run(
194
- system_prompt=system_prompt,
195
- query=query
196
- )
197
- text = ""
198
- for resp in response:
199
- if isinstance(resp, list):
200
- sources = resp
201
- break
202
- text += resp
203
-
204
- text = text.split("Question")[0].strip("\n")
205
-
206
- print("text: ", text)
207
- open("./text.txt", "w").write(text)