
Picture by Creator
Introduction
You don’t at all times want a heavy wrapper, an enormous consumer class, or dozens of strains 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 sort 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 quick rationalization and a hyperlink to official documentation, so you’ll be able to confirm what’s occurring below the hood. By the tip, you’ll know not solely learn how to drop in quick LLM calls but additionally perceive when and why every sample works.
Setting Up
Earlier than dropping within the one-liners, there are some things to organize so 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 setting 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 operating domestically and listening on the right port (as an example, Ollama’s default REST API runs at http://localhost:11434).
All one-liners assume you utilize the correct mannequin title and that the mannequin is both accessible by way of cloud or domestically. With that in place, you’ll be able to 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 best approach to begin utilizing giant language fashions. You don’t need to run a mannequin domestically or fear about GPU reminiscence; simply set up the consumer library, set your API key, and ship a immediate. These APIs are maintained by the mannequin suppliers themselves, so that they’re dependable, safe, and incessantly up to date.
The next one-liners present learn how to name among 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 provides entry to GPT fashions like GPT-4o and GPT-4o-mini. The SDK handles every part 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”}]).decisions[0].message.content material) |
What it does: It creates a consumer, 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 setting 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 an analogous 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 consumer, sends a message, and prints the textual content of the primary response block.
Why it really works: The .messages.create()
technique makes use of a normal message schema (position + content material), returning structured output that’s simple to extract.
Documentation: Anthropic Claude API Reference
3. Google Gemini
Google’s Gemini API (by way of 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 title, and generate_content()
handles the immediate/response circulate. You simply want your GOOGLE_API_KEY
configured.
Documentation: Google Gemini API Quickstart
4. Mistral AI (REST request)
Mistral supplies a easy chat-completions REST API. You ship an inventory of messages and obtain a structured JSON response in return.
import requests, json; print(requests.submit(“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 decisions -> message -> content material
.
Take a look at the Mistral API reference and quickstart.
5. Hugging Face Inference API
In case you host a mannequin or use a public one on Hugging Face, you’ll be able to name it with a single POST. The text-generation
job returns generated textual content in JSON.
import requests; print(requests.submit(“https://api-inference.huggingface.co/fashions/mistralai/Mistral-7B-Instruct-v0.2”, headers={“Authorization”:“Bearer YOUR_HF_TOKEN”}, json={“inputs”:“Write a haiku about knowledge”}).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 an inventory with generated_text
.
Documentation: Inference API and Textual content Era job pages.
Native Mannequin One-Liners
Operating fashions in your machine provides you privateness and management. You keep away from community latency and preserve knowledge native. The tradeoff is about up: you want the server operating and a mannequin pulled. The one-liners beneath assume you have got 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.submit(“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 an area HTTP server with endpoints like /api/generate
and /api/chat
. You will need to have the app operating and the mannequin pulled first. See official API documentation.
7. LM Studio (OpenAI-Appropriate Endpoint)
LM Studio can serve native fashions behind OpenAI-style endpoints akin to /v1/chat/completions
. Begin the server from the Developer tab, then name it like all OpenAI-compatible backend.
import requests; print(requests.submit(“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 an area 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. Current releases additionally add /v1/responses
help. Test the docs in case your native construct makes use of a special route.
8. vLLM (Self-Hosted LLM Server)
vLLM supplies 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.submit(“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 consumer or plain requests name works as soon as the server is operating. Test the documentation.
Helpful Methods and Ideas
As soon as you understand the fundamentals of sending requests to LLMs, a couple of neat tips make your workflow quicker and smoother. These remaining two examples reveal learn how to stream responses in real-time and learn how to execute asynchronous API calls with out blocking your program.
9. Streaming Responses from OpenAI
Streaming means that you can print every token as it’s generated by the mannequin, relatively than ready for the total message. It’s good for interactive apps or CLI instruments the place you need output to seem 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 “stay typing” impact.
Why it really works: The stream=True
flag in OpenAI’s API returns partial occasions. Every chunk
comprises a delta.content material
area, which this one-liner prints because it streams in.
Documentation: OpenAI Streaming Information.
10. Async Calls with httpx
Asynchronous calls allow you to question fashions with out blocking your app, making them splendid for making a number of requests concurrently or integrating LLMs into internet servers.
import asyncio, httpx; print(asyncio.run(httpx.AsyncClient().submit(“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 primary thread. This sample is helpful for light-weight concurrency in scripts or apps.
Documentation: Async Assist.
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 software, or construct them right into a backend service. The identical code that matches on one line can simply develop into manufacturing workflows when you add error dealing with, caching, or logging.
If you wish to discover additional, verify the official documentation for detailed parameters like temperature, max tokens, and streaming choices. Every supplier maintains dependable references:
The true takeaway is that Python makes working with LLMs each accessible and versatile. Whether or not you’re operating GPT-4o within the cloud or Llama 3 domestically, you’ll be able to attain production-grade outcomes with only a few strains of code.