
Picture by Creator
Introduction
You don’t all the time want a heavy wrapper, an enormous shopper class, or dozens of traces of boilerplate to name a big language mannequin. Generally one well-crafted line of Python does all of the work: ship a immediate, obtain a response. That type of simplicity can velocity up prototyping or embedding LLM calls inside scripts or pipelines with out architectural overhead.
On this article, you’ll see ten Python one-liners that decision and work together with LLMs. We’ll cowl:
Every snippet comes with a short clarification and a hyperlink to official documentation, so you possibly can confirm what’s occurring beneath the hood. By the tip, you’ll know not solely how one can drop in quick LLM calls but in addition perceive when and why every sample works.
Setting Up
Earlier than dropping within the one-liners, there are some things to arrange in order that they run easily:
Set up required packages (solely as soon as):
|
pip set up openai anthropic google–generativeai requests httpx |
Guarantee your API keys are set in atmosphere variables, by no means hard-coded in your scripts. For instance:
|
export OPENAI_API_KEY=“sk-…” export ANTHROPIC_API_KEY=“claude-yourkey” export GOOGLE_API_KEY=“your_google_key” |
For native setups (Ollama, LM Studio, vLLM), you want the mannequin server working domestically and listening on the proper port (as an illustration, Ollama’s default REST API runs at http://localhost:11434).
All one-liners assume you employ the appropriate mannequin identify and that the mannequin is both accessible through cloud or domestically. With that in place, you possibly can paste every one-liner straight into your Python REPL or script and get a response, topic to quota or native useful resource limits.
Hosted API One-Liners (Cloud Fashions)
Hosted APIs are the simplest option to begin utilizing massive language fashions. You don’t need to run a mannequin domestically or fear about GPU reminiscence; simply set up the shopper library, set your API key, and ship a immediate. These APIs are maintained by the mannequin suppliers themselves, in order that they’re dependable, safe, and steadily up to date.
The next one-liners present how one can name a few of the hottest hosted fashions straight from Python. Every instance sends a easy message to the mannequin and prints the generated response.
1. OpenAI GPT Chat Completion
OpenAI’s API offers entry to GPT fashions like GPT-4o and GPT-4o-mini. The SDK handles the whole lot from authentication to response parsing.
|
from openai import OpenAI; print(OpenAI().chat.completions.create(mannequin=“gpt-4o-mini”, messages=[{“role”:“user”,“content”:“Explain vector similarity”}]).selections[0].message.content material) |
What it does: It creates a shopper, sends a message to GPT-4o-mini, and prints the mannequin’s reply.
Why it really works: The openai Python bundle wraps the REST API cleanly. You solely want your OPENAI_API_KEY set as an atmosphere variable.
Documentation: OpenAI Chat Completions API
2. Anthropic Claude
Anthropic’s Claude fashions (Claude 3, Claude 3.5 Sonnet, and so forth.) are identified for his or her lengthy context home windows and detailed reasoning. Their Python SDK follows the same chat-message format to OpenAI’s.
|
from anthropic import Anthropic; print(Anthropic().messages.create(mannequin=“claude-3-5-sonnet”, messages=[{“role”:“user”,“content”:“How does chain of thought prompting work?”}]).content material[0].textual content) |
What it does: Initializes the Claude shopper, sends a message, and prints the textual content of the primary response block.
Why it really works: The .messages.create() methodology makes use of a regular message schema (function + content material), returning structured output that’s straightforward to extract.
Documentation: Anthropic Claude API Reference
3. Google Gemini
Google’s Gemini API (through the google-generativeai library) makes it easy to name multimodal and textual content fashions with minimal setup. The important thing distinction is that Gemini’s API treats each immediate as “content material era,” whether or not it’s textual content, code, or reasoning.
|
import os, google.generativeai as genai; genai.configure(api_key=os.getenv(“GOOGLE_API_KEY”)); print(genai.GenerativeModel(“gemini-1.5-flash”).generate_content(“Describe retrieval-augmented era”).textual content) |
What it does: Calls the Gemini 1.5 Flash mannequin to explain retrieval-augmented era (RAG) and prints the returned textual content.
Why it really works: GenerativeModel() units the mannequin identify, and generate_content() handles the immediate/response movement. You simply want your GOOGLE_API_KEY configured.
Documentation: Google Gemini API Quickstart
4. Mistral AI (REST request)
Mistral gives a easy chat-completions REST API. You ship a listing of messages and obtain a structured JSON response in return.
|
import requests, json; print(requests.publish(“https://api.mistral.ai/v1/chat/completions”, headers={“Authorization”:“Bearer YOUR_MISTRAL_API_KEY”}, json={“mannequin”:“mistral-tiny”,“messages”:[{“role”:“user”,“content”:“Define fine-tuning”}]}).json()[“choices”][0][“message”][“content”]) |
What it does: Posts a chat request to Mistral’s API and prints the assistant message.
Why it really works: The endpoint accepts an OpenAI-style messages array and returns selections -> message -> content material.
Try the Mistral API reference and quickstart.
5. Hugging Face Inference API
Should you host a mannequin or use a public one on Hugging Face, you possibly can name it with a single POST. The text-generation activity returns generated textual content in JSON.
|
import requests; print(requests.publish(“https://api-inference.huggingface.co/fashions/mistralai/Mistral-7B-Instruct-v0.2”, headers={“Authorization”:“Bearer YOUR_HF_TOKEN”}, json={“inputs”:“Write a haiku about information”}).json()[0][“generated_text”]) |
What it does: Sends a immediate to a hosted mannequin on Hugging Face and prints the generated textual content.
Why it really works: The Inference API exposes task-specific endpoints; for textual content era, it returns a listing with generated_text.
Documentation: Inference API and Textual content Technology task pages.
Native Mannequin One-Liners
Working fashions in your machine offers you privateness and management. You keep away from community latency and maintain information native. The tradeoff is about up: you want the server working and a mannequin pulled. The one-liners under assume you will have already began the native service.
6. Ollama (Native Llama 3 or Mistral)
Ollama exposes a easy REST API on localhost:11434. Use /api/generate for prompt-style era or /api/chat for chat turns.
|
import requests; print(requests.publish(“http://localhost:11434/api/generate”, json={“mannequin”:“llama3”,“immediate”:“What’s vector search?”}).textual content) |
What it does: Sends a generate request to your native Ollama server and prints the uncooked response textual content.
Why it really works: Ollama runs a neighborhood HTTP server with endpoints like /api/generate and /api/chat. You need to have the app working and the mannequin pulled first. See official API documentation.
7. LM Studio (OpenAI-Suitable Endpoint)
LM Studio can serve native fashions behind OpenAI-style endpoints similar to /v1/chat/completions. Begin the server from the Developer tab, then name it like several OpenAI-compatible backend.
|
import requests; print(requests.publish(“http://localhost:1234/v1/chat/completions”, json={“mannequin”:“phi-3”,“messages”:[{“role”:“user”,“content”:“Explain embeddings”}]}).json()[“choices”][0][“message”][“content”]) |
What it does: Calls a neighborhood chat completion and prints the message content material.
Why it really works: LM Studio exposes OpenAI-compatible routes and in addition helps an enhanced API. Latest releases additionally add /v1/responses assist. Examine the docs in case your native construct makes use of a special route.
8. vLLM (Self-Hosted LLM Server)
vLLM gives a high-performance server with OpenAI-compatible APIs. You may run it domestically or on a GPU field, then name /v1/chat/completions.
|
import requests; print(requests.publish(“http://localhost:8000/v1/chat/completions”, json={“mannequin”:“mistral”,“messages”:[{“role”:“user”,“content”:“Give me three LLM optimization tricks”}]}).json()[“choices”][0][“message”][“content”]) |
What it does: Sends a chat request to a vLLM server and prints the primary response message.
Why it really works: vLLM implements OpenAI-compatible Chat and Completions APIs, so any OpenAI-style shopper or plain requests name works as soon as the server is working. Examine the documentation.
Helpful Tips and Ideas
As soon as the fundamentals of sending requests to LLMs, just a few neat tips make your workflow quicker and smoother. These closing two examples exhibit how one can stream responses in real-time and how one can execute asynchronous API calls with out blocking your program.
9. Streaming Responses from OpenAI
Streaming permits you to print every token as it’s generated by the mannequin, fairly than ready for the complete message. It’s good for interactive apps or CLI instruments the place you need output to look immediately.
|
from openai import OpenAI; [print(c.choices[0].delta.content material or “”, finish=“”) for c in OpenAI().chat.completions.create(mannequin=“gpt-4o-mini”, messages=[{“role”:“user”,“content”:“Stream a poem”}], stream=True)] |
What it does: Sends a immediate to GPT-4o-mini and prints tokens as they arrive, simulating a “reside typing” impact.
Why it really works: The stream=True flag in OpenAI’s API returns partial occasions. Every chunk comprises a delta.content material discipline, which this one-liner prints because it streams in.
Documentation: OpenAI Streaming Guide.
10. Async Calls with httpx
Asynchronous calls allow you to question fashions with out blocking your app, making them excellent for making a number of requests concurrently or integrating LLMs into internet servers.
|
import asyncio, httpx; print(asyncio.run(httpx.AsyncClient().publish(“https://api.mistral.ai/v1/chat/completions”, headers={“Authorization”:“Bearer TOKEN”}, json={“mannequin”:“mistral-tiny”,“messages”:[{“role”:“user”,“content”:“Hello”}]})).json()[“choices”][0][“message”][“content”]) |
What it does: Posts a chat request to Mistral’s API asynchronously, then prints the mannequin’s reply as soon as full.
Why it really works: The httpx library helps async I/O, so community calls don’t block the principle thread. This sample is helpful for light-weight concurrency in scripts or apps.
Documentation: Async Support.
Wrapping Up
Every of those one-liners is greater than a fast demo; it’s a constructing block. You may flip any of them right into a perform, wrap them inside a command-line device, or construct them right into a backend service. The identical code that matches on one line can simply broaden into manufacturing workflows when you add error dealing with, caching, or logging.
If you wish to discover additional, test the official documentation for detailed parameters like temperature, max tokens, and streaming choices. Every supplier maintains dependable references:
The actual takeaway is that Python makes working with LLMs each accessible and versatile. Whether or not you’re working GPT-4o within the cloud or Llama 3 domestically, you possibly can attain production-grade outcomes with only a few traces of code.

