Programming with AI: Exploring REST APIs and Libraries
Written on
Introduction to AI API Programming
Recently, I was invited to conduct a brief workshop on programming with AI. As I prepared the slides and code, my materials expanded significantly. Thus, I decided to compile these resources into a series of articles, which could also serve as reference material for future classes. This article marks the first installment, focusing on how to call APIs from AI providers using REST APIs and libraries.
Note: This content is intended as an introduction and does not cover all aspects comprehensively.
Getting Started with API Calls
To kick things off, let's dive into making some API calls. Utilizing APIs to harness AI capabilities is the most straightforward and prevalent method. Some may dismiss this approach as "not AI enough," but such skepticism is unwarranted—the true value lies in delivering user-centric capabilities rather than the extent of AI utilization.
Numerous providers offer APIs, including OpenAI (GPT), Google (Gemini), Anthropic (Claude), Mistral, and Cohere (Command), among others. Additionally, several platform providers like Replicate, Anyscale, Modal, and Banana offer various functionalities.
In this article, we'll begin with basic REST API calls.
REST APIs Overview
Most AI providers have a REST API available. Making calls is quite simple; you can use curl to send requests to the API endpoint, along with your API key and JSON payload.
For instance, here's how to call the OpenAI API for chat completions:
-H "Content-Type: application/json"
-H "Authorization: Bearer $OPENAI_API_KEY"
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Why is the sky blue?"
}
]
}'
You need a valid API key, which you can obtain by signing up with the provider. Once acquired, you can set it directly or define it as an environment variable:
$ export OPENAI_API_KEY=
Upon calling the API, you should receive a response similar to this:
{
"id": "chatcmpl-9RWBzicE7v7A1ZRLWUMX3a6zwooWd",
"object": "chat.completion",
"created": 1716345631,
"model": "gpt-4o-2024-05-13",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "The sky appears blue due to a phenomenon called Rayleigh scattering..."
}
}
],
"usage": {
"prompt_tokens": 23,
"completion_tokens": 286,
"total_tokens": 309
}
}
OpenAI is often recognized for pioneering this API format, and many other providers, such as Anthropic, follow suit. For example, you would call the Anthropic API like this:
-H "content-type: application/json"
-H "x-api-key: $ANTHROPIC_API_KEY"
-H "anthropic-version: 2023-06-01"
-d '{
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Why is the sky blue?"}]
}'
Each provider has its unique requirements for API key handling, but the structure of the payload is generally similar.
Moving on to Mistral, here's how you can make a call:
--header 'Content-Type: application/json'
--header 'Accept: application/json'
—header "Authorization: Bearer $MISTRAL_API_KEY"
--data '{"model": "mistral-large-latest",
"messages": [
{
"role": "user",
"content": "Why is the sky blue?"
}
]
}'
Notably, Google adopts a slightly different method by embedding the API key within the URL query, as shown below:
-H 'Content-Type: application/json'
-d '{ "contents":[
{ "parts":[{"text": "Why is the sky blue?"}]}]
}'
REST APIs are versatile and widely applicable. If you’re working with a programming language that doesn't have direct support from the provider, you can use an HTTP client library to interact with the REST API directly. Most programming languages offer HTTP client libraries, either natively or through third-party packages.
Using Python for API Calls
Python is notably well-supported for AI programming. Here's an example of calling the OpenAI API using its Python library:
from openai import OpenAI
client = OpenAI()
completion = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Why is the sky blue?"}
]
)
print(completion.choices[0].message.content)
This process is straightforward, as parameters and options can be configured when setting up the client.
For Anthropic, the call would look like this:
import anthropic
message = anthropic.Anthropic().messages.create(
model="claude-3-opus-20240229",
max_tokens=1024,
messages=[
{"role": "user", "content": "Why is the sky blue?"}]
)
print(message.content)
JavaScript (Node.js) API Access
While Python is the most commonly used language, JavaScript (Node.js) also has substantial support. Below is an example of accessing OpenAI APIs using JavaScript:
import OpenAI from "openai";
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Why is the sky blue?" }
],
});
console.log(completion.choices[0]);
Third-Party Libraries and Frameworks
In addition to the official libraries, many third-party libraries exist for various languages. For instance, the go-openai package allows calling OpenAI APIs in Go, and the SwiftOpenAI library can be used for Swift programming.
#### Langchain and LlamaIndex Frameworks
Langchain and LlamaIndex are two popular frameworks designed for working with LLMs (Large Language Models). Langchain, launched in October 2022, has rapidly evolved into a comprehensive ecosystem with various capabilities, including deployment servers and observability tools.
Here's a basic example of using Langchain to connect to OpenAI and call its chat API:
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(model_name="gpt-4o")
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant."),
("user", "{input}")
])
output_parser = StrOutputParser()
chain = prompt | llm | output_parser
results = chain.invoke({"input": "why is the sky blue?"})
print(results)
LlamaIndex, originally known as GPTIndex, focuses on connecting LLMs to datasets, making it easier to perform retrieval-augmented generation tasks.
from llama_index.core import Settings, VectorStoreIndex, SimpleDirectoryReader
from llama_index.llms.openai import OpenAI
Settings.llm = OpenAI(model="gpt-4o")
documents = SimpleDirectoryReader("data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("How has Smart Nation improved citizen's lives?")
print(response)
Conclusion
This article serves as an introduction to programming with AI through APIs. In upcoming installments, I will delve deeper into local LLMs, focusing on deploying them on personal machines, including laptops.
Learn how to trigger APIs using GPT to build your own AI smart assistant.
Discover the power of function calling with OpenAI API in this tutorial.