Update custom_llm.py
Browse files- custom_llm.py +39 -46
 
    	
        custom_llm.py
    CHANGED
    
    | 
         @@ -145,7 +145,9 @@ def format_df(df): 
     | 
|
| 145 | 
         
             
                return out
         
     | 
| 146 | 
         | 
| 147 | 
         | 
| 148 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 149 | 
         
             
                {code}
         
     | 
| 150 | 
         | 
| 151 | 
         
             
                Error Message : {err}
         
     | 
| 
         @@ -158,63 +160,54 @@ out_prompt = PromptTemplate.from_template("""<s><INST>Fix the following code: 
     | 
|
| 158 | 
         | 
| 159 | 
         
             
                </INST></s>""")
         
     | 
| 160 | 
         | 
| 
         | 
|
| 161 | 
         | 
| 
         | 
|
| 162 | 
         | 
| 163 | 
         
            -
             
     | 
| 164 | 
         
            -
             
     | 
| 165 | 
         
            -
            {df_example}
         
     | 
| 166 | 
         
            -
             
     | 
| 167 | 
         
            -
            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:
         
     | 
| 168 | 
         
            -
             
     | 
| 169 | 
         
            -
            {question}
         
     | 
| 170 | 
         
            -
             
     | 
| 171 | 
         
            -
            Always change the corresponding columns into datetime format with parameter day_first=True, example:
         
     | 
| 172 | 
         
            -
            df['column_name'] = pd.to_datetime(df['column_name'], day_first=True)
         
     | 
| 173 | 
         
            -
             
     | 
| 174 | 
         
            -
             
     | 
| 175 | 
         
            -
            Always use idxmin or idxmax instead of array indicies whenever it is possible 
         
     | 
| 176 | 
         | 
| 177 | 
         
            -
             
     | 
| 178 | 
         
            -
             
     | 
| 179 | 
         
            -
             
     | 
| 180 | 
         
            -
             
     | 
| 181 | 
         
            -
             
     | 
| 182 | 
         
            -
             
     | 
| 183 | 
         
            -
             
     | 
| 184 | 
         
            -
             
     | 
| 185 | 
         
            -
             
     | 
| 186 | 
         
            -
             
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 187 | 
         | 
| 188 | 
         | 
| 189 | 
         | 
| 190 | 
         
             
            def custom_dataframe_chain(llm, df):
         
     | 
| 191 | 
         
            -
                prompt =  
     | 
| 192 | 
         
            -
             
     | 
| 193 | 
         
            -
                def out_format(text, df):
         
     | 
| 194 | 
         | 
| 195 | 
         
            -
             
     | 
| 196 | 
         | 
| 197 | 
         
            -
             
     | 
| 198 | 
         | 
| 199 | 
         
            -
             
     | 
| 200 | 
         | 
| 201 | 
         
            -
             
     | 
| 
         | 
|
| 202 | 
         | 
| 203 | 
         
            -
             
     | 
| 204 | 
         
            -
             
     | 
| 205 | 
         
            -
             
     | 
| 206 | 
         
            -
             
     | 
| 207 | 
         
            -
             
     | 
| 208 | 
         
            -
             
     | 
| 209 | 
         
            -
             
     | 
| 210 | 
         
            -
             
     | 
| 211 | 
         
            -
             
     | 
| 212 | 
         
            -
             
     | 
| 213 | 
         
            -
             
     | 
| 214 | 
         
            -
             
     | 
| 215 | 
         
            -
             
     | 
| 216 | 
         
            -
                    return "Bad Python Code, Error Message : " + str(e_)
         
     | 
| 217 | 
         
            -
             
     | 
| 218 | 
         | 
| 219 | 
         
             
                return prompt | llm | RunnableLambda(lambda x:out_format(x, df))
         
     | 
| 220 | 
         | 
| 
         | 
|
| 145 | 
         
             
                return out
         
     | 
| 146 | 
         | 
| 147 | 
         | 
| 148 | 
         
            +
            def out_format(text, df):
         
     | 
| 149 | 
         
            +
             
     | 
| 150 | 
         
            +
                prompt = PromptTemplate.from_template("""<s><INST>Fix the following code:
         
     | 
| 151 | 
         
             
                {code}
         
     | 
| 152 | 
         | 
| 153 | 
         
             
                Error Message : {err}
         
     | 
| 
         | 
|
| 160 | 
         | 
| 161 | 
         
             
                </INST></s>""")
         
     | 
| 162 | 
         | 
| 163 | 
         
            +
                err_chain = prompt | llm
         
     | 
| 164 | 
         | 
| 165 | 
         
            +
                e_ = None
         
     | 
| 166 | 
         | 
| 167 | 
         
            +
                for _ in range(6):
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 168 | 
         | 
| 169 | 
         
            +
                    try :
         
     | 
| 170 | 
         
            +
                        # text = text.split("```python")[-1].split("```")[0]
         
     | 
| 171 | 
         
            +
                        text = text.split("# Generated Code")[-1].split("`")[0]
         
     | 
| 172 | 
         
            +
                        print(text)
         
     | 
| 173 | 
         
            +
                        exec(text)
         
     | 
| 174 | 
         
            +
                        
         
     | 
| 175 | 
         
            +
                        return text
         
     | 
| 176 | 
         
            +
                    except Exception as e:
         
     | 
| 177 | 
         
            +
                        print(e)
         
     | 
| 178 | 
         
            +
                        text = err_chain.invoke({"code":text, "err":str(e)})
         
     | 
| 179 | 
         
            +
                        e_ = e
         
     | 
| 180 | 
         
            +
                # exec(text)
         
     | 
| 181 | 
         
            +
                
         
     | 
| 182 | 
         
            +
                return "Bad Python Code, Error Message : " + str(e_)
         
     | 
| 183 | 
         | 
| 184 | 
         | 
| 185 | 
         | 
| 186 | 
         
             
            def custom_dataframe_chain(llm, df):
         
     | 
| 187 | 
         
            +
                prompt = PromptTemplate.from_template("""<s><INST>You have access to a pandas dataframe variable named "df". Below are the examples of the dataframe:
         
     | 
| 
         | 
|
| 
         | 
|
| 188 | 
         | 
| 189 | 
         
            +
                {df_example}
         
     | 
| 190 | 
         | 
| 191 | 
         
            +
                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:
         
     | 
| 192 | 
         | 
| 193 | 
         
            +
                {question}
         
     | 
| 194 | 
         | 
| 195 | 
         
            +
                Always change the corresponding columns into datetime format with parameter day_first=True, example:
         
     | 
| 196 | 
         
            +
                df['column_name'] = pd.to_datetime(df['column_name'], day_first=True)
         
     | 
| 197 | 
         | 
| 198 | 
         
            +
                
         
     | 
| 199 | 
         
            +
                Always use idxmin or idxmax instead of array indicies whenever it is possible 
         
     | 
| 200 | 
         
            +
                
         
     | 
| 201 | 
         
            +
                
         
     | 
| 202 | 
         
            +
                Do not import pandas and Do not create or re-assign "df" variable
         
     | 
| 203 | 
         
            +
                
         
     | 
| 204 | 
         
            +
                
         
     | 
| 205 | 
         
            +
                The output must follow the following example format:
         
     | 
| 206 | 
         
            +
                ```python
         
     | 
| 207 | 
         
            +
                # Generated Code
         
     | 
| 208 | 
         
            +
                ```
         
     | 
| 209 | 
         
            +
                
         
     | 
| 210 | 
         
            +
                </INST></s>""")
         
     | 
| 
         | 
|
| 
         | 
|
| 211 | 
         | 
| 212 | 
         
             
                return prompt | llm | RunnableLambda(lambda x:out_format(x, df))
         
     | 
| 213 | 
         |