Search

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:











































Now to explain how chat models are different, look at the below example:







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 openai
python -m pip install --upgrade pip
python -m pip install openai
Sometimes openai might require a higher version of python then what is installed on your system.
You can check the required version as follows: 
pip show openai

How 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.')


In this code snippet, we use os.environ.get('MY_VARIABLE') to retrieve the value of the environment variable named MY_VARIABLE. If the variable is set, we print its value; otherwise, we indicate that it's not set.

Handling Missing Environment Variables

It's essential to handle cases where an expected environment variable is not set. Failing to do so can result in unexpected errors and application crashes. Using os.environ.get('MY_VARIABLE') allows you to check if the variable exists and provide a default value or handle the absence gracefully.

import os

# Get the value of an environment variable with a default value
value = os.environ.get('MY_VARIABLE', 'default_value')
print(f'The value of MY_VARIABLE is: {value}')

n this example, if MY_VARIABLE is not set, the code will use 'default_value' as a fallback.

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