Update custom_llm.py
Browse files- custom_llm.py +88 -0
custom_llm.py
CHANGED
@@ -126,6 +126,94 @@ def custom_chain_with_history(llm, memory):
|
|
126 |
# return {"chat_history":prompt_memory, "context":asyncio.run(create_vectorstore()).as_retriever(search_type="similarity", search_kwargs={"k": 12}) | format_docs, "question": RunnablePassthrough()} | prompt | llm
|
127 |
return {"chat_history":lambda x:prompt_memory(x['memory']), "context":itemgetter("question") | asyncio.run(create_vectorstore()).as_retriever(search_type="similarity", search_kwargs={"k": 100000}) | format_docs, "question": lambda x:x['question']} | prompt | llm
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
class CustomLLM(LLM):
|
130 |
repo_id : str
|
131 |
api_token : str
|
|
|
126 |
# return {"chat_history":prompt_memory, "context":asyncio.run(create_vectorstore()).as_retriever(search_type="similarity", search_kwargs={"k": 12}) | format_docs, "question": RunnablePassthrough()} | prompt | llm
|
127 |
return {"chat_history":lambda x:prompt_memory(x['memory']), "context":itemgetter("question") | asyncio.run(create_vectorstore()).as_retriever(search_type="similarity", search_kwargs={"k": 100000}) | format_docs, "question": lambda x:x['question']} | prompt | llm
|
128 |
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
def format_df(df):
|
133 |
+
out = ""
|
134 |
+
|
135 |
+
for x in df.columns:
|
136 |
+
out+= x + "|"
|
137 |
+
out = out[:-1]
|
138 |
+
|
139 |
+
for _,row in df.iterrows():
|
140 |
+
for x in row.values:
|
141 |
+
out += str(x) + "|"
|
142 |
+
|
143 |
+
out = out[:-1]
|
144 |
+
|
145 |
+
return out
|
146 |
+
|
147 |
+
|
148 |
+
def custom_dataframe_chain(llm, dataframe):
|
149 |
+
prompt = PromptTemplate.from_template("""<s><INST>You have access to a dataframe variable named df. Below are the examples of the dataframe:
|
150 |
+
|
151 |
+
{df_example}
|
152 |
+
|
153 |
+
Given the following user input, create relevant python code to get the relevant information in the dataframe and store the response string result in a variable named "response". Do not explain, just create the python code:
|
154 |
+
|
155 |
+
{question}
|
156 |
+
|
157 |
+
Always change the corresponding columns into datetime format with parameter day_first=True, example:
|
158 |
+
df['column_name'] = pd.to_datetime(df['column_name'], day_first=True)
|
159 |
+
|
160 |
+
|
161 |
+
Always use idxmin or idxmax instead of array indicies whenever it is possible
|
162 |
+
|
163 |
+
|
164 |
+
The output must follow the following example format:
|
165 |
+
```python
|
166 |
+
# Generated Code
|
167 |
+
```
|
168 |
+
|
169 |
+
</INST></s>""")
|
170 |
+
|
171 |
+
def out_format(text:str):
|
172 |
+
|
173 |
+
prompt = PromptTemplate.from_template("""<s><INST>Fix the following code:
|
174 |
+
{code}
|
175 |
+
|
176 |
+
Error Message : {err}
|
177 |
+
|
178 |
+
|
179 |
+
The output must follow the following example format:
|
180 |
+
```python
|
181 |
+
# Generated Code
|
182 |
+
```
|
183 |
+
|
184 |
+
</INST></s>""")
|
185 |
+
|
186 |
+
err_chain = prompt | llm
|
187 |
+
|
188 |
+
e_ = None
|
189 |
+
|
190 |
+
for _ in range(6):
|
191 |
+
|
192 |
+
try :
|
193 |
+
text = text.split("```python")[-1].split("```")[0]
|
194 |
+
# print(text)
|
195 |
+
exec(text)
|
196 |
+
break
|
197 |
+
# return response
|
198 |
+
except Exception as e:
|
199 |
+
# print(e)
|
200 |
+
text = err_chain.invoke({"code":text, "err":str(e)})
|
201 |
+
e_ = e
|
202 |
+
exec(text)
|
203 |
+
|
204 |
+
return text
|
205 |
+
|
206 |
+
return "Bad Python Code, Error Message : " + str(e_)
|
207 |
+
|
208 |
+
|
209 |
+
return RunnablePassthrough.assign(df_example=format_df(dataframe.head(4))) | prompt | llm | out_format
|
210 |
+
|
211 |
+
|
212 |
+
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
|
217 |
class CustomLLM(LLM):
|
218 |
repo_id : str
|
219 |
api_token : str
|