Skip to content

D-Lab's 2-hour introduction to using APIs to access Large Language Models. Learn API setup & authentication, API call formatting, creating structured outputs, and more.

Notifications You must be signed in to change notification settings

dlab-berkeley/Python-APIs-for-Large-Language-Models

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

D-Lab Python APIs for Large Language Models Workshop

Datahub Binder License: CC BY 4.0

This repository contains the materials for D-Lab's Python APIs for Large Language Models Workshop.

Prerequisites

We recommend attending Python Fundamentals prior to this workshop.

Check D-Lab's Learning Pathways to figure out which of our workshops to take!

Workshop Goals

In this hands-on workshop, you will learn how to use Python to call LLM APIs and get structured responses for research applications. By the end, you'll be able to integrate LLMs into your social science research workflow to classify data, extract insights, and analyze content at scale. You'll walk away with best practices for LLM integration in research projects, example scripts for common research tasks, and the confidence to begin incorporating AI into your own research workflow.

Learning Objectives

After this workshop, you will be able to do:

  • API Setup & Authentication: Configure access to an LLM provider using OpenRouter for research projects.
  • API Call Formatting: Understand the differences between chat and completion endpoints, construct proper request formats, handle parameters like temperature and max tokens, and manage conversation context.
  • Structured Output: Design prompts that return consistent JSON responses for tasks like sentiment classification, theme extraction, content coding, and demographic categorization.
  • Integration & Error Handling: Parse API responses into Python objects, implement retry logic for failed requests, manage rate limits and costs when processing research datasets.

This workshop does not cover the following:

Installation Instructions

Before attending the workshop, you should install Python and Jupyter to your computer. If you need help, please submit a consulting request with D-Lab prior to the start of the workshop.

Anaconda is software that allows you to run Python and Jupyter notebooks on your computer. Installing Anaconda is the easiest way to make sure you have all the necessary software to run the materials for this workshop. Complete the following steps:

  1. Download and install Anaconda (Python 3.9 distribution). Click "Download" and then click 64-bit "Graphical Installer" for your current operating system.
  2. Download the materials in this repository:
  • Click the green "Code" button in the top right of the repository information.
  • Click "Download Zip".
  • Extract this file to a folder on your computer where you can easily access it (we recommend Desktop).
  1. Optional: if you're familiar with git, you can instead clone this repository by opening a terminal and entering git clone https://github.com/dlab-berkeley/Python-Fundamentals.git.

Run the code

Now that you have all the required software and materials, you need to run the code:

  1. Open the Anaconda Navigator application. You should see the green snake logo appear on your screen. Note that this can take a few minutes to load up the first time.

  2. Click the "Launch" button under "Jupyter Lab" and navigate through your file system to the Python-Fundamentals folder you downloaded above.

  3. Navigate to lessons -> Fundamentals-I

  4. Open the 01_Jupyter_and_Python.ipynb to begin.

  5. Press Shift + Enter (or Ctrl + Enter) to run a cell.

Note: if you are having trouble accessing the Downloads/Desktop folder from Anaconda Navigator, check out this post or submit a consulting request to get help!

Is Python not working on your laptop?

If you do not have Anaconda installed and the materials loaded on your workshop by the time it starts, we strongly recommend using the UC Berkeley Datahub to run the materials for these lessons. You can access the DataHub by clicking this button:

Datahub

The DataHub downloads this repository, along with any necessary packages, and allows you to run the materials in a Jupyter notebook that is stored on UC Berkeley's servers. No installation is necessary from your end - you only need an internet browser and a CalNet ID to log in. By using the DataHub, you can save your work and come back to it at any time. When you want to return to your saved work, just go straight to DataHub, sign in, and you click on the Python-Fundamentals folder.

If you don't have a Berkeley CalNet ID, you can still run these lessons in Binder, which is another cloud-based option. Click this button:

Binder

Note: Using Binder, you unfortunately cannot save your work.

About the UC Berkeley D-Lab

D-Lab works with Berkeley faculty, research staff, and students to advance data-intensive social science and humanities research. Our goal at D-Lab is to provide practical training, staff support, resources, and space to enable you to use R for your own research applications. Our services cater to all skill levels and no programming, statistical, or computer science backgrounds are necessary. We offer these services in the form of workshops, one-to-one consulting, and working groups that cover a variety of research topics, digital tools, and programming languages.

Visit the D-Lab homepage to learn more about us. You can view our calendar for upcoming events, learn about how to utilize our consulting and data services, and check out upcoming workshops.

Other D-Lab Python Workshops

Here are other Python workshops offered by the D-Lab:

Basic competency

Intermediate/advanced competency

Contributors

Sohail Khan Pratik Sachdeva Tom van Nuenen

Provide a list of contributors here, with links to their webpages (D-Lab or private).

About

D-Lab's 2-hour introduction to using APIs to access Large Language Models. Learn API setup & authentication, API call formatting, creating structured outputs, and more.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published