import pandas as pd
A personal weblog about all things related to tech and programming.
Search
Wednesday, 11 October 2023
Using Pandas Agent with OpenAI and Langchain for reading CSV
Read from Postgres DB using langchain and OpenAI
from langchain import OpenAI, SQLDatabase
Microsoft Autogen with a single agent
Prerequisites:
pip install pyautogen
Code:
Tuesday, 26 September 2023
LLMs vs chat models in LangChain
LLMs are models string as input and returns a string.
chat models are models which takes a list of messages as input and returns a message.
You will find this generic answer on langchain website.
Let's see what it actually means in code.
In this first example, both of these behave in the exact same way:
LLM:
Chat Model:
Sunday, 24 September 2023
ImportError: No module named openai, name 'ChatOpenAI' is not defined
There could be many reasons for these error messages related to ChatOpenAI and openai.
If the issue is related to python or pip it will get resolved by following this:
python -m pip uninstall openaipython -m pip install --upgrade pippython -m pip install openaiSometimes openai might require a higher version of python then what is installed on your system. pip show openaiHow to Set and Get Environment Variables in Python
Environment variables are a crucial aspect of software development and system configuration. They provide a way to store configuration settings, API keys, and other sensitive information outside of your codebase, making it easier to manage and secure your applications. Python offers built-in modules to interact with these environment variables, allowing you to set and retrieve values as needed. In this blog post, we'll explore how to set and get environment variables in Python.
What Are Environment Variables?
Environment variables are dynamic values that can affect the way running processes behave on a computer. They are part of the environment in which a process runs and can be used to pass configuration information to applications and system utilities.
Common use cases for environment variables include:
Storing sensitive information like API keys and database credentials.
Configuring application settings such as server addresses and log levels.
Specifying system-level variables like the PATH, which tells the system where to find executable files.
Setting Environment Variables
To set an environment variable in Python, you can use the os module. Here's how to do it:
import os
# Set an environment variable
os.environ['MY_VARIABLE'] = 'my_value'
In this example, we set an environment variable named MY_VARIABLE with the value 'my_value'. Once set, this variable will be available to other processes and scripts that run in the same environment.
It's important to note that setting environment variables in a running Python process will only affect that process and its child processes. To make environment variables persist across sessions, you may need to configure them at the system level, depending on your operating system.
Getting Environment Variables
Retrieving the value of an environment variable in Python is just as simple as setting one:
import os
# Get the value of an environment variable
value = os.environ.get('MY_VARIABLE')
if value:
print(f'The value of MY_VARIABLE is: {value}')
else:
print('MY_VARIABLE is not set.')
Handling Missing Environment Variables
How to create a python virtual environment using venv
Open Powershell:
Navigate to the directory where you want to create a python environment and then type:
python -m venv mynewenv
This command will create a folder named mynewenv in that directory.
Navigate inside this directory by
cd mynewenv
Now to activate the environment type in:
.\Scripts\activate
openai.error.RateLimitError: You exceeded your current quota, please check your plan and billing details.
Error Message: openai.error.RateLimitError: You exceeded your current quota, please check your plan and billing details.
Steps to resolve:
You need to upgrade to a paid plan. Set up a paid account, add a credit or debit card. Add some credit, you can you Pay as you Go method.
After 10-15 minutes generate a new API key and use that in the code
Wednesday, 19 July 2023
modularize your PySpark code that includes SQL queries based on createOrReplaceTempView
To modularize your PySpark code that includes SQL queries based on createOrReplaceTempView, you can follow a few steps:
Create separate Python modules for different components of your code.
Define functions in each module to encapsulate the logic related to specific SQL queries and transformations.
Import the necessary modules and call the functions in your main code.
In this example, the create_temp_views function in queries.py encapsulates the logic to read CSV files, create temporary views using createOrReplaceTempView, and perform a join operation. By separating the SQL-related code into a separate module, you can easily reuse and maintain your queries.
Note that you need to replace the file paths and SQL queries in the example with your specific requirements. Additionally, you might need to adjust the imports and customize the functions according to your actual codebase.
Create a module named queries.py to define functions for your SQL queries:
# queries.py
from pyspark.sql import SparkSession
def create_temp_views(spark):
# Register temp views using createOrReplaceTempView
df1 = spark.read.csv("data/file1.csv", header=True)
df1.createOrReplaceTempView("table1")
df2 = spark.read.csv("data/file2.csv", header=True)
df2.createOrReplaceTempView("table2")
df3 = spark.sql("SELECT * FROM table1 JOIN table2 ON table1.id = table2.id")
df3.createOrReplaceTempView("table3")
# Return the SparkSession for further use, if needed
return spark
Create your main script and import the queries module to call the functions:
# main.py
from pyspark.sql import SparkSession
import queries
# Create a SparkSession
spark = SparkSession.builder.appName("Modularized PySpark").getOrCreate()
# Call the create_temp_views function from queries module
spark = queries.create_temp_views(spark)
# Use the registered temp views for further processing
result = spark.sql("SELECT * FROM table3")
result.show()
# Stop the SparkSession
spark.stop()
Best practices for organizing PySpark code with SQL usage
Example illustrating the best practices for organizing PySpark code with extensive SQL usage:
In this example, the SQL code is stored in separate SQL files within the sql/ directory. The data_preprocessing.sql and feature_engineering.sql files encapsulate the respective SQL operations.
The feature_engineering.sql file imports common functions from common_functions.sql within the utils/ subdirectory. This approach promotes code reusability and separates the common functions from the main SQL code.
The main.py script reads the SQL files and executes the SQL code using spark.sql(). By following this approach, you maintain a clean separation between the PySpark code and SQL logic, making it easier to manage and maintain your project.
Directory Structure:
project/
├── main.py
└── sql/
├── data_preprocessing.sql
├── feature_engineering.sql
└── utils/
├── common_functions.sql
└── feature_functions.sql
data_preprocessing.sql:
-- data_preprocessing.sql
-- Define the SQL operations for data preprocessing
CREATE OR REPLACE TEMPORARY VIEW cleaned_data AS
SELECT
column1,
column2,
-- Perform data preprocessing operations here
FROM
input_data;
feature_engineering.sql:
-- feature_engineering.sql
-- Import common functions
:load sql/utils/common_functions.sql
-- Define the SQL operations for feature engineering
CREATE OR REPLACE TEMPORARY VIEW transformed_data AS
SELECT
column1,
column2,
-- Perform feature engineering operations here
-- Example using a custom feature function
feature_function(column1) AS new_feature
FROM
cleaned_data;
common_functions.sql (in sql/utils/):
-- common_functions.sql
-- Define common SQL functions used across multiple SQL files
CREATE OR REPLACE FUNCTION feature_function(column)
RETURNS feature
BEGIN
-- Implement the feature function logic here
-- ...
RETURN feature_value;
END;
main.py:
from pyspark.sql import SparkSession
# Create SparkSession
spark = SparkSession.builder.getOrCreate()
# Load input data
input_data = spark.read.csv("data.csv", header=True, inferSchema=True)
input_data.createOrReplaceTempView("input_data")
# Load and execute SQL scripts
with open("sql/data_preprocessing.sql") as f:
data_preprocessing_sql = f.read()
spark.sql(data_preprocessing_sql)
with open("sql/feature_engineering.sql") as f:
feature_engineering_sql = f.read()
spark.sql(feature_engineering_sql)
# Retrieve the transformed data
transformed_data = spark.table("transformed_data")
# Perform further operations on transformed_data or train models, etc.