AI Python

Notes from the course of DeepLearning.AI - AI Python for Beginners

Mostly these are examples of practical use but you can have an overview to know how you could adapt them to you context.

  • Check out my page on python if you need a refresher on python here

Create an environment to use AI in Python script

Installations

If you want an all made setup, you can install Anaconda from here. In linux you just have to run bash Anaconda3-2024.10-1-Linux-x86_64.sh Once this is done you will have access to a gui to create your Jupyter notebook To launch said gui you can do as follow:

cd anaconda3
cd bin
./anaconda-navigator

You will need to create and account in anaconda cloud and login.

  • If you need any help, you can find the official doc here

Setting up the API key

import os
from dotenv import load_dotenv
from openai import OpenAI

# Get the OpenAI API key from the .env file
## For security reason I recommend that you rather use a vault with password rotation if you want to deploy something more permanently
load_dotenv('.env', override=True)
openai_api_key = os.getenv('OPENAI_API_KEY')
client = OpenAI(api_key = openai_api_key)

# Method to get llm response
def get_llm_response(prompt):
    completion = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {
                "role": "system",
                "content": "You are an AI assistant.",
            },
            {"role": "user", "content": prompt},
        ],
        temperature=0.0,
    )
    response = completion.choices[0].message.content
    return response

# test our method
prompt = "What is the capital of France?"
response = get_llm_response(prompt)
print(response)

# We can modify the temperature to change the randomness of the output
def get_llm_response_new_temp(prompt):
    completion = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {
                "role": "system",
                "content": "You are an AI assistant.", 
            },
            {"role": "user", "content": prompt},
        ],
        temperature=1.0, # change this to a value between 0 and 2
    )
    response = completion.choices[0].message.content
    return response

Building LLM prompts with variables

Iteratively updating AI prompts using lists

Using dictionaries to complete high priority tasks using AI

Customizing recipes with lists, dictionaries and AI

Helping AI make decisions

Using files in python and AI

Loading and using your own data

Extracting useful information from journal entries

Work with csv files and IA

Work with functions

Last updated